I'm trying to use the following funciton to encrypt a Base64 encoded string using the AES-256-CFB algorithm, and when I try to encode the result back to Base64 it casues the following error: Uncaught TypeError: r.clamp is not a function
function (base64EncodedString, password) {
let iv = CryptoJS.lib.WordArray.random(256 / 16);
let salt = CryptoJS.enc.Hex.parse(iv.toString(CryptoJS.enc.Hex).substr(0, 16));
let key = CryptoJS.EvpKDF(password, CryptoJS.enc.Hex.parse(salt), { keySize: 256 / 32 });
let nonEncryptedString = CryptoJS.enc.Base64.parse(base64EncodedString);
let cipher = CryptoJS.lib.CipherParams.create({ ciphertext: nonEncryptedString , iv: iv, salt: salt, mode: CryptoJS.mode.CFB, padding: CryptoJS.pad.NoPadding,});
let encrypted = CryptoJS.AES.encrypt(cipher, key, {mode: CryptoJS.mode.CFB, padding: CryptoJS.pad.NoPadding, iv: iv});
let ecrytpedBase64 = CryptoJS.enc.Base64.stringify(encrypted);
return ecrytpedBase64;
};
This is happening because you're passing the encrypted variable (of type CipherParams) to the stringify function, which expects a WordArray (your iv variable).
CryptoJS.enc.Base64.stringify(encrypted); // TypeError: r.clamp is not a function
CryptoJS.enc.Base64.stringify(iv); // Ok