ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { decrypt, encrypt } from 'crypto-js/aes';
import Base64 from 'crypto-js/enc-base64';
import Hex from 'crypto-js/enc-hex';
import UTF8, { parse } from 'crypto-js/enc-utf8';
import md5 from 'crypto-js/md5';
import ECB from 'crypto-js/mode-ecb';
import pkcs7 from 'crypto-js/pad-pkcs7';
 
export interface EncryptionParams {
  iv: string;
  key: string;
  useHex: boolean;
}
 
const defaultKey = encryptByMd5((import.meta as any).env.VITE_CIPHER_KEY);
 
export class AesEncryption {
  private iv;
  private key;
  private useHex = true;
 
  constructor(opt: Partial<EncryptionParams> = {}) {
    const { iv, key = defaultKey, useHex = true } = opt;
    if (key) this.key = parse(key);
    if (iv) this.iv = parse(iv);
    this.useHex = !!useHex;
  }
 
  decryptByAES(cipherText: string) {
    const realCipherText = this.useHex
      ? Base64.stringify(Hex.parse(cipherText))
      : cipherText;
    return decrypt(
      realCipherText,
      this.key as CryptoJS.lib.WordArray,
      this.getOptions(),
    ).toString(UTF8);
  }
 
  encryptByAES(cipherText: string) {
    const result = encrypt(
      cipherText,
      this.key as CryptoJS.lib.WordArray,
      this.getOptions(),
    ).toString();
    return this.useHex ? Hex.stringify(Base64.parse(result)) : result;
  }
 
  private getOptions() {
    return {
      iv: this.iv,
      mode: ECB,
      padding: pkcs7,
    };
  }
}
 
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();
}