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

**URL:** <https://forum.ionicframework.com/t/not-able-to-decrypt-aes-256-in-server-side-after-encrypted-in-ionic5/242583>\
**Category:** Ionic Framework\
**Created:** [June 21, 2024, 12:42pm UTC](https://forum.ionicframework.com/t/not-able-to-decrypt-aes-256-in-server-side-after-encrypted-in-ionic5/242583 "2024-06-21T12:42:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eden-coder3](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/eden-coder3/32/147558_2.png) [@eden-coder3](https://forum.ionicframework.com/u/eden-coder3)\
**Post date:** [June 21, 2024, 12:42pm UTC](https://forum.ionicframework.com/t/not-able-to-decrypt-aes-256-in-server-side-after-encrypted-in-ionic5/242583/1 "2024-06-21T12:42:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [June 21, 2024, 12:56pm UTC](https://forum.ionicframework.com/t/not-able-to-decrypt-aes-256-in-server-side-after-encrypted-in-ionic5/242583/2 "2024-06-21T12:56:21Z")

</div>

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](https://ionic.io/blog/a-new-chapter-for-ionic-native). You should update your packages.

---

<div class="post-metadata">

**Author:** ![eden-coder3](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/eden-coder3/32/147558_2.png) [@eden-coder3](https://forum.ionicframework.com/u/eden-coder3)\
**Post date:** [June 21, 2024, 1:22pm UTC](https://forum.ionicframework.com/t/not-able-to-decrypt-aes-256-in-server-side-after-encrypted-in-ionic5/242583/3 "2024-06-21T13:22:48Z")

</div>

ionic:

```auto
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 :

```auto
$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](http://www.devglan.com/online-tools/aes-encryption-decryption)

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [June 21, 2024, 2:39pm UTC](https://forum.ionicframework.com/t/not-able-to-decrypt-aes-256-in-server-side-after-encrypted-in-ionic5/242583/4 "2024-06-21T14:39:05Z")

</div>

Please use proper [code blocks](https://www.markdownguide.org/extended-syntax/#fenced-code-blocks) 🙂

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](https://github.com/Ideas2IT/cordova-aes256/blob/a38fc06064b2f6ae2a02551910c9f44efad87088/src/android/AES256.java).

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

```php
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);

```

---

<div class="post-metadata">

**Author:** ![eden-coder3](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/eden-coder3/32/147558_2.png) [@eden-coder3](https://forum.ionicframework.com/u/eden-coder3)\
**Post date:** [June 22, 2024, 6:50am UTC](https://forum.ionicframework.com/t/not-able-to-decrypt-aes-256-in-server-side-after-encrypted-in-ionic5/242583/5 "2024-06-22T06:50:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [June 24, 2024, 1:23pm UTC](https://forum.ionicframework.com/t/not-able-to-decrypt-aes-256-in-server-side-after-encrypted-in-ionic5/242583/6 "2024-06-24T13:23:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eden-coder3](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/eden-coder3/32/147558_2.png) [@eden-coder3](https://forum.ionicframework.com/u/eden-coder3)\
**Post date:** [June 25, 2024, 10:48am UTC](https://forum.ionicframework.com/t/not-able-to-decrypt-aes-256-in-server-side-after-encrypted-in-ionic5/242583/7 "2024-06-25T10:48:40Z")

</div>

> [@twestrick](#):
>
> ```auto
> $pbkdf2SecuredKey = openssl_pbkdf2(
> $secureKey,
> PBKDF2_SALT,
> PBKDF2_KEY_LENGTH,
> PBKDF2_ITERATION_COUNT
> );
> 
> ```

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.
