import CryptoJS from 'crypto-js'

// 定义固定的密钥和IV
const KEY = CryptoJS.enc.Utf8.parse("abcdefgabcdefg64")
const IV = CryptoJS.enc.Utf8.parse('abcdefgabcdefg64')

export function encrypt(word: string, keyStr?: string, ivStr?: string): string {
  let key = KEY
  let iv = IV

  if (keyStr) {
    key = CryptoJS.enc.Utf8.parse(keyStr)
    iv = CryptoJS.enc.Utf8.parse(ivStr!)
  }

  const srcs = CryptoJS.enc.Utf8.parse(word)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding
  })

  return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
}

// export const decrypt = (word: string, keyStr?: string, ivStr?: string): string =>{
//   let key = CryptoJS.enc.Utf8.parse(KEY)
//   let iv = CryptoJS.enc.Utf8.parse(IV)

//   if (keyStr) {
//     key = CryptoJS.enc.Utf8.parse(keyStr)
//     iv = CryptoJS.enc.Utf8.parse(ivStr!)
//   }

//   const base64 = CryptoJS.enc.Base64.parse(word)
//   const src = CryptoJS.enc.Base64.stringify(base64)

//   const decrypt = CryptoJS.AES.decrypt(src, key, {
//     iv: iv,
//     mode: CryptoJS.mode.CBC,
//     padding: CryptoJS.pad.ZeroPadding
//   })

//   const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
//   return decryptedStr.toString()
// }