js部分
js使用的是crypto-js进行aes加密
github地址:https://github.com/brix/crypto-js
加密
1
2
3
4
5
| var md5SecterKey = 'a1d3eaf653ab8d9c2c1d809f13ab5f05';
var key = crypto.enc.Utf8.parse(md5SecterKey);
var iv = crypto.enc.Utf8.parse(md5SecterKey);
var ciphertext = crypto.AES.encrypt(data, key, { iv: iv,mode:crypto.mode.CBC,padding:crypto.pad.ZeroPadding});
console.log(ciphertext.toString());
|
解密
1
2
3
4
5
6
7
8
| var key = crypto.enc.Utf8.parse(md5SecterKey);
var iv = crypto.enc.Utf8.parse(md5SecterKey);
var ciphertext = localStorage.getItem(name);
if(ciphertext){
var bytes = crypto.AES.decrypt(ciphertext.toString(), key, { iv: iv,mode:crypto.mode.CBC,padding:crypto.pad.ZeroPadding});
var plaintext = bytes.toString(crypto.enc.Utf8);
return plaintext;
}
|
php部分
php使用自带的aes解密
加密
1
2
3
4
5
| $pk = "a1d3eaf653ab8d9c2c1d809f13ab5f05";
$iv = substr($pk, 0, 16);
$t = 'reatang';
$encrypted = ( mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $pk, $t, MCRYPT_MODE_CBC,$iv) );
$base64Str = base64_encode($encrypted).'<br>';
|
解密
1
2
3
4
5
| $str_encrypt = base64_decode($base64Str);
$pk = "a1d3eaf653ab8d9c2c1d809f13ab5f05";
$iv = substr($pk, 0, 16);
echo mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $pk, base64_decode($str_encrypt), MCRYPT_MODE_CBC, $iv);
exit;
|