Mun*_*kin 9 java encryption pdfbox
根据这个答案,我正在尝试使用pdfbox解密pdf文档:
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.");
}
Run Code Online (Sandbox Code Playgroud)
这导致
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
...
Run Code Online (Sandbox Code Playgroud)
路径是正确的,所以我不知道发生了什么.此外,如果我查看PDDocument.decrypt(String pw)方法,我会发现: 这将解密文档.仅出于兼容性原因提供此方法.用户应该使用新的安全层,尤其是openProtection方法.
这是什么意思?有人可以举例说明如何使用pdfbox正确解密pdf文档吗?
Til*_*err 10
请参阅依赖关系列表:https: //pdfbox.apache.org/1.8/dependencies.html
您需要使用bouncycastle库.
<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>
Run Code Online (Sandbox Code Playgroud)
在当前版本(1.8.9)中确实不推荐使用decrypt()调用.使用
pd.openProtection(new StandardDecryptionMaterial(""));
Run Code Online (Sandbox Code Playgroud)
代替.
其他建议:下载源代码包.你会发现很多可以帮助你的例子.
要使用该openProtection方法,您必须提供一个实例DecryptionMaterial.在您的密码保护的情况下,它将是StandardDecryptionMaterial(来自API):
PDDocument doc = PDDocument.load(in);
StandardDecryptionMaterial dm = new StandardDecryptionMaterial("password");
doc.openProtection(dm);
Run Code Online (Sandbox Code Playgroud)
此外,您必须使用加密/签名来填充PDFBox的Bouncy Castle依赖项.请参阅https://pdfbox.apache.org/1.8/dependencies.html.
| 归档时间: |
|
| 查看次数: |
7864 次 |
| 最近记录: |