0

In the past, I have tried to decrypt PDFs using PDFBox using the following code:

if (doc.isEncrypted()){
    doc.openProtection(new StandardDecryptionMaterial(password));
    doc.setAllSecurityToBeRemoved(true);
}

(Note that I don't use doc.decrypt(password) because the Javadoc for decrypt() says that openProtection() should be used instead, although why it's not just deprecated is beyond me).

Anyway, this has worked fine for quite a while. However, lately I seem to be getting the following exception:

java.io.IOException: javax.crypto.BadPaddingException: Given final block not properly padded
        at javax.crypto.CipherInputStream.getMoreData(CipherInputStream.java:118)
        at javax.crypto.CipherInputStream.read(CipherInputStream.java:236)
        at javax.crypto.CipherInputStream.read(CipherInputStream.java:212)
        at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.encryptData(SecurityHandler.java:316)
        at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.decryptStream(SecurityHandler.java:421)
        at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.decrypt(SecurityHandler.java:390)
        at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.decryptObject(SecurityHandler.java:365)
        at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.proceedDecryption(SecurityHandler.java:196)
        at org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.decryptDocument(StandardSecurityHandler.java:158)
        at org.apache.pdfbox.pdmodel.PDDocument.openProtection(PDDocument.java:1598)
            <Stack trace continues with my call to openProtection()>

How can I fix this problem?

Thunderforge
  • 19,637
  • 18
  • 83
  • 130

1 Answers1

1

This is a known issue, which appears to have been introduced as a result of a Java update. At the time of this writing, a fix for this is in review to be included in version 1.8.8, so just upgrade to version 1.8.8 or later and this problem will go away.

Thunderforge
  • 19,637
  • 18
  • 83
  • 130
  • Yes we're working on this. Please try the 1.8.8 snapshot; if it doesn't work, please attach your file in the PDFBOX-2469 issue. https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox/1.8.8-SNAPSHOT/ – Tilman Hausherr Nov 17 '14 at 07:51
  • @Tilman I use the pdfbox-app version of the jar, but as far as I can tell, the snapshots only come in the unbundled form. Pity because I would be interested in trying it. – Thunderforge Nov 17 '14 at 14:35
  • Not sure what you mean - the snapshot pdfbox-app has the same as the released version. In some cases you may need jai_imageio and/or jbig2 in your classpath. – Tilman Hausherr Nov 17 '14 at 14:38
  • @Tilman, Ah, I understand now. The link you gave me was for plain pdfbox, but there also exists [a repository for pdfbox-app snapshots](http://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox-app/1.8.8-SNAPSHOT/), which has what I need. I'll give it a try when I get a chance. – Thunderforge Nov 17 '14 at 16:29