- benben0729
- 参考文章
在文件根目录下
npm install crypto-js
引入以下这个文件
- crypto-js/crypto-js.js
#js端
function secret(string, code, operation) {
code = CryptoJS.MD5('contentDocuments').toString();
code2 = CryptoJS.MD5('contentWindowHig').toString();
var iv = CryptoJS.enc.Utf8.parse(code.substring(0, 16));
var key = CryptoJS.enc.Utf8.parse(code2.substring(0,16));
if (operation) {
return CryptoJS.AES.decrypt(string, key, {iv: iv, padding: CryptoJS.pad.Pkcs7}).toString(CryptoJS.enc.Utf8);
}
return CryptoJS.AES.encrypt(string, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString();
}
# php端
public static function secret($string,$code,$operation=false){
$code = md5('contentDocuments');
$code2 = md5('contentWindowHig');
$iv = substr($code,0,16);
$key = substr($code2,0,16);
if($operation){
return openssl_decrypt(base64_decode($string),"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv);
}
return base64_encode(openssl_encrypt($string,"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv));
}