2

I have tried the below code,

import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("D://protectedfile.xlsx"));
    EncryptionInfo info = new EncryptionInfo(fs);
    Decryptor d = new Decryptor(info); //Error
    d.verifyPassword(Decryptor.DEFAULT_PASSWORD);

It throws an error compilation error : Cannot instantiate the type Decryptor

But eventually this method will need me to copy and create new workbook in which i can read the data.

  1. Why i'm not able to instantiate Decryptor?
  2. Is there any other way than this, so that i can simply read the password protected excel file without creating a copy of it?

Note : I have looked at this post reading excel file, but doesn't help my exact situation

Community
  • 1
  • 1
Vignesh Paramasivam
  • 2,360
  • 5
  • 26
  • 57
  • Have you made sure you have all of the [required POI jars](http://poi.apache.org/overview.html#components) on your classpath? – Gagravarr Sep 23 '14 at 12:55
  • Yes i have all the jars required to read excel, here im not able to read only the password protected one. And also i have imported `import org.apache.poi.poifs.crypt.Decryptor;` it make sures the jar is present otherwise i wud have got compilation error – Vignesh Paramasivam Sep 24 '14 at 06:10
  • Have you made sure you're using the latest version of Apache POI? And that you have the same jars at runtime that you had at compile time? – Gagravarr Sep 24 '14 at 09:03
  • what do you mean by `the same jars at runtime that you had at compile time`? how to check it – Vignesh Paramasivam Sep 24 '14 at 09:05
  • @Gagravarr Is there any other way than this, so that i can simply read the password protected excel file without creating a copy of it? – Vignesh Paramasivam Sep 24 '14 at 09:06
  • You need to fix your build and runtime setup, the code you've given should work fine. My best guess is that you're not using the jars you think you are when your code runs. Just because you had the right jar when you compiled, doesn't mean it's the same/right one when the code later runs! – Gagravarr Sep 24 '14 at 09:12
  • i'm getting the error in compile time only not in runtime – Vignesh Paramasivam Sep 24 '14 at 09:15

1 Answers1

2

Ah, I've spotted your problem. It's this line:

Decryptor d = new Decryptor(info);

As shown in the Apache POI Encryption documentation, that line needs to be

Decryptor d = Decryptor.getInstance(info);

You'd be well advised to review the POI docs on encryption, and also make sure you're using the latest version of Apache POI (3.11 beta 2 as of writing)

Additionally, opening a File from an InputStream isn't recommended, as per the documentation, as it's slower and higher memory (everything has to get buffered). Instead, your code should really be:

NPOIFSFileSystem fs = new NPOIFSFileSystem(new File("D://protectedfile.xlsx"));
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.getInstance(info);
if (d.verifyPassword("password")) {
   XSSFWorkbook wb = new XSSFWorkbook(d.getDataStream(fs));
} else {
   // Password is wrong
}

Finally, get the decrypted data, and pass that to XSSFWorkbook to read the encrypted workbook

Gagravarr
  • 47,320
  • 10
  • 111
  • 156