You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Gary LosHuertos <ga...@arc90.com> on 2010/08/27 19:58:02 UTC

Can't decrypt with default password, but can open without password

Hello all,

I have a collection of PDFs I'm extracting text from programmatically, and
some of them are encrypted.  All of them can be opened in OS X's Preview
without supplying a password, but when decrypting with PDFBox, a small
portion of result in an exception on decryption:

Exception in thread "main"
org.apache.pdfbox.exceptions.CryptographyException: Error: The supplied
password does not match either the owner or user password in the document.
at
org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.decryptDocument(StandardSecurityHandler.java:231)
at org.apache.pdfbox.pdmodel.PDDocument.openProtection(PDDocument.java:1111)
...

This happens both with

doc.openProtection(new StandardDecryptionMaterial(""));

and

doc.decrypt("");

(not that I'm sure there's a difference).  Is there some magic that Preview
is doing to decrypt the document, or am I just hosed?  Any help would be
greatly appreciated.  Thanks!

Gary LosHuertos
Arc90

Re: Can't decrypt with default password, but can open without password

Posted by Ad...@swmc.com.
Try something like this:

PDDocument doc = null;
try {
    doc = PDDocument.load(yourFile);
    if(doc.isEncrypted()) {
        StandardDecryptionMaterial sdm = new 
StandardDecryptionMaterial("");
        doc.openProtection(sdm);
    }
} finally {
    try { if(doc != null) doc.close(); } catch(Exception ex) { } }
}

Also, if you want to remove the security when you save, just call
doc.setAllSecurityToBeRemoved(true);
doc.save(fos); // fos being your FileOutputStream
fos.close();

---- 
Thanks,
Adam





From:
Gary LosHuertos <ga...@arc90.com>
To:
users@pdfbox.apache.org
Date:
08/27/2010 10:58
Subject:
Can't decrypt with default password, but can open without password



Hello all,

I have a collection of PDFs I'm extracting text from programmatically, and
some of them are encrypted.  All of them can be opened in OS X's Preview
without supplying a password, but when decrypting with PDFBox, a small
portion of result in an exception on decryption:

Exception in thread "main"
org.apache.pdfbox.exceptions.CryptographyException: Error: The supplied
password does not match either the owner or user password in the document.
at
org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.decryptDocument(StandardSecurityHandler.java:231)
at 
org.apache.pdfbox.pdmodel.PDDocument.openProtection(PDDocument.java:1111)
...

This happens both with

doc.openProtection(new StandardDecryptionMaterial(""));

and

doc.decrypt("");

(not that I'm sure there's a difference).  Is there some magic that 
Preview
is doing to decrypt the document, or am I just hosed?  Any help would be
greatly appreciated.  Thanks!

Gary LosHuertos
Arc90



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

Re: Can't decrypt with default password, but can open without password

Posted by Ad...@swmc.com.
Gary,

Create an issue on JIRA[1], attach your patch file (and make sure click 
the option so it can be included under the Apache License), and we'll 
check give it a try and see if we can get it included in the next release. 
 Also, if you can upload a PDF file which demonstrates the problem that'll 
let us confirm it's working as expected and do some debugging / analysis.

[1] https://issues.apache.org/jira/browse/PDFBOX

---- 
Thanks,
Adam





From:
Gary LosHuertos <ga...@arc90.com>
To:
users@pdfbox.apache.org
Date:
08/27/2010 15:04
Subject:
Re: Can't decrypt with default password, but can open without password



The issue, as it turns out, is that Adobe added a section to the standard
when crypt filters were added.  In my case, the StrF and StmF use the same
algorithm as would be used without crypt filters, so that's not an issue.
 The real issue here is that "EncryptMetadata", which was added with crypt
filters, passes 0xFFFFFFFF to the MD5 hash following the first element of
the file's identifier array, when EncryptMetadata is false (it defaults to
true).

I've developed a fix and verified it for my use case.  I've never 
submitted
code to a project in my life; I'm guessing I should bring it up on the dev
list.

