Created
October 5, 2016 18:56
-
-
Save 7flash/bb8d4c3c5faf05f4aad29ad9ad0292a4 to your computer and use it in GitHub Desktop.
aes-128
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'); | |
var secret = crypto.randomBytes(24); | |
function encrypt(plaintext) { | |
var cipher = crypto.createCipher('aes-128-cbc', secret); | |
cipher.setAutoPadding(false); | |
var ciphertext = ''; | |
for (var i=0; i < plaintext.length; i+=16) { | |
ciphertext += cipher.update(plaintext.substr(i, i+16), 'utf8', 'base64'); | |
} | |
return ciphertext.toString('base64'); | |
} | |
function decrypt(ciphertext) { | |
var decipher = crypto.createDecipher('aes-128-cbc', secret); | |
decipher.setAutoPadding(false); | |
var plaintext = decipher.update(ciphertext, 'base64', 'utf8'); | |
return plaintext.toString('utf8'); | |
} | |
var ciphertext = encrypt(new Buffer("The secret crow ate the pie of the bear.").toString('utf8')); | |
console.log("Cipher text is: " + ciphertext); | |
console.log("Plain text is: " + decrypt(ciphertext)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment