Java 如何使用ApachePOI解密.doc/docx文件?

Java 如何使用ApachePOI解密.doc/docx文件?,java,eclipse,apache-poi,Java,Eclipse,Apache Poi,我正在尝试使用ApachePOI打开一个受密码保护的.doc文件。然而,我得到了错误 org.apache.poi.EncryptedDocumentException:无法处理加密的word文件 有人能帮我解决这个问题吗。 如果我能得到密码,我将不胜感激。一个EncryptedDocumentException信号表明您正试图处理一个以前未“解锁”的加密文档 以下代码段适用于检查基于XML的格式(.xlsx、.pptx、.docx,…)是否存在这种情况,以便您以后可以安全地处理它: Strin

我正在尝试使用ApachePOI打开一个受密码保护的.doc文件。然而,我得到了错误

org.apache.poi.EncryptedDocumentException:无法处理加密的word文件

有人能帮我解决这个问题吗。
如果我能得到密码,我将不胜感激。

一个
EncryptedDocumentException
信号表明您正试图处理一个以前未“解锁”的加密文档

以下代码段适用于检查基于XML的格式(.xlsx、.pptx、.docx,…)是否存在这种情况,以便您以后可以安全地处理它:

String password = "secret"; // set password
File fileToProcess; // obtain/read/open the file here....
NPOIFSFileSystem filesystem  = new NPOIFSFileSystem(fileToProcess);
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);

    // parse dataStream as the document is now processable from here on
    // ...

} catch (GeneralSecurityException ex) {
    throw new RuntimeException("Unable to process encrypted document", ex);
}
上述示例取自项目的JavaDoc,并根据该项目的JavaDoc进行了修改。您可能需要检查/读取类和/或的JavaDoc

如果要转换二进制文件格式(.xls、.ppt、.doc…),请查看加密部分以获取代码示例


希望有帮助。

一个
加密文档异常
表示您正试图处理一个以前未“解锁”的加密文档

以下代码段适用于检查基于XML的格式(.xlsx、.pptx、.docx,…)是否存在这种情况,以便您以后可以安全地处理它:

String password = "secret"; // set password
File fileToProcess; // obtain/read/open the file here....
NPOIFSFileSystem filesystem  = new NPOIFSFileSystem(fileToProcess);
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);

    // parse dataStream as the document is now processable from here on
    // ...

} catch (GeneralSecurityException ex) {
    throw new RuntimeException("Unable to process encrypted document", ex);
}
上述示例取自项目的JavaDoc,并根据该项目的JavaDoc进行了修改。您可能需要检查/读取类和/或的JavaDoc

如果要转换二进制文件格式(.xls、.ppt、.doc…),请查看加密部分以获取代码示例


希望有帮助。

因为最初的问题是关于解密二进制文件
*。doc
格式:

中的代码需要更新一点,以便与
HWPF
一起使用。无法从
NPOIFSFileSystem
创建。需要
poifsffilesystem
。但在其他方面也是一样的

使用密码“pass”对
file.doc
进行加密,在该密码运行后,新文件
fileDecrypted.doc
将被解密,无需密码即可打开

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ReadEncryptedHWPF {

 public static void main(String[] args) throws Exception {

  Biff8EncryptionKey.setCurrentUserPassword("pass"); 
  POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("file.doc")); 
  HWPFDocument doc = new HWPFDocument(fs);
  Biff8EncryptionKey.setCurrentUserPassword(null);
  doc.write(new FileOutputStream("fileDecrypted.doc"));
  doc.close();

  doc = new HWPFDocument(new FileInputStream("fileDecrypted.doc"));
  org.apache.poi.hwpf.extractor.WordExtractor extractor = new org.apache.poi.hwpf.extractor.WordExtractor(doc);
  System.out.println(extractor.getText());
  extractor.close();

 }
}

由于最初的问题是关于解密二进制文件
*.doc
格式:

中的代码需要更新一点,以便与
HWPF
一起使用。无法从
NPOIFSFileSystem
创建。需要
poifsffilesystem
。但在其他方面也是一样的

使用密码“pass”对
file.doc
进行加密,在该密码运行后,新文件
fileDecrypted.doc
将被解密,无需密码即可打开

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ReadEncryptedHWPF {

 public static void main(String[] args) throws Exception {

  Biff8EncryptionKey.setCurrentUserPassword("pass"); 
  POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("file.doc")); 
  HWPFDocument doc = new HWPFDocument(fs);
  Biff8EncryptionKey.setCurrentUserPassword(null);
  doc.write(new FileOutputStream("fileDecrypted.doc"));
  doc.close();

  doc = new HWPFDocument(new FileInputStream("fileDecrypted.doc"));
  org.apache.poi.hwpf.extractor.WordExtractor extractor = new org.apache.poi.hwpf.extractor.WordExtractor(doc);
  System.out.println(extractor.getText());
  extractor.close();

 }
}

@姆维斯纳:编辑问题直到它适合你的答案?@姆维斯纳:编辑问题直到它适合你的答案?