Gary

On Fri, Aug 27, 2010 at 3:19 PM, Andreas Lehmkuehler 
<an...@lehmi.de>wrote:

> Hi,
>
> Am 27.08.2010 21:10, schrieb Gary LosHuertos:
>
>  The encryption dictionary is as follows (I omitted the user and owner
>> strings):
>> 132 0 obj<</R 4/Length 128/Filter/Standard/O(...)/P -1324/U(...)/V
>> 4/CF<</StdCF<</Length
>> 16/CFM/V2/AuthEvent/DocOpen>>>>/StmF/StdCF/StrF/StdCF/EncryptMetadata
>> false>>
>> endobj
>>
> Looks like the "new" encryption algorithm which isn't supported yet.
> See [1] for further details.
>
> BR
> Andreas Lehmkühler
>
> [1] https://issues.apache.org/jira/browse/PDFBOX-697
>



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

Re: Can't decrypt with default password, but can open without password

Posted by Gary LosHuertos <ga...@arc90.com>.
The issue, as it turns out, is that Adobe added a section to the standard
when crypt filters were added.  In my case, the StrF and StmF use the same
algorithm as would be used without crypt filters, so that's not an issue.
 The real issue here is that "EncryptMetadata", which was added with crypt
filters, passes 0xFFFFFFFF to the MD5 hash following the first element of
the file's identifier array, when EncryptMetadata is false (it defaults to
true).

I've developed a fix and verified it for my use case.  I've never submitted
code to a project in my life; I'm guessing I should bring it up on the dev
list.

Gary

On Fri, Aug 27, 2010 at 3:19 PM, Andreas Lehmkuehler <an...@lehmi.de>wrote:

> Hi,
>
> Am 27.08.2010 21:10, schrieb Gary LosHuertos:
>
>  The encryption dictionary is as follows (I omitted the user and owner
>> strings):
>> 132 0 obj<</R 4/Length 128/Filter/Standard/O(...)/P -1324/U(...)/V
>> 4/CF<</StdCF<</Length
>> 16/CFM/V2/AuthEvent/DocOpen>>>>/StmF/StdCF/StrF/StdCF/EncryptMetadata
>> false>>
>> endobj
>>
> Looks like the "new" encryption algorithm which isn't supported yet.
> See [1] for further details.
>
> BR
> Andreas Lehmkühler
>
> [1] https://issues.apache.org/jira/browse/PDFBOX-697
>

Re: Can't decrypt with default password, but can open without password

Posted by Andreas Lehmkuehler <an...@lehmi.de>.
Hi,

Am 27.08.2010 21:10, schrieb Gary LosHuertos:
> The encryption dictionary is as follows (I omitted the user and owner
> strings):
> 132 0 obj<</R 4/Length 128/Filter/Standard/O(...)/P -1324/U(...)/V
> 4/CF<</StdCF<</Length
> 16/CFM/V2/AuthEvent/DocOpen>>>>/StmF/StdCF/StrF/StdCF/EncryptMetadata
> false>>
> endobj
Looks like the "new" encryption algorithm which isn't supported yet.
See [1] for further details.

BR
Andreas Lehmkühler

[1] https://issues.apache.org/jira/browse/PDFBOX-697

Re: Can't decrypt with default password, but can open without password

Posted by Gary LosHuertos <ga...@arc90.com>.
Julien,

Unfortunately the documents are from a business partner of a client; had I
created them myself I most certainly would not have protected them :) I've
looked in my keychain and I have no related credentials.  When viewing the
file in both Preview and Skim, a password is not prompted for, the document
is displayed in its entirety, and the document's properties show "Encrypted"
as "Yes".

If I attempt to just go ahead and strip the text out using PDFTextStripper,
ie catch-and-ignore any exceptions arising from decryption, I still get

