I'm trying to encrypt the saved plist data using RNCryptor and decrypt it. the output is all gibberish in encrypted file but am not able to get the anything after decryption.
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];
Here is the code for decryption
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];
Complete Example with code using RNEncryptor and 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