广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-04 63b4909ac5d0b7355be211cc7080673b41cdb3cc
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
import CryptoJS from 'crypto-js';
 
export class Crypto<T extends object> {
  /** Secret */
  secret: string;
 
  constructor(secret: string) {
    this.secret = secret;
  }
 
  encrypt(data: T): string {
    const dataString = JSON.stringify(data);
    const encrypted = CryptoJS.AES.encrypt(dataString, this.secret);
    return encrypted.toString();
  }
 
  decrypt(encrypted: string) {
    const decrypted = CryptoJS.AES.decrypt(encrypted, this.secret);
    const dataString = decrypted.toString(CryptoJS.enc.Utf8);
    try {
      return JSON.parse(dataString) as T;
    } catch {
      // avoid parse error
      return null;
    }
  }
}