Java打开受密码保护的xslx文件会抛出EncryptedDocumentException

时间:2016-11-03 11:46:43

标签: java encryption apache-poi xlsx worksheet

我有excel 2013的xlsx文件,带有密码保护。 我有密码 - 我试图解密它。

public static Boolean isWorkbookPasswordValid(File file, String xlsWorkGroupPassword) throws IOException, GeneralSecurityException {
    InputStream is = null;
    FileOutputStream fileOut = null;
    Workbook wb;
    try {
        wb = WorkbookFactory.create(new FileInputStream(file), xlsWorkGroupPassword);
    } catch (Exception e1) {
        // ...
    }
}

并运行上面的代码我收到了这个错误:

org.apache.poi.EncryptedDocumentException: Export Restrictions in place - please install JCE Unlimited Strength Jurisdiction Policy files
    at org.apache.poi.poifs.crypt.CryptoFunctions.getCipher(CryptoFunctions.java:208)
    at org.apache.poi.poifs.crypt.CryptoFunctions.getCipher(CryptoFunctions.java:182)
    at org.apache.poi.poifs.crypt.agile.AgileDecryptor.hashInput(AgileDecryptor.java:269)
    at org.apache.poi.poifs.crypt.agile.AgileDecryptor.verifyPassword(AgileDecryptor.java:116)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:95)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:206)
    at 

阅读online

所以我从here下载了JCE 并将其复制到java home安全目录。

并试图再次运行 - 我得到了同样的错误。

我也尝试过这段代码,它给了我同样的例外。

POIFSFileSystem fs = new POIFSFileSystem(is);
EncryptionInfo info = new EncryptionInfo(fs);
Decryptor d = Decryptor.getInstance(info);
d.verifyPassword(xlsWorkGroupPassword)

添加这个属性并没有做任何事情:

Security.setProperty("crypto.policy", "unlimited");

据我所知,excel 2013通过以下参数加密文件:

keyBits="256",hashAlgorithm="SHA512" 

问题是keyBits。有没有人解决过这个问题?

0 个答案:

没有答案