Skip to content

Instantly share code, notes, and snippets.

@pythonl1
Forked from erans/encrypt_decrypt_example.js
Last active October 20, 2016 07:44
Show Gist options
  • Select an option

  • Save pythonl1/580b1dd6d73fdf980e633617f4f2b464 to your computer and use it in GitHub Desktop.

Select an option

Save pythonl1/580b1dd6d73fdf980e633617f4f2b464 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(text, '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 = "PTP";
var text = "9632470188";
var encryptedText = encrypt(key, text);
var decryptedText = decrypt(key, encryptedText);
console.log("Original Text: " + text);
console.log("Encrypted Text: " + encryptedText);
console.log("Decrypted Text: " + decryptedText);
console.log("\nAnd again...\n");
console.log("Original Text: " + text);
encryptedText = encrypt(key, text);
decryptedText = decrypt(key, encryptedText);
console.log("Encrypted Text: " + encryptedText);
console.log("Decrypted Text: " + decryptedText);
text = "9988776655";
key = "AWS";
encryptedText = encrypt(key, text);
decryptedText = decrypt(key, encryptedText);
console.log("\nNew text: & key: " + text);
console.log("Encrypted Text: " + encryptedText);
console.log("Decrypted Text: " + decryptedText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment