Skip to content

Instantly share code, notes, and snippets.

@theodorosidmar
Last active May 2, 2018 18:59
Show Gist options
  • Select an option

  • Save theodorosidmar/080128f974bbc865efac4f8660e7a45d to your computer and use it in GitHub Desktop.

Select an option

Save theodorosidmar/080128f974bbc865efac4f8660e7a45d to your computer and use it in GitHub Desktop.
Encrypt and decrypt texts to hexadecimal
const crypto = require('crypto')
const algorithm = 'aes-128-cbc'
const key = Buffer.from('5ebe2294ecd0e0f08eab7690d2a6ee69', 'hex')
const iv = Buffer.from('26ae5cc854e36b6bdfca366848dea6bb', 'hex')
exports.encrypt = (text) => {
const cipher = crypto.createCipheriv(algorithm, key, iv)
let crypted = cipher.update(text, 'utf8', 'hex')
crypted += cipher.final('hex')
return crypted
}
exports.decrypt = (text) => {
const decipher = crypto.createDecipheriv(algorithm, key, iv)
let decrypted = decipher.update(text, '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