Skip to content

Instantly share code, notes, and snippets.

@chris-rock
Last active September 18, 2024 15:00
Show Gist options
  • 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'));
@Shanmugapriya-R805
Copy link

Shanmugapriya-R805 commented Sep 18, 2024

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.

@hdiaz-nectia Have you got the solution for this case? I wanted to implement this same case

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