pdfbox:试图解密PDF [英] pdfbox: trying to decrypt PDF

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

问题描述

以下此答案,我正在尝试解密一个pdf文件与pdfbox:

Following this answer I'm trying to decrypt a pdf-document with pdfbox:

PDDocument pd = PDDocument.load(path);
if(pd.isEncrypted()){
    try {
        pd.decrypt("");
        pd.setAllSecurityToBeRemoved(true);
    } catch (Exception e) {
        throw new Exception("The document is encrypted, and we can't decrypt it.");
    }

这导致

Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
at org.apache.pdfbox.pdmodel.PDDocument.openProtection(PDDocument.java:1601)
at org.apache.pdfbox.pdmodel.PDDocument.decrypt(PDDocument.java:948)
...
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.jce.provider.BouncyCastleProvider
...

路径是正确的,所以我不知道发生了什么
此外,如果我看看PDDocument.decrypt(String pw)方法,我发现这个:
这将解密一个文档。仅为兼容性原因提供此方法。用户应该使用新的安全层,特别是openProtection方法。

The path is correct, so I don't know what's going on. Furthermore, if I have a look at the PDDocument.decrypt(String pw) method, I find this: This will decrypt a document. This method is provided for compatibility reasons only. User should use the new security layer instead and the openProtection method especially.

这是什么意思?有人可以举例说明如何使用pdfbox正确解密pdf文档?

What does it mean? Could someone give an example how to decrypt a pdf-document correctly with pdfbox?

推荐答案

请参阅依赖关系列表:
https://pdfbox.apache.org/1.8/dependencies.html

See the dependency list: https://pdfbox.apache.org/1.8/dependencies.html

您需要使用bouncycastle库。

You need to use the bouncycastle libraries.

<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcprov-jdk15</artifactId>
  <version>1.44</version>
</dependency>
<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcmail-jdk15</artifactId>
  <version>1.44</version>
</dependency>

在当前版本(1.8.9)中,decrypt()调用确实已被弃用。使用

the decrypt() call is indeed deprecated in the current version (1.8.9). Use

pd.openProtection(new StandardDecryptionMaterial(""));

其他建议:下载源代码包。你会发现很多例子可以帮助你进一步。

Additional advice: download the source code package. You'll find many examples that will help you further.

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

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