我正在尝试使用CryptoJS在Javascript中加密并在PHP中解密.JS代码是:
var salt = CryptoJS.lib.WordArray.random(128/8);
var key256Bits500Iterations = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 256/32, iterations: 500 });
var iv = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f'); // just chosen for an example, usually random as well
encrypted = CryptoJS.AES.encrypt("Message", key512Bits1000Iterations, { iv: iv });
var data_base64 = crypted.ciphertext.toString(CryptoJS.enc.Base64);
var iv_base64 = crypted.iv.toString(CryptoJS.enc.Base64);
var key_base64 = crypted.key.toString(CryptoJS.enc.Base64);
Run Code Online (Sandbox Code Playgroud)
PHP如下:
$encrypted = base64_decode($data_base64);
$iv = base64_decode($iv_base64);
$key = base64_decode($key_base64);
$plaintext = mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv);
echo ($plaintext);
Run Code Online (Sandbox Code Playgroud)
这不会返回正确答案.
我不确定哪里情况不好!我需要做自己的IV,但如果我只是说:
CryptoJS.AES.Encrypt("Message", "Secret Passphrase");
var data_base64 = crypted.ciphertext.toString(CryptoJS.enc.Base64); …Run Code Online (Sandbox Code Playgroud)