Encrypting data in php on server side and decrypting in iOS fails.
On server in php it looks like this(just for test):
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, "a16byteslongkey!a16byteslongkey!", "iphone", MCRYPT_MODE_CBC, $iv);
$base64encoded_ciphertext = base64_encode($ciphertext);
return $base64encoded_ciphertext;
In iOS:
NSData *decrypted = [[RNCryptor AES256Cryptor] decryptData:[QSStrings decodeBase64WithString: text] password:@"a16byteslongkey!a16byteslongkey!" error:&error];
NSLog(@"errro - %@", [error description]);
NSString *decryptedString = [[[NSString alloc] initWithData: decrypted encoding: NSUTF8StringEncoding] autorelease];
Error description is:
Error Domain=net.robnapier.RNCryptManager Code=2 "Unknown header" UserInfo=0x8582e10 {NSLocalizedDescription=Unknown header}
Using RNCryptor: https://github.com/rnapier/RNCryptor
in encryptor that is the place where error is thrown:
if (![header isEqualToData:[NSData dataWithBytes:AES128CryptorHeader length:sizeof(AES128CryptorHeader)]]) {
*error = [NSError errorWithDomain:kRNCryptorErrorDomain code:kRNCryptorUnknownHeader
userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"Unknown header", @"Unknown header") forKey:NSLocalizedDescriptionKey]];
return NO;
}
What is wrong whith this?
Just reading the site for RNCryptor, I believe it is a full message input output encryptor. If you look at the data format on their wiki
https://github.com/rnapier/RNCryptor/wiki/Data-Format
I believe that they EXPECT that your data to ENCRYPT does NOT have a header, but if you are DECRYPTING, then it expects that the data was encrypted using their encryptor, which adds header and hmac.
If I’m correct, the data that your server side puts together is simply the cipher-text portion of the full message that RNCryptor expects to see!!!