iosobjective-cencryptionrncryptor

RNCryptor: Encrypt with rncrypt (cli) then decrypt in iOS using RNDecryptor


When I use the terminal version of RNCryptor to encrypt a file and then try to decrypt this file in Objective-C using RNDecryptor, I always get the error "unknown header".

I understand this is because my first byte is 'A' instead of '2' or the kRNCryptorFileVersion, but I don't know why this is.

If I decrypt the file using the terminal version, it works like expected.

I encrypt the file using: ./rncrypt -p someKey "$(cat test.txt)" > encr.txt

This gives output like: AwHcVbXbpyI7S/RBXlVhRP1coKqFmSEFDtgFaj/JGJ181qEb024uVdt7lHWqUvUvm1rwdM4yQQ+gsMepHhR58v054qvhO4yu98N2bHGuV28aUA==

To decrypt it in iOS I'm doing the following:

NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"encr" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:resourcePath];

NSError *error;
NSData *uncrypted = [RNDecryptor decryptData:data withPassword:@"someKey" error:&error];

What am I doing wrong?


Solution

  • The rncrypt test program outputs base64 encoded data. You need to decode it before passing it to the decryptor. See [NSData initWithBase64EncodedData:options:].

    Note that if kRNCryptorFileVersion is 2, you're using a slightly broken version of RNCryptor that has poor security for multibyte passwords (Chinese for example). I highly recommend using a later version with the v3 format.