Skip to content

Instantly share code, notes, and snippets.

@aksel
Forked from vlucas/encryption.js
Last active March 19, 2018 19:50
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 aksel/d934ec2af805dc99b3b40e1b3a02ebbd to your computer and use it in GitHub Desktop.
Save aksel/d934ec2af805dc99b3b40e1b3a02ebbd to your computer and use it in GitHub Desktop.
Stronger Encryption and Decryption in Node.js
'use strict';
const crypto = require('crypto');
const KEY_LENGTH = 32; // Must be 32 bytes
const IV_LENGTH = 16; // For AES, this is always 16
// Creates 32 byte key (for AES-256), buffer
const createKey = () => crypto.randomBytes(KEY_LENGTH);
// Creates 16 byte iv, buffer
const createIv = () => crypto.randomBytes(IV_LENGTH);
// Encrypts given text string, using AES-256-CBC. Returns encrypted message as hex string.
const encrypt = (key, iv, text) => {
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
const encrypted = cipher.update(text);
return Buffer.concat([encrypted, cipher.final()]).toString('hex');
};
// Decrypts given text string. Presumes encryptedText to be hex string.
const decrypt = (key, iv, encryptedText) => {
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
const decrypted = decipher.update(new Buffer(encryptedText, 'hex'));
return Buffer.concat([decrypted, decipher.final()]).toString();
};
module.exports = { createKey, createIv, decrypt, encrypt };
@aksel
Copy link
Author

aksel commented Sep 27, 2017

Parameterized key and iv.
Added functions for creating random keys and ivs, using crypto.randomBytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment