Media encrypted with kbpgp showing corruption
I'm encrypting a number of files using kbpgp and decrypting them with GPG. While ascii text file encryption/decryption seems to work fine, I'm seeing file corruption with various media such as PDF files and JPEGs. Perhaps this could be an issue with the file compression?
For example, below I've attached a PDF copy of Cracking the Coding Interview as well as the corrupted version decrypted using GPG.
Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions.pdf
decrypted_cracking_the_coding_interview.pdf
Just to confirm it wasn't an issue with manipulating the raw buffer, I get the same result using the armored message output (provided below).
export const encryptFile = (file, pubkey, callback_func) => {
kbpgp.KeyManager.import_from_armored_pgp({
armored: pubkey
}, function(error, pgp_key) {
if (error) {
throw(error);
}
// do something with pgp_key
var r = new FileReader();
r.readAsArrayBuffer(file);
r.onloadend = function(file) {
var buffer = new kbpgp.Buffer(r.result);
var params = {
msg: buffer,
encrypt_for: pgp_key,
};
kbpgp.box (params, function(error, result_armored_string, result_raw_buffer) {
if (error) {
throw(error);
}
callback_func(result_raw_buffer);
});
};
});
}
I don't think that an ArrayBuffer is really useful here and can, maybe, be the starting point of your trouble.
Try to replace r.readAsArrayBuffer(file); by r.readAsBinaryString(file); like the documentation recommend it.
Switching to r.readAsBinaryString(file) similarly yields a corrupted PDF except that every page is blank now (using r.readAsArrayBuffer(file) gives me a PDF with some pages containing pieces of the original content as shown in the attached file). Both methods produce a PDF with the same number of pages as the original document.
Also, the MDN web docs recommend using readAsArrayBuffer over readAsBinaryString:
Note that this method was once removed from the File API specification, but re-introduced for backward compatibility. Using FileReader.readAsArrayBuffer() is recommended. (source)
@steverecio, did you ever get this figured out? I'm getting the same corruption issues. The files decrypt with my PGP key fine, but are about 25-30% larger.
@jcherniak No, I never got this library working. I had to switch to OpenPGP.
Seems like you are getting a base64-encoded output if it's 25-30% larger on decryption