// 引入crypto-js包
import CryptoJS from 'crypto-js'




/**
 * AES加密
 * @param {any} word 加密数据
 */
export const cryptoEncrypt = (word) => {
  var key = CryptoJS.enc.Utf8.parse(config.AES_KEY);
  var iv = CryptoJS.enc.Utf8.parse(config.AES_IV);
  var encrypted = '';
  var srcs = ''
  if (typeof(word) == 'string') {
      srcs = word
  } else if (typeof(word) == 'object') {//对象格式的转成json字符串
      srcs = CryptoJS.enc.Utf8.parse(word);
  }
  encrypted = CryptoJS.AES.encrypt(srcs, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
  })
  return encrypted.ciphertext.toString();
}

/**
 * AES解密
 * @param {any} word 解密数据
 */
export const cryptoDecrypt = (word) => {
  var key = CryptoJS.enc.Utf8.parse(config.AES_KEY);
  var iv = CryptoJS.enc.Utf8.parse(config.AES_IV);
  var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
  var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  var decrypt = CryptoJS.AES.decrypt(srcs, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
  });
  var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr.toString();
}

Max迪丶先生
1.8k 声望64 粉丝

引用和评论

0 条评论