我是靠谱客的博主 幸福小土豆,这篇文章主要介绍iOS_加密保护1_图片资源,现在分享给大家,希望可以做个参考。

由于版权、防盗用、防山寨的需要

ipa的bundle中的图片需要进行加密,

用户安装app时,bundle中的图片是进行加密后的图片



具体加密思路如下:

1、将要加密的图片全部放到桌面上的一个文件夹内

2、新建一个Mac 命令行项目,对该文件夹内的所有图片进行加密,具体如下

3、读取原始图片的data

4、对前1000位进行异或处理

5、使用GTMBase64编码data,下载地址

6、将加密后的data,写到文件夹,这些图片,就是要拖到bundle里面的图片



具体解密思路如下:

1、使用图片时,先从bundle读取到图片的data

2、使用GTMBase64解码data

3、对前1000位进行异或处理

4、根据data 生成UIImage

5、显示到UIImageView上


命令行项目:

1、导入GTMBase64.h 、GTMBase64.m、GTMDefines.h

2、删除autorelease或添加

复制代码
1
-fno-objc-arc
3、将所有的原始图片放到  /Users/beyond/Desktop/tmp_tai_pic

4、获取所有该文件夹下的图片,进行遍历


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// // main.m // encode // // Created by beyond on 16/4/2. // Copyright (c) 2016年 beyond. All rights reserved. // #import <Foundation/Foundation.h> #import "GTMBase64.h" #import "GTMDefines.h" #define kEncodeKeyLength (21) static char arrayForEncode[kEncodeKeyLength] = "beyond+2016+xsism.com"; int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); NSString *picFolderPath = @"/Users/beyond/Desktop/tmp_tai_pic"; NSArray *shortPicNameArr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:picFolderPath error:nil]; NSLog(@"arr:%@",shortPicNameArr); for (NSString *shortPicName in shortPicNameArr) { // /Users/beyond/Desktop/tmp_tai_pic/9.png NSString *fullPicPath = [picFolderPath stringByAppendingPathComponent:shortPicName]; NSLog(@"fullPicPath:%@",fullPicPath); // 读原始数据 NSData *imageDataOrigin = [NSData dataWithContentsOfFile:fullPicPath]; // 对前1000位进行异或处理 unsigned char * cByte = (unsigned char*)[imageDataOrigin bytes]; for (int index = 0; (index < [imageDataOrigin length]) && (index < kEncodeKeyLength); index++, cByte++) { *cByte = (*cByte) ^ arrayForEncode[index]; } //对NSData进行base64编码 NSData *imageDataEncode = [GTMBase64 encodeData:imageDataOrigin]; // /Users/beyond/Desktop/tmp_tai_pic/encode_9.png NSString *newShortPicName = [NSString stringWithFormat:@"encode_%@",shortPicName]; NSString *newFullPicPath = [picFolderPath stringByAppendingPathComponent:newShortPicName]; NSLog(@"fullPicPath:%@",fullPicPath); [imageDataEncode writeToFile:newFullPicPath atomically:YES]; } } return 0; }



iOS项目:

1、删除项目bundle中原来的图片(或不加入target中),将上面加密过的图片加入到工程中

2、在使用到UIImage的方法中,注释掉原来的方法

    

复制代码
1
2
3
// 未进行加密前,使用的是 1.gif // cell.xib_imgView.image = [UIImage imageNamed:imgName];

3、根据新的图片encode_1.gif读取data

4、使用GTMBase64解码data

5、对前1000位进行异或处理

6、根据解密后的data 生成UIImage,显示到UIImageView上

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 进行了加密后,根据data生成图片 NSString *newImgName = [NSString stringWithFormat:@"encode_%@",imgName]; NSString *filePath = [[NSBundle mainBundle]pathForResource:newImgName ofType:nil]; // 读取被加密文件对应的数据 NSData *dataEncoded = [NSData dataWithContentsOfFile:filePath]; // 对NSData进行base64解码 NSData *dataDecode = [GTMBase64 decodeData:dataEncoded]; // 对前1000位进行异或处理 unsigned char * cByte = (unsigned char*)[dataDecode bytes]; for (int index = 0; (index < [dataDecode length]) && (index < kEncodeKeyLength); index++, cByte++) { *cByte = (*cByte) ^ arrayForEncode[index]; } // 根据解密后的Data生成Image cell.xib_imgView.image = [UIImage imageWithData:dataDecode];







对plist加密同上,解密方法如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
NSString *filePath = @"/Users/beyond/Desktop/tmp_encode/plist/encode_k_Riyu_Alpha_List.plist"; // 进行了加密后,根据data生成图片 // 读取被加密文件对应的数据 NSData *dataEncoded = [NSData dataWithContentsOfFile:filePath]; // 对NSData进行base64解码 NSData *dataDecode = [GTMBase64 decodeData:dataEncoded]; // 对前1000位进行异或处理 unsigned char * cByte = (unsigned char*)[dataDecode bytes]; for (int index = 0; (index < [dataDecode length]) && (index < kEncodeKeyLength); index++, cByte++) { *cByte = (*cByte) ^ arrayForEncode[index]; } // 根据解密后的Data生成Image ; NSString *error; NSPropertyListFormat format; NSArray* plist = [NSPropertyListSerialization propertyListFromData:dataDecode mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error]; NSLog( @"plist arr is:%@", plist );








最后

以上就是幸福小土豆最近收集整理的关于iOS_加密保护1_图片资源的全部内容,更多相关iOS_加密保护1_图片资源内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部