1

I am working on a application where I am selecting pdf file from local file and encrypting it with openpgpjs. Though I am able to encrypt the pdf, When I try to open the encrypted pdf in chrome it is showing dialog with message "Failed to load PDF document.".
But when I decrypt the file manually it is showing original pdf properly.
Here is the code snippet:
Select file and assign to uint8Array:

 //file picker listener uint8Array 
            document.getElementById("file_picker").onchange = function (event) {
                var file = event.target.files[0];
                console.log(file);
                var reader = new FileReader();
                reader.readAsArrayBuffer(file);

                reader.addEventListener('load', (loadEvent) => {
                    console.log("array data")
                    var buffer = loadEvent.target.result;
                    uint8Array = new Uint8Array(buffer);
                    console.log(uint8Array);
                })
            }

Encrypt and download encrypted file :

 //encrypt click listener
            document
                .getElementById("encrypt")
                .addEventListener("click", function (e) {

                    // 
                    if (uint8Array != undefined) {

                        (async () => {
                            const message = await openpgp.createMessage({ binary: uint8Array });
                            const encrypted = await openpgp.encrypt({
                                message, // input as Message object
                                passwords: ['password'], // multiple passwords possible
                                format: 'binary' // don't ASCII armor (for Uint8Array output)
                            });
                            console.log(encrypted); // Uint8Array

                            downloadBlob(encrypted, 'data.pdf', 'application/pdf');

                        })();

                    }

                    // 
                });

            function downloadBlob(data, fileName, mimeType) {
                var blob, url;
                blob = new Blob([data], {
                    type: mimeType
                });
                url = window.URL.createObjectURL(blob);
                downloadURL(url, fileName);
                setTimeout(function () {
                    return window.URL.revokeObjectURL(url);
                }, 1000);
            }

            function downloadURL(data, fileName) {
                var a;
                a = document.createElement('a');
                a.href = data;
                a.download = fileName;
                document.body.appendChild(a);
                a.style = 'display: none';
                a.click();
                a.remove();
            }

I can see the pdf getting download but when I open it, It is showing dialog with message saying "Failed to load PDF document.".
Do I need to set any value to get dialog popup?
Updated:
FYI: I have tried with CryptoJs with AES encryption from here and I still get the same issue.
Expected output should show prompt dialog like below:

enter image description here

KonTash
  • 158
  • 1
  • 2
  • 15
  • 2
    If I understand it correctly, you encrypt a pdf file with PGP (with [openpgpjs](https://github.com/openpgpjs/openpgpjs#examples)) and expect a password protected PDF file to be generated. But a PGP encrypted pdf file (no matter whether with key or password) and a [password protected PDF file](https://www.adobe.com/acrobat/online/password-protect-pdf.html) are not the same thing, but have different formats, as a comparison with a hex editor should show. So you probably need a JavaScript library that supports the creation of password protected PDF files. – Topaco Aug 12 '22 at 10:24
  • Ah, That explain everything, I am encrypting pdf with openpgpjs but expecting a password dialog. My bad I should look for a library that support password protect file not any encryption. Gotcha. Thanks – KonTash Aug 12 '22 at 10:34
  • Probably, besides the format, the algorithm will also differ. [Here](https://www.pdflib.com/pdf-knowledge-base/pdf-password-security/encryption/#:~:text=PDF%20encryption%20makes%20use%20of,in%20the%20standard%20FIPS%2D197) is shown an overview of the various encryption algorithms used by the different PDF versions. [PDFTron](https://www.pdftron.com/documentation/web/guides/security/encrypt/) seems to provide an API (but probably not for free). – Topaco Aug 12 '22 at 12:28

0 Answers0