采用SHA256,AES加密

LinMeng 等级 385 1 0
采用SHA256,AES加密
// import引用AES源码js
import CryptoJS from "crypto-js/crypto-js";
import md5 from 'js-md5';
console.log(process.env);
var lightKey;
if (process.env.NODE_ENV == "production") {
  lightKey = "ing88";
} else {
  lightKey = "123456";
}
console.log(lightKey);
// 真正的,经过SHA256PRNG编码的AES密钥
const darkKey = CryptoJS.enc.Hex.parse(
  CryptoJS.SHA256(CryptoJS.SHA256(lightKey))
    .toString()
    .substring(0, 32)
);
console.log(darkKey);
/**
 * 解密方法,入参:待解密的字符串
 */
export function decrypt(word) {
  // 直接针对word进行AES解密
  let decrypted = CryptoJS.AES.decrypt(word, darkKey, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  // 对解码后的decrypted进行UTF-8转码
  return decrypted.toString(CryptoJS.enc.Utf8);
}
/**
 * 加密方法,入参:待加密的字符串
 */
export function encrypt(word) {
  // 先对word进行UTF-8转码至二进制
  let data = CryptoJS.enc.Utf8.parse(word);
  // 对二进制word进行AES加密
  let encrypted = CryptoJS.AES.encrypt(data, darkKey, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  // 二进制encrypted进行Base64转码
  return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}
收藏
评论区