Skip to content

Instantly share code, notes, and snippets.

@chris-rock
Last active November 24, 2023 09:48
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save chris-rock/6cac4e422f29c28c9d88 to your computer and use it in GitHub Desktop.
Save chris-rock/6cac4e422f29c28c9d88 to your computer and use it in GitHub Desktop.
Encrypt and decrypt buffers in nodejs
// Part of https://github.com/chris-rock/node-crypto-examples
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq';
function encrypt(buffer){
var cipher = crypto.createCipher(algorithm,password)
var crypted = Buffer.concat([cipher.update(buffer),cipher.final()]);
return crypted;
}
function decrypt(buffer){
var decipher = crypto.createDecipher(algorithm,password)
var dec = Buffer.concat([decipher.update(buffer) , decipher.final()]);
return dec;
}
var hw = encrypt(new Buffer("hello world", "utf8"))
// outputs hello world
console.log(decrypt(hw).toString('utf8'));
@Elements-
Copy link

Thanks!

@iraniamir
Copy link

thX

@skerit
Copy link

skerit commented Feb 22, 2018

New node versions will output a warning when using createCipher instead of createCipheriv.
Of course, when using a cipher, you also have to add it to the result.

The way I'm doing it now:

function encrypt(chunk) {

	var cipher,
	    result,
	    iv;

	// Create an iv
	iv = crypto.randomBytes(16);

	// Create a new cipher
	cipher = crypto.createCipheriv(algorithm, password, iv);

	// Create the new chunk
	result = Buffer.concat([iv, cipher.update(chunk), cipher.final()]);

	return result;
}

and

function decrypt(chunk) {

	var decipher,
	    result,
	    iv;

	// Get the iv: the first 16 bytes
	iv = chunk.slice(0, 16);

	// Get the rest
	chunk = chunk.slice(16);

	// Create a decipher
	decipher = crypto.createDecipheriv(algorithm, password, iv);

	// Actually decrypt it
	result = Buffer.concat([decipher.update(chunk), decipher.final()]);

	return result;
}

@f0r3v3ran00b
Copy link

f0r3v3ran00b commented Apr 29, 2018

Thanks!

Thank you also so much, @skerit - your comment helped me a lot!

@ystrdy
Copy link

ystrdy commented May 5, 2018

Thanks!

@memanoj
Copy link

memanoj commented Jul 16, 2018

it work great but i encrypted large mp4 file then i want to play the encrypted video by decrypted method but for 300mb it took 10-12 sec i want progress of decryption so it is possible get progress of decryption and also try to play encrypted video according to progress

@ithustle
Copy link

Any luck?

it work great but i encrypted large mp4 file then i want to play the encrypted video by decrypted method but for 300mb it took 10-12 sec i want progress of decryption so it is possible get progress of decryption and also try to play encrypted video according to progress

@omidnikrah
Copy link

@skerit Hi, I'm using your encrypt and decrypt function to encrypt a file while streaming but I get the following error:

dest.on is not a function 

Can you help me?

@web3dev6
Copy link

@skerit Baba, you are beautiful !

@hdiaz-nectia
Copy link

Hi, I tried to apply this encryption to a buffer pdf created using html-pdf, without encryption I can download it and open correctly, but using encryption method when I download it and open, an error indicates that the file is corrupt. How can I solve it ?

@skerit
Copy link

skerit commented Nov 17, 2020

@omidnikrah Sorry, I didn't get any notifications from Gist :/ I hope you figured it out by now, can't really help because I don't know what dist is in your case :)

@hdiaz-nectia You're manually encrypting the buffer with this method & downloading that encrypted buffer with a browser, without decrypting it on the client side? What you're downloading then is the encrypted buffer, any program opening it will not understand what's in it... because it's encrypted :) Or am I misunderstanding something here?

@hdiaz-nectia
Copy link

hdiaz-nectia commented Nov 17, 2020

Yes, I download encrypted buffer with a rest api like content-type application/pdf and attachments headers. What I'm looking for is to protect the pdf with a password, so I can open it only with this password.

@skerit
Copy link

skerit commented Nov 17, 2020

@hdiaz-nectia That's totally out of the scope of this example, sorry. Anything encrypted with this method would have to be decrypted the same way. You'd be better of archiving the pdf & using any kind of password protection that archive gives you. Or maybe PDF has some built-in password protection?

@vasilyman
Copy link

Thanks!!

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