View Javadoc
1   package edu.jiangxin.apktoolbox.file.password.recovery.checker;
2   
3   import org.apache.commons.io.FilenameUtils;
4   import org.apache.poi.EncryptedDocumentException;
5   import org.apache.poi.POIDocument;
6   import org.apache.poi.hslf.usermodel.HSLFSlideShow;
7   import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
8   import org.apache.poi.hssf.usermodel.HSSFWorkbook;
9   import org.apache.poi.hwpf.HWPFDocument;
10  import org.apache.poi.poifs.filesystem.POIFSFileSystem;
11  
12  import java.io.*;
13  
14  public class BinaryOfficeChecker extends FileChecker {
15      private static final boolean DEBUG = true;
16  
17      public BinaryOfficeChecker() {
18          super();
19      }
20  
21      @Override
22      public String[] getFileExtensions() {
23          return new String[]{"doc", "ppt", "xls"};
24      }
25  
26      @Override
27      public String getFileDescription() {
28          return "*.doc;*.ppt;*.xls";
29      }
30  
31      @Override
32      public String getDescription() {
33          return "Office File Checker(Binary formats)";
34      }
35  
36      @Override
37      public boolean prepareChecker() {
38          return true;
39      }
40  
41      @Override
42      public boolean checkPassword(String password) {
43          if (DEBUG) {
44              logger.info("checkPassword: " + password);
45          }
46  
47          boolean result = false;
48          Biff8EncryptionKey.setCurrentUserPassword(password);
49          try (FileInputStream fis = new FileInputStream(file);
50               POIFSFileSystem pfs = new POIFSFileSystem(fis)) {
51              String extension = FilenameUtils.getExtension(file.getName());
52              switch (extension) {
53                  case "xls" -> {
54                      try (POIDocument poiDocument = new HSSFWorkbook(pfs)) {
55                          logger.info("create workbook successfully: {}", poiDocument);
56                          result = true;
57                      }
58                  }
59                  case "doc" -> {
60                      try (POIDocument poiDocument = new HWPFDocument(pfs)) {
61                          logger.info("create document successfully: {}", poiDocument);
62                          result = true;
63                      }
64                  }
65                  case "ppt" -> {
66                      try (POIDocument poiDocument = new HSLFSlideShow(pfs)) {
67                          logger.info("create slideShow successfully: {}", poiDocument);
68                          result = true;
69                      }
70                  }
71                  default -> logger.error("Not supported: {}", file.getName());
72              }
73          } catch (FileNotFoundException e) {
74              logger.error("checkPassword FileNotFoundException");
75          } catch (IOException e) {
76              logger.error("checkPassword IOException");
77          } catch (EncryptedDocumentException e) {
78              logger.error("checkPassword EncryptedDocumentException");
79          }
80          return result;
81      }
82  }
83