开头问候语:PHP 是世界上最好的语言,可能将来会是宇宙中最好的语言,如果你想学习更多PHP知识, 可以博主更多文章,了解更多PHP专业知识。
npm install crypto-js
GitHub项目地址:https://github.com/brix/crypto-js 官方有文档介绍!
参考文章:https://www.cnblogs.com/phpxuetang/p/5656266.html
注 :单个用法只需要引入src目录中的所需加密js到项目中即可使用。
// 单个用法实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CryptoJS</title>
<script src="__INDEX_JS__/src/core.js"></script>
<script src="__INDEX_JS__/src/md5.js"></script>
<script src="__INDEX_JS__/src/evpkdf.js"></script>
<script src="__INDEX_JS__/src/enc-base64.js"></script>
<script src="__INDEX_JS__/src/cipher-core.js"></script>
<script src="__INDEX_JS__/src/aes.js"></script>
<script src="__INDEX_JS__/src/hmac.js"></script>
<script src="__INDEX_JS__/src/sha1.js"></script>
<script src="__INDEX_JS__/src/sha256.js"></script>
</head>
<body>
<div id="content"></div>
<script>
var aes = CryptoJS.AES("Message").toString(CryptoJS.enc.Hex);
console.log("aes = %s", aes);
var md5 = CryptoJS.MD5("Message").toString(CryptoJS.enc.Hex);
console.log("md5 = %s", md5);
var sHA1 = CryptoJS.SHA1("Message").toString(CryptoJS.enc.Hex);
console.log("sHA1 = %s", sHA1);
var sHA256 = CryptoJS.SHA256("Message").toString(CryptoJS.enc.Hex);
console.log("sHA256 = %s", sHA256);
var hmacMD5 = CryptoJS.HmacMD5("Message", "Secret Passphrase").toString(CryptoJS.enc.Hex);
console.log("hmacMD5 = %s", hmacMD5);
var hmacSHA1 = CryptoJS.HmacSHA1("Message", "Secret Passphrase").toString(CryptoJS.enc.Hex);
console.log("hmacSHA1 = %s", hmacSHA1);
var aesEncrypt = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
console.log("aesEncrypt = %s", aesEncrypt.iv.toString(CryptoJS.enc.Hex));
var aesDecrypt = CryptoJS.AES.decrypt(aesEncrypt, "Secret Passphrase");
console.log("aesDecrypt = %s", aesDecrypt.toString(CryptoJS.enc.Utf8));
// base64 encrypt
var rawStr = "hello world!";
var wordArray = CryptoJS.enc.Utf8.parse(rawStr);
var base64 = CryptoJS.enc.Base64.stringify(wordArray);
console.log('base64Encrypt = ', base64);
// base64 decrypt
var parsedWordArray = CryptoJS.enc.Base64.parse(base64);
var parsedStr = parsedWordArray.toString(CryptoJS.enc.Utf8);
console.log('base64Decrypt = ',parsedStr);
</script>
</body>
</html>
JS :本方法可加密、解密,前端js/后端php相互交互等。
// 引入JS文件
<script src="__INDEX_JS__/src/core.js"></script>
<script src="__INDEX_JS__/src/enc-base64.js"></script>
<script src="__INDEX_JS__/src/cipher-core.js"></script>
<script src="__INDEX_JS__/src/aes.js"></script>
<script src="__INDEX_JS__/src/md5.js"></script>
// string 要加密/解密的字符串
// code 秘钥字符串
// operation 默认false表示加密,传入true表示解密
function secret(string, code, operation) {
code = CryptoJS.MD5(code).toString();
var iv = CryptoJS.enc.Utf8.parse(code.substring(0,16));
var key = CryptoJS.enc.Utf8.parse(code.substring(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 :本方法可加密、解密,前端js/后端php相互交互等。
/*
* $string 要加密/解密的字符串
* $code 秘钥字符串
* $operation 默认false表示加密,传入true表示解密
*/
public function secret($string, $code, $operation = false)
{
$code = md5($code);
$iv = substr($code, 0, 16);
$key = substr($code, 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));
}