Java – pdfbox: attempt to decrypt pdf

The following this answer I tried to use pdf@R_300_2419 @Decrypt PDF document:

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.");
    }

This leads to

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

The path is correct, so I don't know what happened Also, if I look at pddocument Decrypt (string PW) method, I found this: this will decrypt the document This method is provided only for compatibility reasons Users should use the new security layer, especially the openprotection method

What's the meaning of this? Someone can give an example of how to use it pdf@R_300_2419 @Decrypt PDF documents correctly?

Solution

See dependency list:

You need to use the bouncy castle library

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

In the current version (1.8.9), the decrypt () call is no longer used use

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

Replace

Additional suggestions: download the source code package You will find many examples that can help you further

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>