import { encrypt, decrypt } from 'crypto-js/aes'; import UTF8, { parse } from 'crypto-js/enc-utf8'; import pkcs7 from 'crypto-js/pad-pkcs7'; import ECB from 'crypto-js/mode-ecb'; import md5 from 'crypto-js/md5'; import Base64 from 'crypto-js/enc-base64'; import {mode, pad, lib} from 'crypto-js' export interface EncryptionParams { key: string; iv: string; } export interface CustomCipherParams { mode?: typeof mode.ECB; padding?: typeof pad.Pkcs7; iv?: lib.WordArray; } export class AesEncryption { private key; private iv: lib.WordArray; constructor(opt: Partial = {}) { const { key, iv } = opt; if (key) { this.key = parse(key); } this.iv = iv ? parse(iv) : lib.WordArray.create([]); } get getOptions(): CustomCipherParams { return { mode: ECB, padding: pkcs7, iv: this.iv, }; } encryptByAES(cipherText: string) { return encrypt(cipherText, this.key, this.getOptions).toString(); } decryptByAES(cipherText: string) { return decrypt(cipherText, this.key, this.getOptions).toString(UTF8); } } export function encryptByBase64(cipherText: string) { return UTF8.parse(cipherText).toString(Base64); } export function decodeByBase64(cipherText: string) { return Base64.parse(cipherText).toString(UTF8); } export function encryptByMd5(password: string) { return md5(password).toString(); }