php
$data = 'ABCDEFGH';
$key = "1234567898882222";
$iv = '8NONwyJtHesysWpM';
// 加密$encode = base64_encode(openssl_encrypt($data,"AES-128-CBC",$key,true,$iv));
// 解密$decode = openssl_decrypt(base64_decode($encode),"AES-128-CBC",$key,true,$iv);
JS
<script src="https://cdn.bootcdn.net/ajax/libs/crypto-js/4.1.1/crypto-js.js"></script>
<script type="text/javascript">
var IV = '8NONwyJtHesysWpM';
var KEY = '1234567898882222';
var data = 'ABCDEFGH';
// 加密
function encrypt(str) {
var key = CryptoJS.enc.Utf8.parse(KEY);
var iv = CryptoJS.enc.Utf8.parse(IV);
var encrypted = CryptoJS.AES.encrypt(str, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
return encrypted.toString();
}
// 解密
function decrypt(str) {
var key = CryptoJS.enc.Utf8.parse(KEY);
var iv = CryptoJS.enc.Utf8.parse(IV);
var decrypted = CryptoJS.AES.decrypt(str, key, { iv: iv, padding: CryptoJS.pad.Pkcs7 });
return decrypted.toString(CryptoJS.enc.Utf8);
}
var encode = encrypt(data);
var decode = decrypt(encode);
console.log(JSON.stringify({ "encode": encode, "decode": decode, "iv": IV, "key": KEY, "data": data }));
</script>