JS:
引入:
<script src="https://cdn.bootcss.com/crypto-js/3.1.9-1/crypto-js.min.js"></script> <script src="https://cdn.bootcss.com/crypto-js/3.1.9-1/aes.min.js"></script> <script src="https://cdn.bootcss.com/zepto/1.1.5/zepto.min.js"></script>
加密字符串:
<script> var str="test";//加密字符串 key = CryptoJS.enc.Utf8.parse("1111111111111111");// 秘钥 var iv= CryptoJS.enc.Utf8.parse('1111111111111111');//向量iv var encrypted = CryptoJS.AES.encrypt(str, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding }); console.log(encrypted.toString()); </script>
PHP:
加密:
$privateKey="1111111111111111"; $iv="1111111111111111"; $data='test'; $encrypted=@mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$privateKey,$data,MCRYPT_MODE_CBC,$iv); //encrypted=openssl_encrypt($data,"AES-128-CBC",$privateKey,true,$iv); echo base64_encode($encrypted);
注:php5版本可使用mcrypt_encrypt函数,php7需换成openssl_decrypt函数