import CryptoJs from 'crypto-js';

const AES_KEY = 'DataMaleon123456';

/**
 * aes加密
 */
export const encryptAES = (word: string) => {
  const key = CryptoJs.enc.Utf8.parse(AES_KEY);
  const srcs = CryptoJs.enc.Utf8.parse(word);
  const encrypted = CryptoJs.AES.encrypt(srcs, key, {
    mode: CryptoJs.mode.ECB,
    padding: CryptoJs.pad.Pkcs7,
  });
  return encrypted.toString();
};

/**
 * aes解密
 */
export const decryptAES = (word: string) => {
  const key = CryptoJs.enc.Utf8.parse(AES_KEY);
  const decrypt = CryptoJs.AES.decrypt(word, key, {
    mode: CryptoJs.mode.ECB,
    padding: CryptoJs.pad.Pkcs7,
  });
  return CryptoJs.enc.Utf8.stringify(decrypt).toString();
};

/**
 * base64加密
 */
export const encryptBase64 = (word: string) => {
  const wordArray = CryptoJs.enc.Utf8.parse(word);
  const base64 = CryptoJs.enc.Base64.stringify(wordArray);
  return base64;
};

/**
 * base64解密
 */
export const decryptBase64 = (base64: string) => {
  const parsedWordArray = CryptoJs.enc.Base64.parse(base64);
  const parsedStr = parsedWordArray.toString(CryptoJs.enc.Utf8);
  return parsedStr;
};

发表评论