import CryptoJS from 'crypto-js'; class AesUtil { keySize: number; iterationCount: number; constructor(keySize: number, iterationCount: number) { this.keySize = keySize / 32; this.iterationCount = iterationCount; } generateKey(salt: string, passPhrase: string): CryptoJS.lib.WordArray { // Use SHA1 as hasher to match Java's PBKDF2WithHmacSHA1 default const key = CryptoJS.PBKDF2(passPhrase, CryptoJS.enc.Hex.parse(salt), { keySize: this.keySize, iterations: this.iterationCount, hasher: CryptoJS.algo.SHA1 }); return key; } encrypt(salt: string, iv: string, passPhrase: string, plainText: string): string { const key = this.generateKey(salt, passPhrase); const encrypted = CryptoJS.AES.encrypt(plainText, key, { iv: CryptoJS.enc.Hex.parse(iv), mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return encrypted.ciphertext.toString(CryptoJS.enc.Base64); } decrypt(salt: string, iv: string, passPhrase: string, cipherText: string): string { const key = this.generateKey(salt, passPhrase); const cipherParams = CryptoJS.lib.CipherParams.create({ ciphertext: CryptoJS.enc.Base64.parse(cipherText) }); const decrypted = CryptoJS.AES.decrypt(cipherParams, key, { iv: CryptoJS.enc.Hex.parse(iv), mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return decrypted.toString(CryptoJS.enc.Utf8); } } export { AesUtil };