monorepo-microservice-rbac/apps/services/logger/src/app.util.ts

22 lines
510 B
TypeScript
Raw Normal View History

2023-08-27 14:37:59 +08:00
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;
}
}
}