crypto-js,加密使用

1安装  "crypto-js": "^4.1.1",

2页面引入使用

import CryptoJS from 'crypto-js'
//第二种只用crypto-js和base64
// 使用AesUtil.genAesKey()生成,需和后端配置保持一致
const aesKey = "123456789";

// 使用DesUtil.genDesKey()生成,需和后端配置保持一致
const desKey = "123456789";

/**
 * aes 加密方法,同java:AesUtil.encryptToBase64(text, aesKey);
 */
export const encryptAES = (data, key) => {
  if (!key) {
    key = aesKey;
  }
  const dataBytes = CryptoJS.enc.Utf8.parse(data);
  const keyBytes = CryptoJS.enc.Utf8.parse(key);
  const encrypted = CryptoJS.AES.encrypt(dataBytes, keyBytes, {
    iv: keyBytes,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
}

/**
 * aes 解密方法,同java:AesUtil.decryptFormBase64ToString(encrypt, aesKey);
 */
export const decryptAES = (data, key) => {
  if (!key) {
    key = aesKey;
  }
  const keyBy

你可能感兴趣的:(前端静态页面以及逻辑,javascript)