直接上源码:
#import <Foundation/Foundation.h>
#import "DataEncryption.h"
/*
思想:
可以用归档输出需要加密的资源文件,要用资源文件时再进行解档。
*/
NSString * OutputFilePath(NSString *fileName) {
NSString *outPutPath = [NSString stringWithFormat:@"/Users/#这里替换成你本机的用户名#/Desktop/Resource/%@%@",subPath,fileNam];
return outPutPath;
}
NSString * InputFilePath(NSString *fileName) {
//资源文件路径,其实就是那个 Resources 文件夹的路径
return [NSString stringWithFormat:@"/Users/#这里替换成你本机的用户名#/Documents/ResourceEncryption/ResourceEncryption/Resource/%@", fileName];
}
/**归档二进制文件*/
void EncryptionDataFiles(NSArray *names) {
[names enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSData *data = [NSData dataWithContentsOfFile:InputFilePath(obj)];
[NSKeyedArchiver archiveRootObject:data toFile:OutputFilePath(obj)];
}];
}
/**归档数组类型的Plist文件*/
void EncryptionArrayPlistFiles(NSArray *names) {
[names enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//原理同上
NSArray *array = [NSArray arrayWithContentsOfFile:InputFilePath(obj)];
[NSKeyedArchiver archiveRootObject:array toFile:OutputFilePath(obj)];
}];
}
/**归档字典类型的Plist文件*/
void EncryptionDictionaryPlistFiles(NSArray *names) {
[names enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//原理同上
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:InputFilePath(obj)];
NSString *str = [DataEncryption convertToJsonData:dic];
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[NSKeyedArchiver archiveRootObject:data toFile:OutputFilePath(obj)];
}];
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *names = @[@"aa.plist"];
EncryptionDataFiles(names);
EncryptionArrayPlistFiles(names);
EncryptionDictionaryPlistFiles(names);
}
return 0;
}
PS:
解档的话,要保证需要解档的文件路径是完全没问题的,否则解不出来
网友评论