加密Plist文件方法

时间:2016-01-27 17:30:51

标签: ios objective-c encryption plist

我有Property List标题为 GameData.plist 。我试图看看我加密plist的方式是否正确。由于您无法手动编辑主捆绑文件(这可能是查看数据是否确实现在已加密的简单方法),而且我似乎无法通过iExplorer在可用数据中找到我的plist,我不确定是否有任何实际加密。

目标是用加密数据覆盖信息。我不确定它是如何工作的,我只是想阻止plist中的数据在app运行或不运行之外被查看。这就是我在做的事情:

- (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>

如何检查数据是否被替换&#34;或被现在加密的数据覆盖?我尝试在之前插入文件内容的日志调用此方法以查看它是否将检索并显示加密的plist数据,但事实并非如此。我不确定是不是因为它只是从主包中的plist中读取数据,或者writeToFile没有按照我的需要进行操作。谢谢!

更新 这适用于普通用户试图获取我的应用数据,以便他们可以将plist视为一堆加密而不是所有纯文本plist数据。

1 个答案:

答案 0 :(得分:1)

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