Nodejs snippet: AES encryption


nodejs_logo

Example: AES encryption with mode CBC and Initialization vector

const crypto = require('crypto');
const algorithm = 'aes-128-cbc';
const key = '1234567890123456';
const iv = '1234567890123456'; // Initialization vector.
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update('some clear text data', 'utf8', 'hex');
encrypted += cipher.final('hex');
console.log(encrypted);

Result: beb5163efc9bed08506917e83c878af9d81afa571a83f55ecd9a57475ac987e7

Online demo: https://runkit.com/embed/7erc71c8ge8o

Leave a Reply