const EncAndDec = { AesKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgnvJ0Xw7etrzacM4fIZIY6o', /** * AES加密 * @param {*} word 需要加解密的文本 * @param {*} key 加解密的秘钥 * iv: 偏移量,最短8位数,ECB模式不需要此参数 * @returns */ encryptData(data, key = this.AesKey) { // 将数据转换为 WordArray const dataWA = CryptoJS.enc.Utf8.parse(data); // 将密钥转换为 WordArray const keyWA = CryptoJS.enc.Utf8.parse(key); // 生成随机 IV(初始化向量) const iv = CryptoJS.lib.WordArray.random(16); // 加密 const encrypted = CryptoJS.AES.encrypt(dataWA, keyWA, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); // 将 IV 和密文组合在一起 const combined = iv.concat(encrypted.ciphertext); // 将组合后的数据转换为 Base64 字符串 return CryptoJS.enc.Base64.stringify(combined); }, /** * AES解密 * @param string word * @param string key * @returns string */ Decrypt(word = '', key = this.AesKey) { if (word.length == 0) return null let decodeBase64Str = atob(word) let signData = decodeBase64Str.slice(16); let encrypted = atob(signData); encrypted = JSON.parse(encrypted); const iv = CryptoJS.enc.Base64.parse(encrypted.iv); const value = encrypted.value; key = CryptoJS.enc.Base64.parse(key); var decrypted = CryptoJS.AES.decrypt(value, key, { iv: iv }); decrypted = decrypted.toString(CryptoJS.enc.Utf8); return decrypted; }, decryptData(encryptedData, key = this.AesKey) { // 将 Base64 编码的字符串转换为 WordArray const encryptedDataWA = CryptoJS.enc.Base64.parse(encryptedData); // 提取 IV(初始化向量)。CryptoJS WordArray 对象的 sigBytes 属性表示字节数 const iv = CryptoJS.lib.WordArray.create(encryptedDataWA.words.slice(0, 4), 16); // 提取密文 const ciphertext = CryptoJS.lib.WordArray.create(encryptedDataWA.words.slice(4), encryptedDataWA.sigBytes - 16); // 转换密钥为 WordArray const keyWA = CryptoJS.enc.Utf8.parse(key); // 解密 const decrypted = CryptoJS.AES.decrypt({ ciphertext: ciphertext }, keyWA, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); // 将解密结果转换为 UTF8 字符串 return decrypted.toString(CryptoJS.enc.Utf8); }, /** * 图片解密 * @param string strImgBase64 * @param object GetData * @returns string */ DecryptImg(strImgBase64, GetData) { let strBase64Content = `data:image/png;base64,${strImgBase64}`; let strImgId = "#" + GetData.id + "" let strImgUrl = $(strImgId).attr('data-original') if (strImgUrl == GetData.imgurl) { $(strImgId).attr('src', strBase64Content) $(strImgId).removeClass("jqlazyload"); setImgHeightFun(strImgId) } return strBase64Content }, }