Last active
January 1, 2016 07:29
-
-
Save aloncarmel/8111927 to your computer and use it in GitHub Desktop.
Encrypt and Decrypt text on nodejs with callbacks.
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
//Encrypt data, call callback when done for make sure everything is done. | |
function EncryptText(text,key,callback) { | |
console.log('Encrypting text'); | |
var cipher = crypto.createCipher('aes-256-cbc',key) | |
var crypted = cipher.update(text,'utf8','hex') | |
crypted += cipher.final('hex'); | |
callback(crypted); | |
} | |
//Decrypt data, call callback when done for make sure everything is done. | |
function DecryptText(text,key,callback) { | |
console.log('Decrypting text'); | |
var decipher = crypto.createDecipher('aes-256-cbc',key) | |
var dec = decipher.update(text,'hex','utf8') | |
dec += decipher.final('utf8'); | |
callback(dec); | |
} | |
//Calling the encrypt with callback because it may hang before finishing resulting in bad encryption. | |
EncryptText(text,key,function(crypted){ | |
console.log(crypted); | |
}); | |
//Calling the Decrypt with callback because it may hang before finishing resulting in bad encryption. | |
DecryptText(text,key,function(dec){ | |
console.log(dec); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment