Skip to content

Instantly share code, notes, and snippets.

@airtheva
Last active March 9, 2022 05:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save airtheva/6307570 to your computer and use it in GitHub Desktop.
Save airtheva/6307570 to your computer and use it in GitHub Desktop.
// An example of decrypting AES/ECB/PKCS5Padding-encrypted buffer in node.js.
// Hope this gist will help you. Feel free to discuss with me.
var crypto = require('crypto');
// Change this to fit your needs.
// The buffer of your raw password.
var keyBuffer = new Buffer('12345678');
// The buffer of your data to be decrypted.
var encryptedBuffer = new Buffer('12345678');
// ECB mode won't need IV, so keep it like this and it will work well.
var ivBuffer = new Buffer('');
// crypto.createDecipher() receives argument as passpharse, and uses the passpharse to generate raw password and IV.
// Which really confuses me a lot.
var decipher = crypto.createDecipheriv('aes-128-ecb', keyBuffer, ivBuffer);
var chunks = [];
chunks.push(decipher.update(encryptedBuffer, 'buffer', 'hex'));
chunks.push(decipher.final('hex'));
// Use chunks.join('') to combine all parts, and then parse the hex string into Buffer object.
var decryptedBuffer = new Buffer(chunks.join(''), 'hex');
// The decryptedBuffer is what you want.
// Done.
@utamas
Copy link

utamas commented May 25, 2016

thanks.

@MohammedEssehemy
Copy link

Thanks.
you made my day.

@Mo33n
Copy link

Mo33n commented Nov 22, 2017

Thanks,

Copy link

ghost commented Aug 2, 2018

Works great, thanks!

@smit-code
Copy link

Data encryption in Android and decrypting in node.js how this is possible.

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