Airagale Airagale - 3 months ago 16
iOS Question

RNCryptor "encryptData" returning null

New to iOS dev, trying to work on encoding data for secure data storage in db.

I found the current example here:

This is my code.

NSString * aPassword =@"tempkey";

NSData *data = [@"Data" dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSData *encryptedData = [RNEncryptor encryptData:data

NSLog(@"Data: %@", [[NSString alloc] initWithData:encryptedData encoding:NSUTF8StringEncoding]);

My log

2016-10-20 11:41:52.662 BlueBoard[57245:10027277] Data: (null)

Am I missing a step in this process? I've confirmed that it's null because it the db its being stored as null as well.


Your issue isn't that encryptedData is nil, it's that you are attempted to create an NSString from data that doesn't represent a string.

If you wish to convert encryptedData into a string for storage or other purposes, you should convert the data into a Base 64 encoded representation. Do this with the base64EncodedStringWithOptions: method.

NSString *base64String = [encryptedData base64EncodedStringWithOptions:0];

Of course when you want to decrypt the string later, you will need to convert the Base 64 encoded string back into NSData, and then decrypt that data.