gpt4 book ai didi

ios - 无法加密和解密plist中的数据

转载 作者:行者123 更新时间:2023-11-29 02:14:47 27 4
gpt4 key购买 nike

我正在尝试使用 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];

最佳答案

使用 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

关于ios - 无法加密和解密plist中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28871065/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com