AES加密处理

AES加密处理

vue中先安装然后引入

import Crypto from 'crypto-js'
import AES from 'crypto-js/aes'
// aesSecret为秘钥
/**
 * aes 加密
 * @param data 加密前数据
 * @return 加密后数据
 * */
const encryptAes = (data) => {
  let aesSecret =aesSecret;
  let key = Crypto.enc.Utf8.parse(aesSecret),
    iv = Crypto.enc.Utf8.parse(aesSecret)
  if (typeof data === 'object') {
    data = JSON.stringify(data);
  } else {
    data = '' + data;
  }
  let ret = AES.encrypt(data, key, {
    iv: iv,
    mode: Crypto.mode.CBC,
    padding: Crypto.pad.Pkcs7
  })
  return Crypto.enc.Base64.stringify(ret.ciphertext)
}

/**
 * aes 解密
 * @param data 解密前数据
 * @return 解密后数据
 * */
export const decodeAes = (data) => {
  let aesSecret = aesSecret,
    cipherTextHexStr = Crypto.enc.Utf8.parse(data),
    cipherTextBase64Str = Crypto.enc.Utf8.stringify(cipherTextHexStr),
    key = Crypto.enc.Utf8.parse(aesSecret),
    iv = Crypto.enc.Utf8.parse(aesSecret)
  let oriStr = Crypto.AES.decrypt(cipherTextBase64Str, key, {
    iv: iv,
    mode: Crypto.mode.CBC,
    padding: Crypto.pad.Pkcs7
  });

  let decrypted = oriStr.toString(Crypto.enc.Utf8)
  let ret;
  try {
    ret = JSON.parse(decrypted)
  } catch (e) {
    ret = decrypted
  }
  return ret
}

你可能感兴趣的:(node.js,vue)