View Javadoc
1   package edu.jiangxin.apktoolbox.file.password.recovery.checker;
2   
3   import edu.jiangxin.apktoolbox.file.password.recovery.exception.UnknownException;
4   import org.apache.commons.io.IOUtils;
5   import org.apache.pdfbox.Loader;
6   import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
7   import org.apache.pdfbox.pdmodel.PDDocument;
8   import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
9   
10  import java.io.IOException;
11  
12  /**
13   * Brief introduction to PDF password
14   * The standard security provided by PDF has two different passwords: user passwords and owner passwords.
15   * A PDF document may be protected by a password for opening (user password)
16   * and the document may also specify operations that should be restricted even when the document is decrypted:
17   * printing; copying text and graphics out of the document; modifying the document;
18   * and adding or modifying text notes (using owner password).
19   */
20  public class PdfChecker extends FileChecker {
21  
22      public PdfChecker() {
23          super();
24      }
25  
26      @Override
27      public String[] getFileExtensions() {
28          return new String[]{"pdf"};
29      }
30  
31      @Override
32      public String getFileDescription() {
33          return "*.pdf";
34      }
35  
36      @Override
37      public String getDescription() {
38          return "PDF Checker";
39      }
40  
41      @Override
42      public boolean prepareChecker() {
43          return true;
44      }
45  
46      @Override
47      public boolean checkPassword(String password) {
48          boolean result = false;
49          PDDocument pdDocument = null;
50          try {
51              pdDocument = Loader.loadPDF(new RandomAccessReadBufferedFile(file), password);
52              result = true;
53          } catch (InvalidPasswordException e) {
54              logger.debug("[InvalidPasswordException]password is incorrect: {}", password);
55          } catch (IOException e) {
56              throw new UnknownException(e);
57          } finally {
58              if (pdDocument != null) {
59                  IOUtils.closeQuietly(pdDocument);
60              }
61          }
62          return result;
63      }
64  }