String fileName = "C:\\encryptedExcel.xlsm";
String password = "passcode!";
try {
// XOR/RC4 decryption for xls
Biff8EncryptionKey.setCurrentUserPassword(password);
NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(fileName), true);
HSSFWorkbook hwb = new HSSFWorkbook(fs.getRoot(), true);
catch(Exception e) {e.printStackTrace();System.err.println(e);}
//org.apache.poi.EncryptedDocumentException: The supplied spreadsheet seems to be an Encrypted .xlsx file. It must be decrypted before use by XSSF, it cannot be used by HSSF
try {
Biff8EncryptionKey.setCurrentUserPassword(password);
Workbook workbook = new XSSFWorkbook(OPCPackage.open(fileName,PackageAccess.READ));
Biff8EncryptionKey.setCurrentUserPassword(null);
catch(Exception e) {e.printStackTrace();System.err.println(e);}
//org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
//if (!bis.markSupported()) {is = new PushbackInputStream(bis, 8);}
if (POIFSFileSystem.hasPOIFSHeader(bis)) {
POIFSFileSystem fs = new POIFSFileSystem(bis);
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.getInstance(info);
d.verifyPassword(password);
//is = d.getDataStream(fs);
catch(Exception e) {e.printStackTrace();}
//throws java.io.IOException: getFileMagic() only operates on streams which support mark(int)
//fixed by converting to BufferedInputStream
//then throws org.apache.poi.EncryptedDocumentException: Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files