RNCryptor Encrypt in iOS and Decrypt in PHP

1.9k views Asked by At

I've been trying to use RNCryptor to encrypt a string in iOS and have the app post the encrypted string to a server which will decrypt the string in PHP.

Everything appears to be working fine (no error messages) until the end when the PHP script returns an empty string.

I think the problem is in the iOS code because when I tried to decrypt the string in the example decrypt.php, it worked fine.

iOS:

NSString *key = @"myPassword";
NSString *string = @"Secret String";
NSData *plain = [string dataUsingEncoding:NSUTF8StringEncoding];
NSData *cipherData = [RNEncryptor encryptData:plain withSettings:kRNCryptorAES256Settings password:key error:&error];
NSString *cipherString = [[NSString alloc] initWithData:[cipherData base64EncodedDataWithOptions:0] encoding:NSUTF8StringEncoding];

Then I post the cipherString to the following PHP script

PHP:

require 'autoload.php';
$password = "myPassword";
$base64Encrypted = $_POST['data'];
$cryptor = new \RNCryptor\Decryptor();
$plaintext = $cryptor->decrypt($base64Encrypted, $password);
echo $plaintext;

All help is appreciated. Thank you.

Edit: I learned from this discussion that when I entered the Base64 from the cipherString directly to the PHP, without the POST, it worked perfectly. Any thoughts?

1

There are 1 answers

0
Arzoo On

replace

$cryptor = new \RNCryptor\Decryptor();

by

$cryptor = new \RNCryptor\RNCryptor\Decryptor;