Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media encrypted with kbpgp showing corruption #180

Open
steverecio opened this issue Dec 11, 2018 · 5 comments
Open

Media encrypted with kbpgp showing corruption #180

steverecio opened this issue Dec 11, 2018 · 5 comments

Comments

@steverecio
Copy link

steverecio commented Dec 11, 2018

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).

coding_armored.txt

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);
            });
        };
    });
}
@LoulergueC
Copy link

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.

@steverecio
Copy link
Author

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)

@jcherniak
Copy link

@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.

@steverecio
Copy link
Author

@jcherniak No, I never got this library working. I had to switch to OpenPGP.

@maxtaco
Copy link
Contributor

maxtaco commented Jan 15, 2020

Seems like you are getting a base64-encoded output if it's 25-30% larger on decryption

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants