如何保护iOS包文件,如plist,image,sqlite,媒体文件

Rav*_*ran 12 iphone bundle protection ios

我创建了示例hello world项目,然后将Data.plist文件添加到资源文件夹中.现在,人们可以通过解压缩.ipa文件轻松获取捆绑文件.有没有办法保护保存在iPhone应用程序包中的Data.plist文件?

 Encryption is a decent method of scrambling the data but i don't know how to implement encription concept.
Run Code Online (Sandbox Code Playgroud)

你有任何示例代码吗?

在此输入图像描述

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    NSArray *arrData = [[NSArray alloc]initWithContentsOfFile:filePath];

    NSData *datas = [NSKeyedArchiver archivedDataWithRootObject:arrData];
    [datas writeToFile:filePath atomically:YES];
Run Code Online (Sandbox Code Playgroud)

提取IPA文件后

在此输入图像描述

Dai*_*jan 12

  1. 在部署期间加密mac上的文件:

    首先:不要将要加密的文件添加到目标中,
    例如Encryption-Test.plist

    然后向您的xcode项目添加一个shell脚本阶段,openssl用于加密和复制文件.
    例如
    openssl enc -e -aes-256-cbc -pass pass:asdasd -in $PROJECT_DIR/test/Encryption-Test.plist -out $TARGET_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/Encryption-Test.enc

  2. 将github中的RNCryptor源文件添加到您的项目中.这使得解密openSSL加密的AES文件变得非常容易.(谢谢抢!)https://github.com/RNCryptor/RNCryptor (Apple的CCrypt api不好直接合作)

  3. 加载数据并解密:

例如

@implementation TestViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Encryption-Test" ofType:@"enc"];
    NSData *passEncryptedData =[[NSData alloc] initWithContentsOfFile:path];
    NSString *pass = @"asdasd";

    NSData *dataDecrypted = [RNOpenSSLDecryptor decryptData:passEncryptedData withSettings:kRNCryptorAES256Settings password:pass error:nil];
    id plist = [NSPropertyListSerialization propertyListFromData:dataDecrypted mutabilityOption:NSPropertyListImmutable format:nil errorDescription:nil];

    assert(plist);
    self.text.text = [plist description];
}

@end
Run Code Online (Sandbox Code Playgroud)

添加完整示例:https://github.com/Daij-Djan/encryptBundleFiles


小智 -1

使用 NSKeyedArchiver 从字典中创建 NSData 对象(NSKeyedArchiver archivedDataWithRootObject:)。然后使用 AES 加密 NSData 并将其写入您的文件。

读取相反的过程:首先,读取 NSData,通过上述链接中的方法解密它,然后将解密的 NSData 传递给 NSKeyedUnarchiver (NSKeyedUnarchiver unarchiveObjectWithData:),然后你就可以取回你的字典了。您可以将其用于 plist 文件或 NSDictionary 以确保数据安全。

示例1

示例2

编辑2:

NSDictionary *Your_NSDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"Obj1", @"Key1",
                           @"Obj2", @"Key2", nil];
//store dictionary
NSMutableData *yourData = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:Your_NSDictionary forKey: @"key"];
[archiver finishEncoding];
[yourData writeToFile:@"FilePath" atomically:YES];
Run Code Online (Sandbox Code Playgroud)

或者

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"Data" 
                                                 ofType:@"plist"];
NSDictionary* data = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSMutableDictionary * rootObject;
rootObject = [NSMutableDictionary dictionary];
[rootObject setValue: data forKey:@"accounts"];
[NSKeyedArchiver archiveRootObject: rootObject toFile: path];
Run Code Online (Sandbox Code Playgroud)