Java apachepdfbox-can';t解密PDF

Java apachepdfbox-can';t解密PDF,java,pdfbox,Java,Pdfbox,我在使用ApachePDFBox(v1.8.2)lib解密PDF文档时遇到问题。加密有效,但使用相同密码解密会引发异常。(Java 1.6) 我不知道怎么了。对于版本1.8.7,我得到了相同的异常。我已经发布了上面的完整代码 Exception in thread "main" org.apache.pdfbox.exceptions.CryptographyException: Error: The supplied password does not match either the own

我在使用ApachePDFBox(v1.8.2)lib解密PDF文档时遇到问题。加密有效,但使用相同密码解密会引发异常。(Java 1.6)

我不知道怎么了。对于版本1.8.7,我得到了相同的异常。我已经发布了上面的完整代码

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.prepareForDecryption(StandardSecurityHandler.java:265)
    at org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.decryptDocument(StandardSecurityHandler.java:156)
    at org.apache.pdfbox.pdmodel.PDDocument.openProtection(PDDocument.java:1595)
    at org.apache.pdfbox.pdmodel.PDDocument.decrypt(PDDocument.java:942)
    at com.test.PdfEncDecTest.main(PdfEncDecTest.java:29)

我已经将示例项目放到github上:

我已经测试了您的代码,它对我来说运行良好

我正在使用

<dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>1.8.7</version>
</dependency>

org.apache.pdfbox
pdfbox
1.8.7

您需要用户密码

    if (doc.isEncrypted())
    {
        StandardDecryptionMaterial sdm = new StandardDecryptionMaterial(PDF_USER_PASSWORD);
        doc.openProtection(sdm);
        // don't call decrypt() here
    }
即使用户密码不为空,也可以使用此选项。用户密码是普通人认为的加密,所有者密码是对安全权利的加密

编辑:对不起,我的答案是错误的,虽然它是有帮助的。您可以使用用户密码(您可能获得受限权限)或所有者密码(您将获得完全权限)打开PDF。可能发生的情况是,将所有者密码与40位密钥(即
默认值)。当前正在调查此错误,请参阅并搜索“MD5”。

请使用当前版本(1.8.7)重试。如果仍然不起作用,请在您的问题中包含异常。我在1.8.7版本中得到了相同的异常…我已将示例项目放到github:
https://github.com/marioosh-net/pdfbox
您可以检查。这对我不起作用:(我只需要在pdf上设置安全权限,用户密码必须为空。即使这样,你的代码仍然错误,你的文件是用户用空密码加密的,所以你需要传递该密码,而不是当时的所有者密码。嗯……你是对的!我已经检查过了。非常感谢!
    if (doc.isEncrypted())
    {
        StandardDecryptionMaterial sdm = new StandardDecryptionMaterial(PDF_USER_PASSWORD);
        doc.openProtection(sdm);
        // don't call decrypt() here
    }