-
-
Save cdimascio/62a5d0b6ddceb2df6af30fb4542fb060 to your computer and use it in GitHub Desktop.
Encrypt and decrypt text in nodejs
This file contains hidden or 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
// Part of https://github.com/chris-rock/node-crypto-examples | |
// Nodejs encryption with CTR | |
var crypto = require('crypto'), | |
algorithm = 'aes-256-ctr', | |
password = 'd6F3Efeq', | |
key = Buffer.from('5ebe2294ecd0e0f08eab7690d2a6ee69', 'hex'), | |
iv = Buffer.from('26ae5cc854e36b6bdfca366848dea6bb', 'hex'); | |
function encrypt(text){ | |
const cipher = crypto.createCipheriv('aes-128-cbc', key, iv); | |
// var cipher = crypto.createCipheriv(algorithm,password) | |
var crypted = cipher.update(text,'utf8','hex') | |
crypted += cipher.final('hex'); | |
return crypted; | |
} | |
function decrypt(text){ | |
var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv); | |
// var decipher = crypto.createDecipher(algorithm,password) | |
var dec = decipher.update(text,'hex','utf8') | |
dec += decipher.final('utf8'); | |
return dec; | |
} | |
var hw = encrypt("hello world") | |
// outputs hello world | |
console.log(decrypt(hw)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment