Created
May 19, 2016 09:16
-
-
Save getchenge/7b47bbd9519c8dd113b019f23026ca01 to your computer and use it in GitHub Desktop.
node aes cryptor
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
const crypto = require('crypto'); | |
const cryptor = { | |
encrypt(cryptkey, iv, cleardata) { | |
cryptkey = crypto.createHash('sha256').update(cryptkey).digest(); | |
const encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv); | |
let encryptdata = encipher.update(cleardata, 'utf8', 'binary'); | |
encryptdata += encipher.final('binary'); | |
return new Buffer(encryptdata, 'binary').toString('base64'); | |
}, | |
decrypt(cryptkey, iv, encryptdata) { | |
cryptkey = crypto.createHash('sha256').update(cryptkey).digest(); | |
encryptdata = new Buffer(encryptdata, 'base64').toString('binary'); | |
const decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv); | |
let decoded = decipher.update(encryptdata, 'binary', 'utf8'); | |
decoded += decipher.final('utf8'); | |
return decoded; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@kukukukikii How do you calculate the
iv
in this case?