encryption/解密.plist文件ios

我有一个plist与一些存储的数据,并希望encryption解密,所以它是不可读的使用目标c。 我读过AESencryption等,但我希望整个plistencryption一些怎么不在plist中的string….

任何帮助将非常感激。

howanghk提供的链接包含有错误的代码。 在网页上应用InoriXu提供的修复程序来解决问题。 您必须修改encryption和解密函数。

所以在一行之后:

const char *password = [pass UTF8String]; 

加:

 const int passwordLen = [pass length]; 

并换行:

 key[i] = password != 0 ? *password++ : 0; 

成:

 key[i] = i < passwordLen != 0 ? *password++ : 0; 

代码本身仍然增加了一些空白填充,但如果你需要它来encryption属性列表,你会没事的。

使用http://blog.objectgraph.com/index.php/2010/04/20/encrypting-decrypting-base64-encode-decode-in-iphone-objective-c/上的代码(您在评论中提供的链接),你可以通过以下方式encryption你的plist&#xFF1A;

 NSData *plistFileData = [NSData dataWithContentsOfFile:plistPath]; NSData *encryptedData = [plistFileData AESEncryptWithPassphrase:password]; [encryptedData writeToFile:encryptedPath atomically:YES]; 

plistPath是一个NSString,包含要encryption的plist文件的path
密码是您想要使用的encryption密钥
encryptedPath是您要保存encryption文件的位置

解密:

 NSData *encryptedData = [NSData dataWithContentsOfFile:encryptedPath]; NSData *plistFileData = [plistFileData AESDecryptWithPassphrase:password]; [plistFileData writeToFile:plistPath atomically:YES]; 

encryptedPath是一个NSString,包含encryption的plist文件的path
密码是您想要使用的encryption密钥
plistPath是你想要保存解密后的plist文件的地方

这是一个非常简单的答案,希望这可以简化问题,如果有的话;

首先你需要在这里下载NSData + AES文件。 你只需要NSData + AES.h和NSData + AES.m以及cipher.h和cipher.m文件。 一旦羁押,将这些文件添加到您的Xcode项目中,并从NSData + AES.h和cipher.h中删除#import Cocoa / Cocoa.h头(仅适用于那些打算在iOS上编程的人,如果是MacOS,请让头是)。 在您的文件中导入NSData + AES.h,您可以在其中获取和写入plist文件。

现在已经确定了最初的基础知识,我们将使用这些重要的文件。 您需要了解的是您想要解密和encryption数据的方式。 在第一次运行中,您需要将您的plist复制到文档文件夹,然后对其进行encryption。 请注意,如果您复制它并尝试直接解密它,则会抛出exception,因此为了迎合这一点,我们将使用UserDefaults布尔值并在第一次运行时跳过解密。 你也需要定义一个预处理指令常量string,以娱乐encryption和解密的密钥。 这是你的DataHandler类中的东西。

  #import <Foundation/Foundation.h> #import "NSData+AES.h" #define MY_SECRET_KEY @"MY_SECRET_KEY" static NSMutableDictionary *dataDictionary_ = nil; static NSMutableDictionary *allSettings_ = nil; @implementation DataHandler - (id)init { if(self = [super init]) { [self copyPlistData]; } return self; } // Encrypt File - (NSData*)encryptFile:(NSMutableDictionary *)plistDict { NSError *err = nil; NSData *data = [NSPropertyListSerialization dataWithPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 options:0 error:&err]; NSData *file = [data encryptWithString:MY_SECRET_KEY]; return file; } // Decrypt File - (NSMutableDictionary *)decryptFile:(NSData *)data { NSError *err = nil; NSData* newData = [data decryptWithString:MY_SECRET_KEY]; NSPropertyListFormat format; NSMutableDictionary *file = [NSPropertyListSerialization propertyListWithData:newData options:NSPropertyListMutableContainersAndLeaves format:&format error:&err]; return file; } - (void) copyPlistData { NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent: @"myData.plist"]; NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL fileExists = [fileManager fileExistsAtPath:path]; //check if the file exists already in users documents folder //if file does not exist copy it from the APPLICATION bundle Plist file if (!fileExists) { NSLog(@"copying database to users documents"); NSString *pathToSettingsInBundle = [[NSBundle mainBundle] pathForResource:@"mydata" ofType:@"plist"]; BOOL copySuccess = [fileManager copyItemAtPath:pathToSettingsInBundle toPath:path error:&error]; if(copySuccess) { noCopyError_ = YES; } } //if file is already there do nothing else { noCopyError_ = YES; NSLog(@"users database already configured"); } BOOL firstRun = [[NSUserDefaults standardUserDefaults] boolForKey:@"IS_FIRST_RUN"]; if(noCopyError_ && firstRun) { dataDictionary_ = [self decryptFile:[NSData dataWithContentsOfFile:path]]; } else { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"IS_FIRST_RUN"]; [[NSUserDefaults standardUserDefaults] synchronize]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"mydata.plist"]; dataDictionary_ = (NSMutableDictionary*)[[NSDictionary alloc ] initWithContentsOfFile:plistPath]; NSMutableDictionary *data = (NSMutableDictionary*)[dictionaryDATA_ objectForKey:@"Data"]; allSettings_ = [data objectForKey:@"AllSettings"]; } } - (NSMutableDictionary*) properties { NSMutableDictionary * props = [[NSMutableDictionary alloc]init]; [props setObject: allSettings_ forKey:@"AllSettings"]; NSMutableDictionary * data = [NSMutableDictionary dictionaryWithObject:props forKey:@"Data"]; return data; } - (void)persistData { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"mydata.plist"]; NSMutableDictionary *dict = [self properties]; NSData *encryptedDict = [self encryptFile:dict]; [encryptedDict writeToFile:plistPath atomically:YES]; } 

但是dataDictionary_第一次被填充时,我们必须强制将它保存在didFinishLaunching的AppDelegate.m中:

  DataHandler *dataHandler = [[DataHandler alloc] init]; [dataHandler persistData]; 

数据将始终被encryption,但是在copyPlist方法中,您将针对dataDictionary_填充模型,并与这些模型进行交互。 完成后,您将坚持您的模型并再次encryption,因此不会发生错误。 这是一个简单而且没有任何麻烦的可行解决scheme。 干杯。