I am trying to use RNCryptor to do AES256 encryption at iOS side and AES256 decryption at remote site in PHP. But I am not able to get the correct decrypted data with php. Please help review if anything I am doing wrong.
Below is my code for iOS.
NSString *key = @"1234567890123456789012";
NSData *encryptedData = [RNEncryptor encryptData:data
withSettings:kRNCryptorAES256Settings
password:key
error:&error];
Then I post the encryptedData to server which is in PHP. Below is my PHP code.
$key ="1234567890123456789012" //32-bit key
$username = aes256Decrypt ($key, $username);
function aes256Decrypt($key, $data) {
if(32 !== strlen($key)) $key = hash('SHA256', $key, true);
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_CBC,
str_repeat("\0", 16));
$padding = ord($data[strlen($data) - 1]);
$result = substr($data, 0, -$padding);
return substr($data, 0, -$padding);
}