Skip to content

Instantly share code, notes, and snippets.

@getchenge
Created May 19, 2016 09:16
Show Gist options
  • Save getchenge/7b47bbd9519c8dd113b019f23026ca01 to your computer and use it in GitHub Desktop.
Save getchenge/7b47bbd9519c8dd113b019f23026ca01 to your computer and use it in GitHub Desktop.
node aes cryptor
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;
}
}
@rodolfobandeira
Copy link

rodolfobandeira commented Mar 10, 2018

@kukukukikii How do you calculate the iv in this case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment