-
-
Save lgh06/aa0121b17e332a65477b154b99a21ee8 to your computer and use it in GitHub Desktop.
Simple String Encryption & Decryption with Node.js
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 fs = require('fs'), | |
crypto = require('crypto'); | |
var key = '14189dc35ae35e75ff31d7502e245cd9bc7803838fbfd5c773cdcd79b8a28bbd', | |
cipher = crypto.createCipher('aes-256-cbc', key); | |
input = fs.createReadStream('test.txt'), | |
output = fs.createWriteStream('test.txt.enc'); | |
input.pipe(cipher).pipe(output); | |
output.on('finish', function() { | |
console.log('Encrypted file written to disk!'); | |
}); |
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
function encrypt(text){ | |
var cipher = crypto.createCipher('aes-256-cbc','d6F3Efeq') | |
var crypted = cipher.update(text,'utf8','hex') | |
crypted += cipher.final('hex'); | |
return crypted; | |
} | |
function decrypt(text){ | |
var decipher = crypto.createDecipher('aes-256-cbc','d6F3Efeq') | |
var dec = decipher.update(text,'hex','utf8') | |
dec += decipher.final('utf8'); | |
return dec; | |
} | |
var hw = encrypt("hello world") | |
decrypt(hw) | |
// feel free to change >> d6F3Efeq | |
// To test just copy + paste the above inside the node shell | |
// TIP: always encrypt IDs before sending via HTTP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment