Created
March 19, 2019 06:48
-
-
Save ashishtiwari1993/2c90fcb41a7d70c21317dd16a6d468f5 to your computer and use it in GitHub Desktop.
AES encryption and decryption with 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
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