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.
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: Cannot process encrypted word file
It seems the key has not been set correctly.