I am trying to store encrypted data in localStorage and decrypt it when needed by using crypto-js This is the encryption function:
const passphrase = CryptoJS.enc.Utf8.parse('key');
const iv = CryptoJS.enc.Utf8.parse('key');
return CryptoJS.AES.encrypt(string, passphrase, { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7}).toString();
And the decryption function is:
const passphrase = CryptoJS.enc.Utf8.parse('key');
const iv = CryptoJS.enc.Utf8.parse('key');
return CryptoJS.AES.decrypt(string, passphrase, { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7}).toString(CryptoJS.enc.Utf8)
But I get an empty string when decrypting. I have tried changing iv, padding, keysize and mode but i get different strings each time I run encryption in that case. I need encryption to always return the same result
Can somebody tell me what am I doing wrong please?