Apache PDFBox - 无法解密 PDF [英] Apache PDFBox - can't decrypt PDF

查看:182
本文介绍了Apache PDFBox - 无法解密 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem with decrypting a PDF document with Apache PdfBox (v1.8.2) lib. Encryption works, but decryption with the same password throws an exception. (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 版,我得到了同样的例外.我已经在上面发布了完整的代码.

I don't know what is wrong. With version 1.8.7 I get the same exception. I've posted the full code above.

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

I've put sample project to github: https://github.com/marioosh-net/pdfbox

推荐答案

您需要用户密码.

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

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

this works even if the user password is not null. The user password is for what the ordinary human thinks encryption is, the owner password is an encryption for the security rights.

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

edit: sorry, my answer is wrong, although it was helpful. You can open a PDF with the user password (you'll possibly get restricted rights) or with the owner password (you'll get full rights). What may have happened is that there is a bug with matching the owner password with 40bit keys (which is the default). This bug is currently being investigated, see PDFBOX-2456 and search for "MD5".

这篇关于Apache PDFBox - 无法解密 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