npm install crypto-js
js文件
import CryptoJS from 'crypto-js/crypto-js' let initKey = 'metagross-zhihuihouqin-shanxixueyu-400-151-6766'; //加密 export function Encrypt(word) { let plainText = initKey.length > 32 ? initKey.substr(0, 32) : initKey; // 密钥支持前32位 let srcs = CryptoJS.enc.Utf8.parse(word); let key = CryptoJS.enc.Utf8.parse(plainText); let encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString().toUpperCase(); } //解密 export function decrypt(word) { let encryptedHexStr = CryptoJS.enc.Hex.parse(word); let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); let plainText = initKey.length > 32 ? initKey.substr(0, 32) : initKey; // 密钥支持前32位 let key = CryptoJS.enc.Utf8.parse(plainText); let decrypt = CryptoJS.AES.decrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8); return decryptedStr.toString(); }
页面引用
比如:
this.setCookie("password", Encrypt(this.loginForm.password),3650);
this.loginForm.password = decrypt(this.getCookie("password"));