无法加密和解密 plist 中的数据
Posted
技术标签:
【中文标题】无法加密和解密 plist 中的数据【英文标题】:Unable to encrypt and decrypt data in plist 【发布时间】:2015-03-05 06:02:19 【问题描述】:我正在尝试使用 RNCryptor 加密保存的 plist 数据并对其进行解密。 输出在加密文件中都是乱码,但解密后无法得到任何东西。
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"data.plist"]];
NSError *error1;
bac = [RNEncryptor encryptData:data withSettings:kRNCryptorAES256Settings password:@"abcdef" error:&error1];
NSString *pathToDesktop = [NSString stringWithFormat:@"/Users/rajparmar/Desktop/encrypt.plist"];
[bac writeToFile:pathToDesktop atomically:YES];
这里是解密代码
NSError *error1;
NSData *decryptedData = [RNDecryptor decryptData:bac
withPassword:@"abcdef"
error:&error1];
NSString *pathToDesktop1 = [NSString stringWithFormat:@"/Users/rajparmar/Desktop/decrypt.plist"];
[decryptedData writeToFile:pathToDesktop1 atomically:YES];
【问题讨论】:
检查desktop-path-on-mac是不可能的。 【参考方案1】:包含使用 RNEncryptor 和 RNDecryptor 的代码的完整示例。
#import "ViewController.h"
#import <Security/Security.h>
#import "RNCryptor/RNEncryptor.h"
#import "RNCryptor/RNDecryptor.h"
@interface ViewController ()
UILabel* lable;
NSMutableDictionary* plistDict;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
UIButton* btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(0, 0, 150, 60);
btn1.backgroundColor = [UIColor lightGrayColor];
[btn1 setTitle:@"SaveData" forState:UIControlStateNormal];
btn1.center = self.view.center;
[btn1 addTarget:self action:@selector(saveDataToPlist:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
UIButton* btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(btn1.frame.origin.x, btn1.frame.origin.y + 100, 150, 60);
btn2.backgroundColor = [UIColor lightGrayColor];
[btn2 setTitle:@"Show Data" forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(getDataFromPlist:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
lable = [[UILabel alloc] initWithFrame:CGRectMake(btn2.frame.origin.x, btn2.frame.origin.y + 100, 300, 40)];
lable.text = @"";
[self.view addSubview:lable];
//create plist if not exits
[self createPlistIfNotExists];
#pragma mark - Button Events
- (void)saveDataToPlist:(id)sender
NSString* eString = @"Hello World How are you";
[self insertEncryptedDataIntoPlist:eString forKey:@"ENData"];
- (void)getDataFromPlist:(id)sender
NSString* dString = [self decryptDataFromPlistForKey:@"ENData"];
[lable setText:dString];
#pragma mark - Encryption and Decryption
- (void)insertEncryptedDataIntoPlist:(NSString*)aDataString forKey:(NSString*)aKey
NSError* error;
NSData* data;
data = [RNEncryptor encryptData:[aDataString dataUsingEncoding:NSUTF8StringEncoding]
withSettings:kRNCryptorAES256Settings
password:@"@ABC123"
error:&error];
if (error)
NSLog(@"Failed: %@", error);
exit(1);
[plistDict setObject:data forKey:aKey];
[plistDict writeToFile:[self getPlistPath] atomically:YES];
- (NSString*)decryptDataFromPlistForKey:(NSString*)aKey
NSMutableDictionary* savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:[self getPlistPath]];
NSError* error;
NSData* data;
data = [RNDecryptor decryptData:[[NSData alloc] initWithData:[savedStock valueForKey:aKey]] withPassword:@"@ABC123" error:&error];
if (error)
NSLog(@"Failed: %@", error);
exit(1);
NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return string;
- (void)createPlistIfNotExists
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:@"eplist.plist"];
NSFileManager* fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path])
path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"eplist.plist"]];
if ([fileManager fileExistsAtPath:path])
plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
else
// If the file doesn’t exist, create an empty dictionary
plistDict = [[NSMutableDictionary alloc] init];
- (NSString*)getPlistPath
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:@"eplist.plist"];
return path;
@end
【讨论】:
如何给出路径?我的代码正确吗?我在解密的文件中没有得到任何东西 根据行,NSData *data = [@"Data" dataUsingEncoding:NSUTF8StringEncoding];它的编码词@“数据”,我如何加密我的整个 plist 文件。我的 plist 文件名为 data.plist。 iam 传递字符串来编码假设我已经使用@"Data" 字符串进行编码。 好的。如何加密整个 data.plist ?是的,我正在开发 ios以上是关于无法加密和解密 plist 中的数据的主要内容,如果未能解决你的问题,请参考以下文章