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

时间:2015-03-05 06:02:19

标签: ios encryption plist rncryptor

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

1 个答案:

答案 0 :(得分: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