Skip to content

Instantly share code, notes, and snippets.

@chrisyip
Forked from adrianbravo/encrypt-decrypt.js
Last active July 6, 2017 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrisyip/972cc8639bfff9d5883670e5de576953 to your computer and use it in GitHub Desktop.
Save chrisyip/972cc8639bfff9d5883670e5de576953 to your computer and use it in GitHub Desktop.
Basic Node.js crypto cipher/decipher example.
'use strict'
const crypto = require('crypto')
const key = 'salt_from_the_user_document'
const plaintext = '56d008a27c36552a0f97b291'
const cipher = crypto.createCipher('aes-256-cbc', key)
const decipher = crypto.createDecipher('aes-256-cbc', key)
let encryptedPassword = cipher.update(plaintext, 'utf8', 'base64')
encryptedPassword += cipher.final('base64')
let decryptedPassword = decipher.update(encryptedPassword, 'base64', 'utf8')
decryptedPassword += decipher.final('utf8')
console.log('encrypted :', encryptedPassword)
console.log('decrypted :', decryptedPassword)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment