-
-
Save chris-rock/6cac4e422f29c28c9d88 to your computer and use it in GitHub Desktop.
// 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')); |
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 ?
@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?
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 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?
Thanks!!
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
@skerit Baba, you are beautiful !