Skip to content

Instantly share code, notes, and snippets.

@ashishtiwari1993
Created March 19, 2019 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashishtiwari1993/2c90fcb41a7d70c21317dd16a6d468f5 to your computer and use it in GitHub Desktop.
Save ashishtiwari1993/2c90fcb41a7d70c21317dd16a6d468f5 to your computer and use it in GitHub Desktop.
AES encryption and decryption with Node JS.
const crypto = require('crypto');
key = "secretKey";
string = "your string";
let encryptedString = encrypt(key,string);
let decryptedString = decrypt(key,encryptedString);
console.log(encryptedString);
console.log(decryptedString);
function encrypt(KEY,text){
const cipher = crypto.createCipher('aes256', KEY);
var encrypted = cipher.update(text,'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
function decrypt(KEY,encryptedText){
const decipher = crypto.createDecipher('aes256', KEY)
var decrypted = decipher.update(encryptedText,'hex','utf8')
decrypted += decipher.final('utf8');
return decrypted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment