import CryptoJS from "crypto-js"; const key = "weKANfYbTTqZCve0"; // 密钥,自行修改 export function aesEncrypt(plaintext: string) { try { const keyBytes = CryptoJS.enc.Utf8.parse(key); const encrypted = CryptoJS.AES.encrypt( CryptoJS.enc.Utf8.parse(plaintext), keyBytes, { mode: CryptoJS.mode.ECB, //aes加密模式EcB 前后端使用统一的加密模式 padding: CryptoJS.pad.Pkcs7, //使用Pkcs7的方式填充 }, ); const hexStr = CryptoJS.enc.Hex.parse(encrypted.ciphertext.toString()); return CryptoJS.enc.Base64.stringify(hexStr); } catch (e) { // console.log("===aesEncrypt ", e); } return plaintext; } export function aesDecrypt(secrecyStr: string | CryptoJS.lib.CipherParams) { try { const keyBytes = CryptoJS.enc.Utf8.parse(key); const decryptedBytes = CryptoJS.AES.decrypt(secrecyStr, keyBytes, { mode: CryptoJS.mode.ECB, // 前后端一致的加解密模式 padding: CryptoJS.pad.Pkcs7, // 前后端一致的填充方式 }); return decryptedBytes.toString(CryptoJS.enc.Utf8); } catch (e) { return secrecyStr; } }