2 回答
![?](http://img1.sycdn.imooc.com/545863aa00014aa802200220-100-100.jpg)
TA贡献1830条经验 获得超9个赞
解密Microsoft Office基于XML格式的基本代码以基于XML的格式-Decryption显示。
但是当然必须知道*.docx,它不能是WordOffice Open XML格式的文件,而应该是二进制文件格式HSSFWorkbook的Excel工作簿BIFF,而必须是XWPFDocument。
所以:
import java.io.InputStream;
import java.io.FileInputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.Decryptor;
import java.security.GeneralSecurityException;
public class ReadEncryptedXWPF {
static XWPFDocument decryptdocx(POIFSFileSystem filesystem, String password) throws Exception {
EncryptionInfo info = new EncryptionInfo(filesystem);
Decryptor d = Decryptor.getInstance(info);
try {
if (!d.verifyPassword(password)) {
throw new RuntimeException("Unable to process: document is encrypted");
}
InputStream dataStream = d.getDataStream(filesystem);
return new XWPFDocument(dataStream);
} catch (GeneralSecurityException ex) {
throw new RuntimeException("Unable to process encrypted document", ex);
}
}
public static void main(String[] args) throws Exception {
POIFSFileSystem filesystem = new POIFSFileSystem(new FileInputStream("abc.docx"));
XWPFDocument document = decryptdocx(filesystem, "user");
XWPFWordExtractor extractor = new XWPFWordExtractor(document);
System.out.println(extractor.getText());
extractor.close();
}
}
![?](http://img1.sycdn.imooc.com/533e4c5600017c5b02010200-100-100.jpg)
TA贡献1966条经验 获得超4个赞
我已经解决了。代码如下
POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("D:/abc.docx"));
EncryptionInfo info=new EncryptionInfo(fs);
Decryptor decryptor=Decryptor.getInstance(info);
XWPFDocument document=null;
if(decryptor.verifyPassword("password"))
{
InputStream dataStream = decryptor.getDataStream(fs);
document = new XWPFDocument(dataStream);
}else{
throw new Exception("file is protected with password...please open with right password");
}
File f=new File("D:/abc.docx");
FileOutputStream fos = new FileOutputStream(f);
document.write(fos);
document.close();
添加回答
举报