安装
npm install crypto-js --save-dev
加密
/** * AES加密 * @param {Object} word 待加密的数据 * @param {Object} keyStr 密钥 */ encrypt (word,keyStr) { let key = CryptoJS.enc.Utf8.parse(keyStr) let srcs = CryptoJS.enc.Utf8.parse(JSON.stringify(word)) let encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) return encrypted.ciphertext.toString(); }
解密
/**
* AES解密 * @param {Object} word 待解密的数据 * @param {Object} keyStr 密钥 */ decrypt (word,keyStr) { var key = CryptoJS.enc.Utf8.parse(keyStr) var encryptedHexStr = CryptoJS.enc.Hex.parse(word); var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr); var decrypt = CryptoJS.AES.decrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) return decrypt.toString(CryptoJS.enc.Utf8).toString(); }