You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by Ad...@swmc.com on 2009/09/09 00:58:04 UTC

Fw: Removing passwords

I was able to set the passwords to null and that generally allows PDFBox 
to deal with the data, but PDFMergerUtility can't handle encrypted 
documents at all.  It looks like that's on account of how encryption is 
done in the PDF spec (encrypting data inside COS objects).  I tried adding 
in support for encrypted documents without a password, but that did not 
work out so well because I don't know the PDF structure well enough.

I found that the deprecation notice for 
org.pdfbox.pdmodel.PDDocument.clearWillEncryptWhenSaving()  is "Do not 
rely on this method anymore. It is the responsability of COSWriter to hold 
this state."  Which implies that the answer in how to write out an 
unencrypted document lies in COSWriter.  I found that write() sets 
willEncrypt to false if document.getSecurityHandler() is set to null.  So 
I tried doc.protect(null); but got a NullPointerException[1].  Patching 
the SecurityHandlersManager wouldn't help because it looks like 
willEncrypt in the COSWriter only deals with *decryption* not encryption. 
So it seems that either I don't decrypt my data (which doesn't work, 
obviously) or I decrypt it, but I'm then forced to write it out encrypted 
(on account of COSWriter::write(), line 1002, which is: 
securityHandler.prepareDocumentForEncryption(document);).

Does anyone know how this works or if I'm on the right track?  I simply 
want to remove encryption and all protection from the PDF.  I'll write a 
new SecurityHandler class if I must, but I can't imagine this has never 
come up before...

--Adam

[1]  java.lang.NullPointerException
        at 
org.apache.pdfbox.pdmodel.encryption.SecurityHandlersManager.getSecurityHandler(SecurityHandlersManager.java:164)
        at 
org.apache.pdfbox.pdmodel.PDDocument.protect(PDDocument.java:991)

----- Forwarded by Adam Nichols/UR/CER/XLDynamics on 09/08/2009 14:47 
-----

Adam Nichols/UR/CER/XLDynamics 
09/04/2009 16:10

To
pdfbox-users@incubator.apache.org
cc

Subject
Removing passwords





Now that I've done the encryption, I'm trying to decrypt (imagine that). 
My goal is to read in an encrypted File, and write out that same file 
without the encryption.  I've tried a few different things, and I seem to 
be very close.  The file I write out can be opened in Adobe Acrobat Reader 
and it displays without entering a password, however if I read in the file 
with PDFBox, doc.isEncrypted() will return true.  I opened it in a hex 
editor and it does look like it's encrypted, so I'm guessing what's 
happening is that it's being encrypted with an empty password.  I wanted 
to just remove the encryption all together.  Here's my code:

                AccessPermission ap = new AccessPermission();
                StandardProtectionPolicy spp = new 
StandardProtectionPolicy(null, null, ap);
                StandardDecryptionMaterial sdm = new 
StandardDecryptionMaterial(password);
                StandardSecurityHandler ssh = new 
StandardSecurityHandler(spp);
                ssh.decryptDocument(doc, sdm);
                doc.protect(spp);
                doc.save(fos);

I tried it without doc.protect(spp) but then the old policy still applied 
so it was still encrypted and the password was still needed to open it. 
Anyone know how to completely remove the encryption.

--Adam
?  Click here to submit conditions  

This email and any content within or attached hereto from  Sun West Mortgage Company, Inc.  is confidential and/or legally privileged. The information is intended only for the use of the individual or entity named on this email. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this email information is strictly prohibited, and that the documents should be returned to this office immediately by email. Receipt by anyone other than the intended recipient is not a waiver of any privilege. Please do not include your social security number, account number, or any other personal or financial information in the content of the email. Should you have any questions, please call  (800) 453 7884.