Search code examples
phpjavascriptaesmcryptrijndael

Decrypt PHP AES/CBC encrypted string in JavaScript


I'm getting literally crazy trying to decrypt in JavaScript a string encrypted in PHP with the following code snippet:

function myencrypt($str) {
    $iv = '1234567890abcdef';
    $key = 'abcdef1234567890';

    $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
    mcrypt_generic_init($td, $key, $iv);
    $encrypted = mcrypt_generic($td, $str);

    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);

    return bin2hex($encrypted);
}

I've tried slowAES, gibberish-aes and some other available JS libraries, but always failed to correctly decrypt a single string.

I've also extensively searched in SO, but was unable to find a solution applicable to my situation.

Any pointer toward the right solution would be more than welcome!

Thanks, Alessandro


Solution

  • have you tried the crypto-js library? Sorry but i'm not sure what mcrypt_module_open('rijndael-128' does exactly but if it just uses CBC it should work