[
https://issues.apache.org/jira/browse/PDFBOX-2781?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14520947#comment-14520947
]
Tilman Hausherr commented on PDFBOX-2781:
-----------------------------------------
AES encryption with key length other than 256 bits is not supported, and AES256
is supported only in the unreleased 2.0 version that you can get with svn. See
the table in PDFBOX-1594 for what is supported.
The NPE you get is because you must set setAllSecurityToBeRemoved(true) first.
The next version will have a useful error message instead of an NPE, i.e. that
one was fixed recently (PDFBOX-2769) but only in 2.0 (I'll think about that
again, now that you mention it, and will see if it can be fixed in 1.8.*).
You can look at TestSymmetricKeyEncryption.java, this has a lot of testcases.
Please ask any "how to" questions in the mailing list.
> Opening pdf document after encrypting it with PDFBox throws
> IllegalBlockSizeException
> -------------------------------------------------------------------------------------
>
> Key: PDFBOX-2781
> URL: https://issues.apache.org/jira/browse/PDFBOX-2781
> Project: PDFBox
> Issue Type: Bug
> Components: PDModel, Writing
> Affects Versions: 1.8.9, 1.8.10, 2.0.0
> Environment: jdk 1.7.0_55, bouncy castle provider 1.52
> Reporter: Samuli Saarinen
> Assignee: Tilman Hausherr
> Fix For: 1.8.10, 2.0.0
>
> Attachments: testform_protected.pdf
>
>
> I'm trying to encrypt pdf document as per [1] like this:
> {code:java}
> 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);
> {code}
> It works fine but opening the encrypted document with PDFBox (last line of
> above code) throws:
> {noformat}
> 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
> {noformat}
> Opening the saved document with Acrobat works fine.
> [1] http://pdfbox.apache.org/1.8/cookbook/encryption.html
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]