参考:AES-256-CBC Mcrypt-PHP decrypt and Crypto-JS Encrypt
我尝试使用Shurik提供的代码来获得一个有效的示例,但无法使其工作。
感谢您的帮助。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Encrypt with javascript and decrypt using php</title>
<meta name="generator" content="BBEdit 10.5" />
<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>
function encrypt_me(obj)
{
// alert(obj.input_value.value);
var input_value = document.getElementById("input_value").value;
// alert(input_value);
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(input_value+"_Messsage", 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);
document.getElementById("data_base64").value = data_base64;
document.getElementById("iv_base64").value = iv_base64;
document.getElementById("key_base64").value = key_base64;
document.getElementById("encrypted_value").value = encrypted;
}
</script>
</head>
<body>
<?php
$mode = 'text'; // choices {"text", "hidden"}
if(isset($_POST['submit']))
{
echo('<pre>'.print_r($_POST, true).'</pre>');
$input_value = $_POST['input_value'];
$encrypted_value = $_POST['encrypted_value'];
$decrypted_value = $_POST['decrypted_value'];
$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
$decrypted_value = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_128, $key, $encrypted_value, MCRYPT_MODE_CBC, $iv ), "\t\0 " );
echo ("\$decrypted_value : ".$decrypted_value."<br />");
}
else
{
$input_value = "";
$encrypted_value = "";
$decrypted_value = "";
$encrypted = "";
$iv = "";
$key = "";
$plaintext = "";
}
?>
<form action="sample_encrypt.php" method="post" name="main_form" id="main_form">
input_value : <input type="text" id="input_value" name="input_value" value="<?=$input_value?>" size="100"><br />
encrypted_value : <input type="text" id="encrypted_value" name="encrypted_value" value="<?=$encrypted_value?>" size="100"><br />
decrypted_value : <input type="text" id="decrypted_value" name="decrypted_value" value="<?=$decrypted_value?>" size="100"><br />
<br />
data_base64 : <input type="<?=$mode?>" id="data_base64" name="data_base64" value="<?=$encrypted?>" size="100"><br />
iv_base64 : <input type="<?=$mode?>" id="iv_base64" name="iv_base64" value="<?=$iv?>" size="100"><br />
key_base64 : <input type="<?=$mode?>" id="key_base64" name="key_base64" value="<?=$key?>" size="100"><br />
<input type="button" id="no_submit" name="no_submit" value="Encrypt" onclick="encrypt_me(this.form);">
<br />
<input type="submit" id="submit" name="submit" value="Decrypt">
</form>
</body>
</html>
答案 0 :(得分:1)
你需要改变三件事来使其发挥作用。
首先,由于您使用的是128位AES,因此您的密钥必须为128位:
var key256Bits500Iterations = CryptoJS.PBKDF2("Secret Passphrase", salt, { keySize: 128/32, iterations: 500 });
注意,我将keySize从256/32更改为128/32。
接下来,您需要从输入中获取加密,iv和密钥:
$encrypted = base64_decode($_POST["data_base64"]); // data_base64 from JS
$iv = base64_decode($_POST["iv_base64"]); // iv_base64 from JS
$key = base64_decode($_POST["key_base64"]); // key_base64 from JS
注意,我在字符串周围添加了$ _POST [...]。
最后,您应该对加密值进行base64解码。
$encrypted_value = base64_decode($_POST['encrypted_value']);
完整查看代码here。