import CryptoJS from "crypto-js";

const key = 'ab7c603643a9e1223250ba1802bfa09b'
const iv = 'ef87dbc437335809'

/**
 * 加密
 * @param text
 * @returns {string}
 */
export const encrypt = (text) => {
  const keyHex = CryptoJS.enc.Hex.parse(key)
  const ivHex = CryptoJS.enc.Hex.parse(iv)

  const encrypted = CryptoJS.AES.encrypt(
    text,
    keyHex,
    {
      iv: ivHex,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    }
  )
  return encrypted.toString()
}

/**
 * 解密
 * @param text
 * @returns {string}
 */
export const decrypt = (text) => {
  const keyHex = CryptoJS.enc.Hex.parse(key)
  const ivHex = CryptoJS.enc.Hex.parse(iv)

  const decrypted = CryptoJS.AES.decrypt(
    text,
    keyHex,
    {
      iv: ivHex,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    }
  )
  return decrypted.toString(CryptoJS.enc.Utf8)
}
