0

I am trying PDFBox for merging multiple PDF documents. Code is below. I am facing an issue. For a few PDF files I am getting "java.io.IOException: java.security.InvalidKeyException: Illegal key size".
I am not sure what is going on. Could someone please help me ?

PDFMergerUtility PDFutility = new PDFMergerUtility();
ByteArrayOutputStream mergedPdf = new ByteArrayOutputStream();

PDFutility.addSources(pdfDocs);
PDFutility.setDestinationStream(mergedPdf);
PDFutility.mergeDocuments();

The Error

Caused by: java.io.IOException: java.security.InvalidKeyException: Illegal key size
  at org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.computeEncryptedKeyRev56(StandardSecurityHandler.java:779)
  at org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.computeEncryptedKey(StandardSecurityHandler.java:681)
  at org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.prepareForDecryption(StandardSecurityHandler.java:256)
  at org.apache.pdfbox.pdfparser.COSParser.prepareDecryption(COSParser.java:2929)
  at org.apache.pdfbox.pdfparser.COSParser.retrieveTrailer(COSParser.java:286)
  at org.apache.pdfbox.pdfparser.PDFParser.initialParse(PDFParser.java:173)
  at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:226)
  at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1222)
  at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1140)
  at org.apache.pdfbox.multipdf.PDFMergerUtility.legacyMergeDocuments(PDFMergerUtility.java:455)
  at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:346)
  at org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(PDFMergerUtility.java:330)
Bashir
  • 2,057
  • 5
  • 19
  • 44
Sunil
  • 93
  • 1
  • 7
  • Which Java version do you use? Probably an old version and you have not updated it with the unlimited strength policy for security related algorithms? – mkl Apr 08 '20 at 15:14
  • I am using v 1.7. I agree that JCE needs an upgrade but I get problems with only a few pdfs not all of them. Could this be related to PDF file ? – Sunil Apr 08 '20 at 15:19
  • 1
    PDF supports different encryption methods, some of them ridiculously simple to break. Probably only some of your PDFs are encrypted and only some of those encrypted PDFs are encrypted using a strong algorithm. – mkl Apr 08 '20 at 15:24
  • Is there a way i can find the encryption ? I need a strong reason to upgrade JCE. – Sunil Apr 08 '20 at 15:59
  • Asked and answered http://issues.apache.org/jira/browse/PDFBOX-4808, and [Java Security: Illegal key size or default parameters?](https://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters). The "strong reason" is that you won't be able to open these PDFs without strong crypto capability. – Tilman Hausherr Apr 08 '20 at 21:12
  • 1
    *"I need a strong reason to upgrade JCE"* - if you don't at least install the unlimited strength policy (or use an equivalent hack), signatures and encryption available to you aren't worth a dime. If your use case does not require security and can cope with rejecting files applying stronger security, you don't need to upgrade. – mkl Apr 08 '20 at 21:33

0 Answers0