Skip to content

Instantly share code, notes, and snippets.

@goeroeku
Forked from siwalikm/aes-256-cbc.js
Created April 12, 2021 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goeroeku/defc2f4dd67f2fe31f4bcd746b758d78 to your computer and use it in GitHub Desktop.
Save goeroeku/defc2f4dd67f2fe31f4bcd746b758d78 to your computer and use it in GitHub Desktop.
AES-256-CBC implementation in nodeJS with built-in Crypto library
'use strict';
const crypto = require('crypto');
const ENC_KEY = "bf3c199c2470cb477d907b1e0917c17b"; // set random encryption key
const IV = "5183666c72eec9e4"; // set random initialisation vector
// ENC_KEY and IV can be generated as crypto.randomBytes(32).toString('hex');
const phrase = "who let the dogs out";
var encrypt = ((val) => {
let cipher = crypto.createCipheriv('aes-256-cbc', ENC_KEY, IV);
let encrypted = cipher.update(val, 'utf8', 'base64');
encrypted += cipher.final('base64');
return encrypted;
});
var decrypt = ((encrypted) => {
let decipher = crypto.createDecipheriv('aes-256-cbc', ENC_KEY, IV);
let decrypted = decipher.update(encrypted, 'base64', 'utf8');
return (decrypted + decipher.final('utf8'));
});
encrypted_key = encrypt(phrase);
original_phrase = decrypt(encrypted_key);
// star this gist if you found it useful
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment