[JS]AES加密(基于crypto-js)PHP后端解密

我的程序员人生 2015-07-28

这里主要讲 前端使用crypto-js AES加密后,php解密。

前端js

<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/aes.js"></script> 
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script> 
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/pad-zeropadding.js"></script> 
<script> 
    var key_hash = CryptoJS.MD5("Message"); 
    var key = CryptoJS.enc.Utf8.parse(key_hash); 
    var iv  = CryptoJS.enc.Utf8.parse('1234567812345678'); 
    var encrypted = CryptoJS.AES.encrypt("Message", key, { iv: iv,mode:CryptoJS.mode.CBC,padding:CryptoJS.pad.ZeroPadding}); 
    document.write("encode:"+encrypted); 
</script>

php代码

<?php 
$text = "Message"; 
$key = md5($text);  //CuPlayer.com提示key的长度必须16,32位,这里直接MD5一个长度为32位的key 
$iv='1234567812345678'; 
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv); 
$decode = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $crypttext, MCRYPT_MODE_CBC, $iv); 
echo base64_encode($crypttext); 
echo "<br/>"; 
echo $decode; 
echo "<br/>"; 
?>

相关推荐