Apache POI decrypt doc 文件无法处理加密文件?

Apache POI decrypt doc file cannot process encrypted file?

public static void decryptedDoc(String path,String[] password) throws FileNotFoundException, IOException{
  FileOutputStream fileOut = null;
  for(int i=0;i<password.length;i++){
//  try{
  Biff8EncryptionKey.setCurrentUserPassword(password[i]);
  NPOIFSFileSystem fs = new NPOIFSFileSystem( new FileInputStream(path));
  HWPFDocument doc=new HWPFDocument(fs.getRoot());
  Biff8EncryptionKey.setCurrentUserPassword(null);
  String neweachpath=path.substring(0, path.length()-4)+"_decrypted"+path.substring(path.length() -4);
  fileOut = new FileOutputStream(neweachpath);
  doc.write(fileOut);
  fileOut.flush(); 
  //}
 /* catch (EncryptedDocumentException e){
      System.out.println("wrong password for"+path+password[i]);
  }*/
  }

我想用这个代码来解密doc文件。

我从 Apache POI Encryption 引用了这段代码。它真正适用于 docx 和 xls、xlsx 文件。但是这里不行,即使密码正确也总是出现如下异常

org.apache.poi.EncryptedDocumentException: Cannot process encrypted word file

好像密钥没有设置正确

如您在问题中链接到的 Apache POI Encryption page 中的图表所示:

HWPF,处理 Word .doc 文件的 Apache POI 组件,不支持解密受密码保护的 .doc 文件。因此,如果您尝试(如您所做的那样)

,您将得到一个异常

正如 table 所解释的那样,所有基于 OOXML 的格式都以其加密/密码保护的形式受到支持,因为这些格式都使用一种通用的方式来存储受保护的内容。较旧的文件格式各自有自己的处理方式,这需要对每种格式进行独立的实现。支持在 HSSF 中用于 xls 的最常见类型,在 HSLF 中支持在 ppt 中使用的一种类型,但在 HWPF 中不支持 doc

如果这对您来说真的很重要,您需要通读 Microsoft 发布的文件格式文档以了解 .doc 文件如何加密,然后添加支持并将其回馈。 Apache POI - Contribution Guidelines page

中提供了相关链接