//所需依赖
"crypto-js": "^4.1.1",
//引入 CryptoJS
import CryptoJS from 'crypto-js'
//加密
encrypt(word, keyStr) {
keyStr = keyStr ? keyStr : ""; //判断是否存在key,不存在就用定义好的key
var key = CryptoJS.enc.Utf8.parse(keyStr);
var srcs = CryptoJS.enc.Utf8.parse(word);
var encrypted = CryptoJS.AES.encrypt(srcs, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
},
//解密
decrypt(word,key){
var key = CryptoJS.enc.Utf8.parse(key);
var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
},
前端aes加密及解密方法
最新推荐文章于 2024-06-19 16:43:28 发布