1

I am using following js code to encrypt string

var text = 'should be decrypted!';
var key = 'HighlySecretKeyForJsEncryption!!';
var encrypted = CryptoJS.AES.encrypt(text, key);
console.log(encrypted.toString());

output : U2FsdGVkX19vf+s6/+eB8A+3iKFCl1A0e+oe0BSbcMVGxb64FL35Q3CB/LZNu4ng

and this what I did in php to decrypt this

function decrypt($toDecrypt) {
    $key = "HighlySecretKeyForJsEncryption!!";
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $toDecrypt = base64_decode($toDecrypt);
    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, substr($toDecrypt, $iv_size), MCRYPT_MODE_CBC, substr($toDecrypt, 0, $iv_size)));
}

But this is not working, it gives me garbage string.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Pranav
  • 174
  • 1
  • 8

1 Answers1

-1

From docs:

var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");

CryptoJS supports AES-128, AES-192, and AES-256. It will pick the variant by the size of the key you pass in. If you use a passphrase, then it will generate a 256-bit key.

You probabily need to pass the constant MCRYPT_RIJNDAEL_256 when decrypting php-side

More about AES encryption / decrytption in php: https://stackoverflow.com/a/3422787/4499267

Community
  • 1
  • 1
Phate01
  • 2,499
  • 2
  • 30
  • 55
  • Using MCRYPT_RIJNDAEL_256 no luck! – Pranav May 30 '16 at 11:30
  • CryptoJS only supports AES, but `RIJNDAEL_256` is not AES anymore. Only `RIJNDAEL_128` is compatible with AES. – Artjom B. May 30 '16 at 14:09
  • Please notice that the [wiki page](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard) says that The Advanced Encryption Standard (AES) is also known as Rijndael – Phate01 May 31 '16 at 07:28