Tag Archives: decryption

NodeJS AES256 Encryption/Decryption

In one of my projects I needed to encrypt some data. I decided to use AES256 symmetrical encryption algorithm.

So this is what I’ve come to (aes_encryption.js):

var crypto	= require('crypto'),
    cipher_seed = 'some_random_characters';
var encrypt = function(text) {
	var cipher	= crypto.createCipher('aes-256-cbc', cipher_seed),
	    crypted	= cipher.update(text, 'utf8', 'hex');

	crypted += cipher.final('hex');

	return crypted;
var decrypt = function(text) {
	var decipher  = crypto.createDecipher('aes-256-cbc', cipher_seed),
	    decrypted = decipher.update(text, 'hex', 'utf8');

	decrypted += decipher.final('utf8');

	return decrypted;
module.exports.encrypt = encrypt;
module.exports.decrypt = decrypt;


var AES = require('path_to_aes_encryption.js');

var data = {
    name:    'Catalin',
    surname: 'Munteanu',
    address: 'Romania'

// Encryption
var encrypted_data = AES.encrypt(JSON.stringify(data));

// Decryption
var decrypted_data = JSON.parse(AES.decrypt(encrypted_data));

Don’t forget that if the cipher seed is changed or lost all the encrypted data is also lost.

Easy, right?