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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.