Skip to content

Instantly share code, notes, and snippets.

@maxbeatty
Created November 5, 2014 03:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxbeatty/f66c90915a1caabbf7c6 to your computer and use it in GitHub Desktop.
Save maxbeatty/f66c90915a1caabbf7c6 to your computer and use it in GitHub Desktop.
Encrypt and decrypt a string with Node.js
crypto = require('crypto');
exampleClientId = 'my_client_id'
mySharedKey = 'shared_key'
cipher = crypto.createCipher('aes256', mySharedKey)
cipherText = cipher.update(exampleClientId, 'utf8', 'base64')
cipherText += cipher.final('base64')
decipher = crypto.createDecipher('aes256', mySharedKey)
res = decipher.update(cipherText, 'base64', 'utf8')
res += decipher.final('utf8')
console.log(exampleClientId, res, exampleClientId === res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment