Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-2781

Opening pdf document after encrypting it with PDFBox throws IllegalBlockSizeException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8.9, 1.8.10, 2.0.0
    • 1.8.10, 2.0.0
    • Crypto, PDModel, Writing
    • None
    • jdk 1.7.0_55, bouncy castle provider 1.52

    Description

      I'm trying to encrypt pdf document as per [1] like this:

      PDDocument doc = PDDocument.loadNonSeq(getClass().getResourceAsStream("/testform_protected.pdf"), null);
      
      int keyLength = 128; // 40 or 128; 256 will be available in version 2.0
      AccessPermission ap = doc.getCurrentAccessPermission();
      
      StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "", ap);
      spp.setEncryptionKeyLength(keyLength);
      spp.setPermissions(ap);
      doc.protect(spp);
      
      File outFile = new File("./target/testform_saved.pdf");
      
      doc.save(outFile);
      doc.close();
      
      PDDocument loaded = PDDocument.loadNonSeq(outFile, null);
      

      It works fine but opening the encrypted document with PDFBox (last line of above code) throws:

      org.apache.pdfbox.exceptions.WrappedIOException
      	at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.encryptData(SecurityHandler.java:371)
      	at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.decryptString(SecurityHandler.java:577)
      	at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.decryptString(NonSequentialPDFParser.java:1571)
      	at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.decryptDictionary(NonSequentialPDFParser.java:1535)
      	at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.decrypt(NonSequentialPDFParser.java:1596)
      	at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.parseObjectDynamically(NonSequentialPDFParser.java:1460)
      	at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.parseObjectDynamically(NonSequentialPDFParser.java:1343)
      	at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.initialParse(NonSequentialPDFParser.java:383)
      	at org.apache.pdfbox.pdfparser.NonSequentialPDFParser.parse(NonSequentialPDFParser.java:886)
      	at org.apache.pdfbox.pdmodel.PDDocument.loadNonSeq(PDDocument.java:1273)
      	at org.apache.pdfbox.pdmodel.PDDocument.loadNonSeq(PDDocument.java:1256)
      	at com.remion.bronto.proex.pdf.PDFBoxEncryptionTest.testEncryptDecrypt(PDFBoxEncryptionTest.java:30)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:606)
      	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
      	at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
      	at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
      	at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
      	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
      	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
      	at org.testng.TestRunner.privateRun(TestRunner.java:767)
      	at org.testng.TestRunner.run(TestRunner.java:617)
      	at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
      	at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
      	at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
      	at org.testng.SuiteRunner.run(SuiteRunner.java:240)
      	at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
      	at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
      	at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
      	at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
      	at org.testng.TestNG.run(TestNG.java:1057)
      	at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
      	at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
      	at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
      Caused by: javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
      	at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:750)
      	at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
      	at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
      	at javax.crypto.Cipher.doFinal(Cipher.java:1970)
      	at org.apache.pdfbox.pdmodel.encryption.SecurityHandler.encryptData(SecurityHandler.java:351)
      	... 35 more
      

      Opening the saved document with Acrobat works fine.

      [1] http://pdfbox.apache.org/1.8/cookbook/encryption.html

      Attachments

        1. testform_protected.pdf
          65 kB
          Samuli Saarinen

        Activity

          People

            tilman Tilman Hausherr
            ssaarinen Samuli Saarinen
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: