加密plist文件方法

Encrypting Plist File Method

我有一个 Property List 标题为 GameData.plist。我想看看我加密 plist 的方式是否正确。由于您无法手动编辑主捆绑文件(这本来是查看数据现在是否确实加密的一种简单方法),而且我似乎没有通过 iExplorer 在可用数据中找到我的 plist,我是不确定是否真的对任何内容进行了加密。

目标是用加密数据覆盖信息。我不确定它是不是这样工作的,我只是想阻止 plist 中的数据在应用程序外部被查看 运行 或 运行。这是我正在做的事情:

- (void)encryptionWithAES256 {
        .
        .
        .
    NSString *path = [[ NSBundle mainBundle] bundlePath];
    NSString *finalPath = [ path stringByAppendingPathComponent:@"GameData"];
    NSDictionary *plistDic = [NSDictionary dictionaryWithContentsOfFile:finalPath];
    NSLog(@"Plist: %@", plistDic);
    
    NSData *plistData = [NSKeyedArchiver archivedDataWithRootObject:plistDic];
    NSData *encryptedData = [plistData AES256EncryptWithKey:@"<ENCRYPTION_KEY>"];
    [encryptedData writeToFile:finalPath atomically:YES];
        .
        .
        .
}

注意:出于测试目的,GameData.plist 只是 Xcode 创建的默认 plist一个项目,如下图:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Test</key>
        <string>GameDataTest</string>
        <key>CFBundleDevelopmentRegion</key>
        <string>en</string>
        <key>CFBundleExecutable</key>
        <string>$(EXECUTABLE_NAME)</string>
        <key>CFBundleIdentifier</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>CFBundleName</key>
        <string>$(PRODUCT_NAME)</string>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleShortVersionString</key>
        <string>1.0</string>
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleVersion</key>
        <string>1</string>
        <key>LSRequiresIPhoneOS</key>
        <true/>
        <key>UILaunchStoryboardName</key>
        <string>LaunchScreen</string>
        <key>UIMainStoryboardFile</key>
        <string>Main</string>
        <key>UIRequiredDeviceCapabilities</key>
        <array>
            <string>armv7</string>
        </array>
        <key>UISupportedInterfaceOrientations</key>
        <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
        </array>
        <key>UISupportedInterfaceOrientations~ipad</key>
        <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationPortraitUpsideDown</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
        </array>
    </dict>
</plist>

我如何检查数据是否被现在加密的数据“替换”或覆盖?然后我尝试插入文件内容的日志 before 此方法被调用以查看它是否会检索并显示加密的 plist 数据,但它不会。我不确定那是因为它只是从主包中的 plist 读取数据,还是 writeToFile 没有做我需要的。谢谢!

更新 这是为普通用户准备的,他们试图获取我的应用程序数据,这样他们就有可能将 plist 视为一团加密,而不是所有纯文本 plist 数据。

如果您希望加密数据随您的应用程序一起交付,则需要在构建过程中对其进行加密,而不是在运行时进行加密。最简单的方法是根据需要加密文件,然后将其作为资源添加到 Xcode 项目中。然后,您可以像读取任何其他资源一样将其作为 NSData 读取并在内存中解密。