import cryptoJs from 'crypto-js'
import Md5 from 'js-md5'
const CBCKEY = Md5('AcquisitHouseInformation')
const CBCIV = CBCKEY.slice(0, 8)

// DES加密 -- CBC模式
export const encryptDes = (message) => {
  let keyHex = cryptoJs.enc.Utf8.parse(CBCKEY)
  let ivHex = cryptoJs.enc.Utf8.parse(CBCIV)
  let option = { iv: ivHex, mode: cryptoJs.mode.CBC, padding: cryptoJs.pad.Pkcs7 }
  let encrypted = cryptoJs.DES.encrypt(message, keyHex, option)
  return encrypted.ciphertext.toString()
}

// DES解密 -- CBC模式
export const decryptDes = (message) => {
  let keyHex = cryptoJs.enc.Utf8.parse(CBCKEY)
  let ivHex = cryptoJs.enc.Utf8.parse(CBCIV)
  let decrypted = cryptoJs.DES.decrypt(
    {
      ciphertext: cryptoJs.enc.Hex.parse(message)
    },
    keyHex,
    {
      iv: ivHex,
      mode: cryptoJs.mode.CBC,
      padding: cryptoJs.pad.Pkcs7
    }
  )
  return decrypted.toString(cryptoJs.enc.Utf8)
}
