在PHP加密與mcrypt的用PHP加密解密後AES使用Javascript CryptoJS mcrypt的
<?php
$string = 'Secret Message';
$key = 'd4b494e4502a62edd695a903a94c2701';
$iv = '02f30dffbb0d084755f438f7d8be4a7d';
$encrypted = base64_encode(
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256,
$key,
$string,
MCRYPT_MODE_CBC,
$iv
)
);
//$encrypted results in 'nYoFAiyDARVSI09lH/IPdim5TvE51izVjk6sc2AK9Rg='
?>
在Javascript解密與CryptoJS
<script>
var encrypted = 'nYoFAiyDARVSI09lH/IPdim5TvE51izVjk6sc2AK9Rg=';
var key = CryptoJS.enc.Hex.parse('d4b494e4502a62edd695a903a94c2701');
var iv = CryptoJS.enc.Hex.parse('02f30dffbb0d084755f438f7d8be4a7d');
var decrypted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.Pkcs7});
console.log(decrypted.toString(CryptoJS.enc.Utf8)); //prints an empty string
</script>
我無法弄清楚如何讓Javascript一邊吐出原文。
MCRYPT_RIJNDAEL_256是* not * AES; MCRYPT_RIJNDAEL_128是。 – 2014-10-29 11:25:27
謝謝,但是有沒有辦法在JavaScript中解密256位塊的RIJNDAEL密碼? – 2014-10-29 23:14:33
當然,找到一個Rijndael JavaScript實現。 – 2014-10-30 00:07:41