Apache PDFBox - can't decrypt PDF

浪尽此生 提交于 2019-12-08 09:15:52

问题


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();
    }

}

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)

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


回答1:


You need the user password.

    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.

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".




回答2:


I have tested your code and it work's fine for me.

i am using

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


来源:https://stackoverflow.com/questions/26295553/apache-pdfbox-cant-decrypt-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!