javascript AES-256-CBC Mcrypt-PHP 解密和 Crypto-JS 加密
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21180721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AES-256-CBC Mcrypt-PHP decrypt and Crypto-JS Encrypt
提问by Paul
I am trying to encrypt in Javascript with CryptoJS and decrypt in PHP. The JS code is:
我正在尝试使用 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);
And the PHP is as follows:
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);
This does not return the correct answer.
这不会返回正确答案。
I am unsure where things are going poorly! I need to do my own IV, but if I do just say:
我不确定哪里事情进展不顺利!我需要做我自己的 IV,但如果我这样做只是说:
CryptoJS.AES.Encrypt("Message", "Secret Passphrase");
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);
It DOES successfully work in the PHP code -- only the key_base64 isn't something that can be changed, it has to be what the user remembers... And then it gives me a salt to get a key from the passphrase entered and IDK how it managed to get that using CryptoJS
它确实在 PHP 代码中成功运行——只有 key_base64 不是可以改变的东西,它必须是用户记得的......然后它给了我一个盐来从输入的密码短语和 IDK 中获取密钥它是如何使用 CryptoJS 做到的
回答by xzag
Your code WILL work if you just fix a few typos there
如果您在那里修复一些错别字,您的代码将起作用
JS
JS
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/pbkdf2.js"></script>
<script>
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
var encrypted = CryptoJS.AES.encrypt("Message", key256Bits500Iterations, { iv: iv });
var data_base64 = encrypted.ciphertext.toString(CryptoJS.enc.Base64);
var iv_base64 = encrypted.iv.toString(CryptoJS.enc.Base64);
var key_base64 = encrypted.key.toString(CryptoJS.enc.Base64);
</script>
PHP
PHP
<?php
$encrypted = base64_decode("data_base64"); // data_base64 from JS
$iv = base64_decode("iv_base64"); // iv_base64 from JS
$key = base64_decode("key_base64"); // key_base64 from JS
$plaintext = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv ), "\t##代码## " );