Search code examples
androidiosencryptionrncryptorjncryptor

RNCryptor/JNCryptor - decrypt string in ios which is encrypted in android


Hellp All,

I have used RNCryptor/JNCryptor for iOS and android following is the code for encrypt in Android and Decrypt in iOS.

Encrypt code in Android

public static String EncryptMessage(String message)
{
JNCryptor cryptor = new AES256JNCryptor();
byte[] plaintext = message.getBytes();
byte[] ciphertext = null;

try {
ciphertext = cryptor.encryptData(plaintext,Constants.ENCRYPTION_KEY.toCharArray());
} catch (CryptorException e) {
// Something went wrong
e.printStackTrace();
}

return  Base64.encodeBytes(ciphertext);
}

Decrypt code in iOS

 NSData *fromAData =     [@"AwEche05IIj/OYEN5brgF/p6jyfKfARJliUQwEwa2lko40+ScPMumlDSIZWB2O6XRZjIucXVbggieWaHb2x6sJkYYt7tn3k17V5Jd5PAnkIoFA==" base64DecodedData];

NSData *fromADecryptedData = [RNDecryptor decryptData:fromAData withSettings:kRNCryptorAES256Settings password:@"password" error:&decryptionError];
NSLog(@"decryptionError %@", decryptionError);
NSLog(@"Result = %@", fromPHPDecryptedData);
NSLog(@"Result = %@", [[NSString alloc] initWithData:fromADecryptedData encoding:NSUTF8StringEncoding]);

I have also tried simple decrypt with password

 NSData *fromADecryptedData = [RNDecryptor decryptData:fromAData withPassword:@"password" error:&decryptionError];

Getting the error of unknown header, error code=2

I am using same sting "password" as password at both the side.

If anyone has any idea please let me know if i am missing anything.


Solution

  • It worked once I updated my RNcryptor library in my iOS...

    My iOS RNcryptor library version is v2.2

    Android Version 1.2.0