Apache POI 解密 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]);
}*/
}
我想用这个代码来解密文档文件.
I want to use this code to decrypt doc files.
我从 Apache POI 加密 中引用了此代码.它真正适用于 docx 和 xls、xlsx 文件.但是这里不起作用,即使密码正确也总是有以下异常.
I referenced this code from Apache POI Encryption . It truly works for docx and xls, xlsx files. But it does not work here, always has the following exception even when the password is right.
org.apache.poi.EncryptedDocumentException:无法处理加密word文件
org.apache.poi.EncryptedDocumentException: Cannot process encrypted word file
似乎没有正确设置密钥.
It seems the key has not been set correctly.
推荐答案
如 Apache 中的图表所述您在问题中链接到的 POI 加密页面:
HWPF 是处理 Word .doc
文件的 Apache POI 组件,不支持解密受密码保护的 .doc
文件.因此,如果您尝试(如您所愿),您将得到一个异常
HWPF, the Apache POI component which handles Word .doc
files, does not support decrypting password protected .doc
files. As such, you will get an Exception if you try (as you have)
如该表所述,所有基于 OOXML 的格式都受其加密/密码保护形式的支持,因为它们都使用一种通用方式来存储受保护的内容.较旧的文件格式都有自己的处理方式,这需要每种格式都有独立的实现.支持 HSSF 中用于 xls
的最常见种类,支持 HSLF 中 ppt
中使用的种类之一,但不支持 doc
HWPF.
As the table explains, all of the OOXML-based formats are supported in their encrypted / password protected form, as those all use a common way of storing the protected content. The older file formats each have their own way of doing things, which requires independent implementations for each format. There's support for the most common kind used for xls
in HSSF, for one of the kinds used in ppt
in HSLF, but no doc
support in HWPF.
如果这对您来说真的很重要,您需要通读 Microsoft 发布的文件格式文档以了解 .doc 文件如何进行加密,然后添加支持并将其回馈.Apache POI - 贡献指南页面
If this really matters to you, you'll need to read through the Microsoft published file format documentation to work out how .doc files do encryption, then add in the support and contribute it back. Links for that are available in the Apache POI - Contribution Guidelines page
这篇关于Apache POI 解密 doc 文件无法处理加密文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!