【问题标题】:Apache PDFBox - can't decrypt PDFApache PDFBox - 无法解密 PDF
【发布时间】:2014-10-10 08:56:15
【问题描述】:

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

package com.test;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial;
import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;

public class PdfEncDecTest {

    static String pdfPath = "G:\\files\\filed5b3.pdf";
    public final static String PDF_OWNER_PASSWORD = "cd1j";
    public final static String PDF_USER_PASSWORD = "";  

    public static void main(String[] args) throws Exception {

        PDDocument document = PDDocument.load(pdfPath);
        AccessPermission ap = new AccessPermission();
        ap.setCanPrint(true);
        ap.setCanExtractContent(false);
        ap.setCanExtractForAccessibility(false);
        StandardProtectionPolicy spp = new StandardProtectionPolicy(PDF_OWNER_PASSWORD, PDF_USER_PASSWORD, ap);
        document.protect(spp);
        document.save(pdfPath+".pdf");
        document.close();

        PDDocument doc = PDDocument.load(pdfPath+".pdf");
        if(doc.isEncrypted()) {
            StandardDecryptionMaterial sdm = new StandardDecryptionMaterial(PDF_OWNER_PASSWORD);
            doc.openProtection(sdm); // org.apache.pdfbox.exceptions.CryptographyException: Error: The supplied password does not match either the owner or user password in the document.
            doc.decrypt(PDF_OWNER_PASSWORD); // the same like above
        }
        doc.close();
    }

}

我不知道出了什么问题。对于 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:https://github.com/marioosh-net/pdfbox

【问题讨论】:

  • 请使用当前版本 (1.8.7) 重试。如果它仍然不起作用,请在您的问题中包含例外情况。
  • 我在 1.8.7 版本中遇到了同样的异常...

标签: java pdfbox


【解决方案1】:

您需要用户密码。

    if (doc.isEncrypted())
    {
        StandardDecryptionMaterial sdm = new StandardDecryptionMaterial(PDF_USER_PASSWORD);
        doc.openProtection(sdm);
        // don't call decrypt() here
    }

即使用户密码不为空,这也有效。用户密码是普通人认为的加密,所有者密码是安全权限的加密。

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

【讨论】:

  • 我只需要在 pdf 上设置安全权限,用户密码必须为空
  • 即便如此,您的代码仍然是错误的,您的文件是使用空密码进行用户加密的,因此您需要传递那个密码而不是当时的所有者密码。
  • 嗯...你是对的!我已经检查过了。非常非常感谢!
【解决方案2】:

我已经测试了你的代码,它对我来说很好。

我正在使用

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

【讨论】:

  • 我已经将示例项目放到了github:https://github.com/marioosh-net/pdfbox你可以查看。它对我不起作用:(
猜你喜欢
  • 1970-01-01
  • 2013-03-22
  • 2019-01-17
  • 2018-08-31
  • 1970-01-01
  • 2016-12-22
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多