Skip to content

Instantly share code, notes, and snippets.

@William-Yeh
Last active August 29, 2015 14:16
Show Gist options
  • Save William-Yeh/caa3b1acbbc91f5a308b to your computer and use it in GitHub Desktop.
Save William-Yeh/caa3b1acbbc91f5a308b to your computer and use it in GitHub Desktop.
Node.js mcrypt example in CentOS 6.6
/**
* simple demo for mcrypt in Node.js
* adapted from PHP example: http://php.net/manual/en/function.mcrypt-encrypt.php
*/
var mcrypt = require('mcrypt');
var CIPHER_ALGO = 'rijndael-256';
var cipher = new mcrypt.MCrypt(CIPHER_ALGO, 'cbc');
var key = new Buffer(
"bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3"
, 'hex');
var iv = cipher.generateIv();
var iv_size = iv.length;
console.log('Cipher:', CIPHER_ALGO);
console.log('Key size:', key.length);
console.log('Key:', key.toString('hex'));
console.log('IV: ', iv.toString('hex'));
console.log("---");
// display plaintext
var plaintext = 'This string was AES-256 / CBC / ZeroBytePadding encrypted.';
console.log('Before encryption:', plaintext);
// --- ENCRYPTION ---
cipher.open(key, iv);
var ciphertext = cipher.encrypt(plaintext);
var combined = Buffer.concat([iv, ciphertext]);
var ciphertext_base64 = combined.toString('base64');
console.log('After encryption: ', ciphertext_base64);
// --- DECRYPTION ---
var ciphertext_binary = new Buffer(ciphertext_base64, 'base64');
var iv_dec = new Buffer(iv_size);
var ciphertext_dec = new Buffer(ciphertext_binary.length - iv_size);
ciphertext_binary.copy(iv_dec, 0, 0, iv_size);
ciphertext_binary.copy(ciphertext_dec, 0, iv_size);
cipher.open(key, iv_dec);
var plaintext_dec = cipher.decrypt(ciphertext_dec);
console.log('After decryption: ', plaintext_dec.toString());
Vagrant.configure(2) do |config|
config.vm.box = "chef/centos-6.6"
config.vm.provision "shell", inline: <<-SHELL
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum -y install libmcrypt-devel nodejs npm
npm install mcrypt
# vagrant ssh ; cd /vagrant
# node example.js
SHELL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment