Skip to content

Instantly share code, notes, and snippets.

@erans
Last active October 14, 2022 02:31
Show Gist options
  • Save erans/8115172 to your computer and use it in GitHub Desktop.
Save erans/8115172 to your computer and use it in GitHub Desktop.
Example of encryption and decryption in node.js
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);
Copy link

ghost commented Jan 29, 2015

Thank you!

@joesanford
Copy link

I believe on line 5 the intended code is var crypted = cipher.update(data, 'utf-8', 'hex');. text is not defined in the scope of the encrypt function.

@hessel
Copy link

hessel commented May 23, 2017

Thanks!!!

@Amey11
Copy link

Amey11 commented Aug 22, 2017

Thanks !! This code is very good and very easy to use Thank you BOSS

@timcarl
Copy link

timcarl commented Nov 23, 2017

Thank you! The W3C example is either outdated or deprecated. I appreciate the example.

@sergio-domingues
Copy link

sergio-domingues commented Apr 9, 2018

Bear in mind that this example does not define an Initialization Vector (IV) which can lead to security issues.
Have a look on Node Crypto method description

@shamii087
Copy link

shamii087 commented Apr 23, 2018

"encrypt": function ('password'){
var key = "123|a123123123123123@&";
var cipher = crypto.createCipher('aes-256-cbc', key);
var crypted = cipher.update(data, 'utf-8', 'hex');
crypted += cipher.final('hex');
return crypted;
},
"decrypt":function('password') {
var key = "123|a123123123123123@&";
var decipher = crypto.createDecipher('aes-256-cbc', key);
var decrypted = decipher.update(data, 'hex', 'utf-8');
decrypted += decipher.final('utf-8');
return decrypted;
}

#All: You can use this code after a bit changes.

@PradeepPsudo
Copy link

Thank you, this code is easy to understand and use.

@ham5768
Copy link

ham5768 commented May 15, 2018

Great it helps me a lot thank you bro

@XaooB
Copy link

XaooB commented May 18, 2018

Why theres not IV defined? Am I missing something ?

@swapnilmarathe007
Copy link

@XaooB
Use function createCipheriv and createDecipheriv to encypt and decrypt file with random IV if needed.

@nazariyv
Copy link

nazariyv commented Jun 3, 2019

if you take an encrypted value using this method, and start the script anew, providing that value, it won't decrypt it correctly. How can you take an encrypted value and then decrypt it by starting the script anew?

@erans
Copy link
Author

erans commented Jun 3, 2019

@nazariyv first, there was a copy/paste error in Line #5 which I just fixed.

Second, what do you mean it doesn't work correctly after starting it again? Assuming you specify the exact same key and the right encrypted output it should decrypt it without a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment