monorepo-microservice-rbac/apps/services/dmp/archive/src/app.util.ts
2023-08-31 17:11:14 +08:00

22 lines
510 B
TypeScript

import * as CryptoJS from 'crypto-js';
import { parse } from 'flatted';
export class SymmetricCrypto {
private key: string;
constructor(key: string) {
this.key = key;
}
decrypt(encryptedData: string): object | null {
try {
const bytes = CryptoJS.AES.decrypt(encryptedData, this.key);
const decryptedData = bytes.toString(CryptoJS.enc.Utf8);
return parse(decryptedData);
} catch (error) {
console.error('Decryption failed:', error);
return null;
}
}
}