Skip to content

Instantly share code, notes, and snippets.

@eldoy
Created April 12, 2018 04:12
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 eldoy/f7bcedddc5fefeb76bb250589d5e04cc to your computer and use it in GitHub Desktop.
Save eldoy/f7bcedddc5fefeb76bb250589d5e04cc to your computer and use it in GitHub Desktop.
// Nodejs encryption with CTR
const crypto = require('crypto')
class Crypto {
constructor () {
this.algorithm = 'aes192'
this.secret = 'd6F3Efeq'
}
encrypt (text) {
let cipher = crypto.createCipher(this.algorithm, this.secret)
let crypted = cipher.update(text, 'utf8', 'hex')
crypted += cipher.final('hex')
return crypted
}
decrypt (text) {
let decipher = crypto.createDecipher(this.algorithm, this.secret)
let dec = decipher.update(text, 'hex', 'utf8')
dec += decipher.final('utf8')
return dec
}
}
const c = module.exports = new Crypto()
// DEMO:
let enc = c.encrypt('Hello')
console.log(enc)
let dec = c.decrypt(enc)
console.log(dec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment