Skip to content

Instantly share code, notes, and snippets.

@eliOcs
Created July 19, 2012 09:11
Show Gist options
  • Save eliOcs/3142385 to your computer and use it in GitHub Desktop.
Save eliOcs/3142385 to your computer and use it in GitHub Desktop.
Cipher plain text with Node.js
var crypto = require("crypto"); // native module that allows ciphering
var CIPHER_KEY = "test-key", CIPHER_ALGORITHM = "aes256";
// Original message
var message = "This message will be ciphered";
console.log("Original message -> " + message);
// Cipher message
var cipher = crypto.createCipher(CIPHER_ALGORITHM, CIPHER_KEY);
var cipheredMessage = cipher.update(message, "binary", "hex");
cipheredMessage += cipher.final("hex");
console.log("Ciphered message -> " + cipheredMessage);
// Decipher message
var decipher = crypto.createDecipher(CIPHER_ALGORITHM, CIPHER_KEY);
var decipheredMessage = decipher.update(cipheredMessage, "hex", "binary");
decipheredMessage += decipher.final("binary");
console.log("Deciphered message -> " + decipheredMessage);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment