2015-07-28 170 views
1
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]); 
    }*/ 
    } 

我想利用這個代碼解密文檔文件。Apache POI解密doc文件無法處理加密文件?

我引用此代碼從Apache POI Encryption。它真正適用於docx和xls,xlsx文件。但它在這裏不起作用,即使密碼正確,也總是有以下例外。

org.apache.poi.EncryptedDocumentException:無法處理加密 word文件

看來鍵沒有正確設置。

+0

我在此期間實現瞭解密/加密 - 檢查[工廠類](https://stackoverflow.com/questions/45238486#45337641)以處理doc/docx – kiwiwings

回答

2

由於所涉及的圖中,你在你的問題聯繫到Apache POI Encryption page

HWPF,它處理的Word文件.doc在Apache POI組件,不支持解密密碼保護.doc文件。因此,如果您嘗試(如您所見),您將得到一個例外

正如表中所解釋的那樣,所有基於OOXML的格式都以加密/密碼保護的形式得到支持,因爲這些格式都使用常見的方式存儲受保護的內容。較舊的文件格式都有自己的做事方式,這要求每種格式都有獨立的實現。在HSSF中支持xls最常用的類型,用於HSLF中ppt中的一種,但HWPF中不支持doc

如果這真的對你很重要,你需要閱讀通過微軟發佈的文件格式的文檔制定.doc文件怎麼辦加密,然後加在支持和促進了回去。 Apache POI - Contribution Guidelines page

+0

thx很多。我會試着找出是否有其他的lib支持這個。 – printemp