aesUtil.ts 916 Bytes
import CryptoJS from 'crypto-js'

const KEY = 'abcdefgabcdefg64'
const IV = 'abcdefgabcdefg64'

export class AESUtil {
  private static KEY = CryptoJS.enc.Utf8.parse(KEY)
  private static IV = CryptoJS.enc.Utf8.parse(IV)

  static decrypt(data: string): string {
    try {
      // 将Base64字符串转换为CipherParams对象
      const ciphertext = CryptoJS.enc.Base64.parse(data)
      const cipherParams = CryptoJS.lib.CipherParams.create({
        ciphertext: ciphertext
      })

      // 解密
      const decrypted = CryptoJS.AES.decrypt(cipherParams, this.KEY, {
        iv: this.IV,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.NoPadding
      })

      // 转换为字符串并去除填充
      return decrypted.toString(CryptoJS.enc.Utf8).replace(/\x00+$/, '')
    } catch (error) {
      console.error('Decryption failed:', error)
      throw new Error('解密失败')
    }
  }
}