Caused by: org.apache.pdfbox.exceptions.WrappedIOException: Error decrypting
document, details:
at
org.apache.pdfbox.util.PDFTextStripper.writeText(PDFTextStripper.java:250)
at org.apache.pdfbox.util.PDFTextStripper.getText(PDFTextStripper.java:184)
... 24 more
Caused by: org.apache.pdfbox.exceptions.CryptographyException: Error: The
supplied password does not match either the owner or user password in the
document.
at
org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.decryptDocument(StandardSecurityHandler.java:231)
at org.apache.pdfbox.pdmodel.PDDocument.openProtection(PDDocument.java:1111)
at org.apache.pdfbox.pdmodel.PDDocument.decrypt(PDDocument.java:591)
at
org.apache.pdfbox.util.PDFTextStripper.writeText(PDFTextStripper.java:246)
... 25 more

The encryption dictionary is as follows (I omitted the user and owner
strings):
132 0 obj<</R 4/Length 128/Filter/Standard/O(...)/P -1324/U(...)/V
4/CF<</StdCF<</Length
16/CFM/V2/AuthEvent/DocOpen>>>>/StmF/StdCF/StrF/StdCF/EncryptMetadata
false>>
endobj

Thanks again,

Gary

On Fri, Aug 27, 2010 at 2:45 PM, Julien Plée <ju...@gmail.com> wrote:

> Hello Gary,
>
> You may try to open it in an other free reader not based on the MacOS
> library.
> Either I would think that:
> 1) you stored the password in keychain
> Or 2) you didn't protect the documents
>
> Regards,
>
> Julien
>
>
>
>
>  Hello all,
>>
>> I have a collection of PDFs I'm extracting text from programmatically, and
>> some of them are encrypted.  All of them can be opened in OS X's Preview
>> without supplying a password, but when decrypting with PDFBox, a small
>> portion of result in an exception on decryption:
>>
>> Exception in thread "main"
>> org.apache.pdfbox.exceptions.CryptographyException: Error: The supplied
>> password does not match either the owner or user password in the document.
>> at
>>
>> org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.decryptDocument(StandardSecurityHandler.java:231)
>> at
>> org.apache.pdfbox.pdmodel.PDDocument.openProtection(PDDocument.java:1111)
>> ...
>>
>> This happens both with
>>
>> doc.openProtection(new StandardDecryptionMaterial(""));
>>
>> and
>>
>> doc.decrypt("");
>>
>> (not that I'm sure there's a difference).  Is there some magic that
>> Preview
>> is doing to decrypt the document, or am I just hosed?  Any help would be
>> greatly appreciated.  Thanks!
>>
>> Gary LosHuertos
>> Arc90
>>
>

Re: Can't decrypt with default password, but can open without password

Posted by Julien Plée <ju...@gmail.com>.
Hello Gary,

You may try to open it in an other free reader not based on the MacOS  
library.
Either I would think that:
1) you stored the password in keychain
Or 2) you didn't protect the documents

Regards,

Julien



> Hello all,
>
> I have a collection of PDFs I'm extracting text from  
> programmatically, and
> some of them are encrypted.  All of them can be opened in OS X's  
> Preview
> without supplying a password, but when decrypting with PDFBox, a small
> portion of result in an exception on decryption:
>
> Exception in thread "main"
> org.apache.pdfbox.exceptions.CryptographyException: Error: The  
> supplied
> password does not match either the owner or user password in the  
> document.
> at
> org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.decryptDocument( 
> StandardSecurityHandler.java:231)
> at org.apache.pdfbox.pdmodel.PDDocument.openProtection 
> (PDDocument.java:1111)
> ...
>
> This happens both with
>
> doc.openProtection(new StandardDecryptionMaterial(""));
>
> and
>
> doc.decrypt("");
>
> (not that I'm sure there's a difference).  Is there some magic that  
> Preview
> is doing to decrypt the document, or am I just hosed?  Any help  
> would be
> greatly appreciated.  Thanks!
>
> Gary LosHuertos
> Arc90