const CRYPTOJSKEY = '7LrjnqHv2mS5Qr5s'
import CryptoJS from 'crypto-js'
export function encrypt(plaintText) {
  var plaintText = plaintText
  var options = {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  }
  var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY)
  var encryptedData = CryptoJS.AES.encrypt(plaintText, key, options)
  var encryptedBase64Str = encryptedData.toString().replace(/\//g, '_')
  encryptedBase64Str = encryptedBase64Str.replace(/\+/g, '-')
  return encryptedBase64Str
}
export function decrypt(encryptedText) {
  // 还原 URL 安全字符
  let encryptedBase64Str = encryptedText
    .replace(/_/g, '/') // 还原 _
    .replace(/-/g, '+') // 还原 -
  // 补充末尾的 =（如果需要）
  const mod4 = encryptedBase64Str.length % 4
  if (mod4) {
    encryptedBase64Str += '='.repeat(4 - mod4)
  }
  try {
    const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY)
    const decrypt = CryptoJS.AES.decrypt(encryptedBase64Str, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })
    return CryptoJS.enc.Utf8.stringify(decrypt).toString()
  } catch (e) {
    console.error('解密失败:', e)
    return null
  }
}
