couple of small errors in your script
- you didn't define the output of the encryption, hence it would be a buffer
- you did add an extra parameter in the decipher update (that's the error it's complaining about above)
- you didn't define the decipher final
The following script is the working version
const crypto = require('crypto');
// Encryption
function encrypt(plainText, encryptionKey, iv) {
const cipher = crypto.createCipheriv('aes128-cbc', encryptionKey, iv);
let encrypted = cipher.update(plainText);
return cipher.final('hex');;
}
// Decryption
function decrypt(encryptedText, encryptionKey, iv) {
console.error('Insidie Decrypt:', encryptedText );
console.error('Insidie Decrypt iv:', iv );
var decipher = crypto.createDecipheriv('aes128-cbc', encryptionKey, iv);
decipher.update(encryptedText,'hex');
var originalPlainText = decipher.final('utf8');
return originalPlainText;
}
// Example usage
const encryptionKey = Buffer.from('0123456789abcdef0123456789abcdef','hex');
const iv = Buffer.from(crypto.randomBytes(16),'hex');
const plainText = 'This is a test message';
const encryptedText = encrypt(plainText, encryptionKey, iv);
console.error('Encrypted text:', encryptedText);
const decryptedText = decrypt(encryptedText, encryptionKey, iv);
console.error('Decrypted text:', decryptedText);
------------------------------
Tom van Oppens
------------------------------