-
-
Save cevatkerim/800e88a34a05f6fa30a7 to your computer and use it in GitHub Desktop.
Encrypt Decrypt in Node using crypto
This file contains 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
var SecurityUtil = (function(key) { | |
var crypto = require('crypto'); | |
var secret=new Buffer(key); | |
var encrypt = function(text) { | |
var cipher = crypto.createCipher('aes-256-cbc', secret); | |
cipher.update(text, 'utf8', 'hex'); | |
var cipherText = cipher.final('hex'); | |
return cipherText; | |
}; | |
var decrypt = function(encryptedText) { | |
try { | |
var decipher = crypto.createDecipher('aes-256-cbc', secret); | |
decipher.update(encryptedText, 'hex', 'utf8'); | |
return decipher.final('utf8'); | |
} catch (e) { | |
return undefined; | |
} | |
}; | |
return { | |
encrypt: encrypt, | |
decrypt: decrypt | |
}; | |
}); | |
var util = SecurityUtil('some stuff to encrypt'); | |
var encrypted = util.encrypt('test this out') | |
console.log(encrypted); | |
var original = util.decrypt(encrypted); | |
console.log(original); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment