Created
June 11, 2016 16:23
-
-
Save riston/419668483d523ba68cf85787ebf52924 to your computer and use it in GitHub Desktop.
Encrypt-decrypt AES256 Node.js with crypto module example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crypto = require("crypto"); | |
function encrypt(key, data) { | |
var ciph = crypto.createCipher("aes256", key); | |
ciph.end(data); | |
var encBuffer = ciph.read(); | |
// Convert to base64 | |
return encBuffer.toString("base64"); | |
} | |
function decrypt(key, data) { | |
var ciph = crypto.createDecipher("aes256", key); | |
var encBuffer = new Buffer(data, "base64"); | |
ciph.end(encBuffer); | |
return ciph.read().toString("utf8"); | |
} | |
var enc = encrypt("kass", "hernesupp"); | |
var dec = decrypt("kass", enc); | |
console.log(enc, dec); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment