-
-
Save candh/9cf42a83dc7fc7a07c8d459b270af245 to your computer and use it in GitHub Desktop.
Example of encryption and decryption in 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 crypto = require("crypto") | |
function encrypt(key, data) { | |
var cipher = crypto.createCipher('aes-256-cbc', key); | |
var crypted = cipher.update(data, 'utf-8', 'hex'); | |
crypted += cipher.final('hex'); | |
return crypted; | |
} | |
function decrypt(key, data) { | |
var decipher = crypto.createDecipher('aes-256-cbc', key); | |
var decrypted = decipher.update(data, 'hex', 'utf-8'); | |
decrypted += decipher.final('utf-8'); | |
return decrypted; | |
} | |
var key = "supersecretkey"; | |
var text = "123|a123123123123123"; | |
console.log("Original Text: " + text); | |
var encryptedText = encrypt(key, text); | |
console.log("Encrypted Text: " + encryptedText); | |
var decryptedText = decrypt(key, encryptedText); | |
console.log("Decrypted Text: " + decryptedText); | |
console.log("\nAnd again...\n"); | |
console.log("Original Text: " + text); | |
encryptedText = encrypt(key, text); | |
console.log("Encrypted Text: " + encryptedText); | |
decryptedText = decrypt(key, encryptedText); | |
console.log("Decrypted Text: " + decryptedText); | |
text = "this is another text"; | |
key = "this is another key"; | |
console.log("\nNew text: & key: " + text); | |
encryptedText = encrypt(key, text); | |
console.log("Encrypted Text: " + encryptedText); | |
decryptedText = decrypt(key, encryptedText); | |
console.log("Decrypted Text: " + decryptedText); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment