Not able to decrypt AES-256 in server side after encrypted in IONIC5

I am ecryptinga text in IONIC using @ionic-native/aes-256/ngx library and trying to decrypt the same in PHP but unable to do so.

Here’s the data : Key : 12345678901234567890123456789012 (32bit) IV : 1234567890123456 Plain Text : ABCD

Encrypted in IONIC : "W2/PbR8z0sKBblfd4ezO1DKI/7UrB/egwDEGyw7w9tY= "

But it’s decrypting in IONIC but not in PHP. In PHP i tried encryption of the same text using same keys but also I am getting different encrypted data. PHP encrypted data for string “ABCD” : “5JvCGZhhM12hA9Tz/ldBvw==”

Can somebody please explain if I am missing anything?

Encrypted data in IONIC and PHP are different. I want to decrypt the text in PHP which was done using IONIC

Please show your code. How are you encrypting/decrypting in Ionic? How are you encrypting/decrypting in PHP?

EDIT
Also as a side note, Ionic Native is dead (since 2021) - A New Chapter for @ionic-native - Ionic Blog. You should update your packages.

ionic:

import { AES256 } from '@ionic-native/aes-256/ngx';

constructor(public aes:AES256){}

encrypt(key, iv, data){
    return this.aes.encrypt(key, iv, data);
}

output : "W2/PbR8z0sKBblfd4ezO1DKI/7UrB/egwDEGyw7w9tY= "

PHP :

$iv = "1234567890123456";
$key = "12345678901234567890123456789012";
$encrypted = "W2/PbR8z0sKBblfd4ezO1DKI/7UrB/egwDEGyw7w9tY= ";

$decrypted = openssl_decrypt($encrypted, "AES-256-CBC", $key, 0, $iv);

echo $decrypted;

output : error

I tried encrypting using same keys in php and getting different ecnrypted data as : “5JvCGZhhM12hA9Tz/ldBvw==” also same in AES Encryption and Decryption Online

Please use proper code blocks :slight_smile:

Are you testing on Android or iOS? There is more going on than just passing in the key and iv. A PBKDF2 is being created first. See the Android source here.

I tried replicating the Android encryption in PHP, but still missing something (at least not getting the same encrypted value as you shared).

const PBKDF2_ITERATION_COUNT = 1001;
const PBKDF2_KEY_LENGTH = 256;
const PBKDF2_SALT = 'hY0wTq6xwc6ni01G';

$secureKey = '12345678901234567890123456789012';
$iv = '1234567890123456';

$pbkdf2SecuredKey = openssl_pbkdf2(
    $secureKey,
    PBKDF2_SALT,
    PBKDF2_KEY_LENGTH,
    PBKDF2_ITERATION_COUNT
);

// $pbkdf2SecuredKey = base64_encode(bin2hex($pbkdf2SecuredKey));

$encrypted = openssl_encrypt('ABCD', 'aes-256-cbc', $pbkdf2SecuredKey, 0, $iv);

// base64_encode($encrypted);
1 Like

I am testing in Android. But if I decrypt the same in ionic it works perfect. While passing to PHP its not decrypting. Is there anything to do with key and iv or encrypted data?

I am pretty sure it has to do with the logic in PHP. It needs to do the same as the Capacitor plugin is doing. Hopefully someone with cryptography expertise can chime in.

Thank you so much. The above code-block works perfect for Android. We need to do android style key derivation in PHP and then decrypt using that specific key.

1 Like