【发布时间】:2020-06-09 09:30:03
【问题描述】:
希望大家在疫情期间身体健康,
我可以成功加密 Excel 文件,但无法解密。 我需要帮助在 Android 中解密 excel 文件。我正在使用 apache poi 库。 我不知道我缺哪里了。
密码就是密码
使用 encryptXLSX 函数加密的文件是:https://drive.google.com/file/d/1eQV62uFWj5Tg6g7gSmP61mNk_Ta5dMHq/view?usp=sharing
加密代码为:
public void encryptXLSX()
throws IOException, GeneralSecurityException, InvalidFormatException {
// input is unprotected excel named as excel.xlsx
String input = "/storage/emulated/0/Android/data/com.protect.excel_protect_example/files/excel.xlsx";
/// output file path
String outputPath = "/storage/emulated/0/Android/data/com.protect.excel_protect_example/files/protected_excel.xlsx";
POIFSFileSystem fs = new POIFSFileSystem();
Biff8EncryptionKey.setCurrentUserPassword("password");
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("password");
InputStream fis = new FileInputStream(input);
try (OPCPackage opc = OPCPackage.open(fis); OutputStream os = enc.getDataStream(fs)) {
opc.save(os);
os.close();
}
FileOutputStream fos1 = new FileOutputStream(outputPath);
fs.writeFilesystem(fos1);
fos1.close();
fs.close();
fis.close();
}
解密代码为:
public boolean isEncrypted(String path) {
try {
try {
new POIFSFileSystem(new FileInputStream(path));
} catch (IOException ignored) {
}
System.out.println("protected");
return true;
} catch (OfficeXmlFileException e) {
System.out.println("not protected");
return false;
}
}
public byte[] decryptXLSX() throws Exception {
String sourcepath = "/storage/emulated/0/Android/data/com.protect.excel_protect_example/files/protected_excel.xlsx";
InputStream in = null;
FileInputStream fis = new FileInputStream(sourcepath);
if (isEncrypted(sourcepath)) {
org.apache.poi.hssf.record.crypto.Biff8EncryptionKey.setCurrentUserPassword(password);
POIFSFileSystem filesystem = new POIFSFileSystem(fis);
print("Header Block:" + filesystem.getHeaderBlock().toString());
print("property tables:" + filesystem.getRoot().getEntries().toString());
EncryptionInfo info = new EncryptionInfo(filesystem);
//EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
//EncryptionInfo info = new EncryptionInfo(filesystem.getRoot().createDocumentInputStream("EncryptionInfo"), EncryptionMode.agile);
//EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes256, HashAlgorithm.sha512, 256, 16, ChainingMode.cbc);
Decryptor d = Decryptor.getInstance(info);
if (!d.verifyPassword("password")) {
print("Wrong password");
} else {
print("Good!");
}
in = d.getDataStream(filesystem);
} else {
in = new FileInputStream(sourcepath);
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] byteArray = buffer.toByteArray();
FileOutputStream fos1 = new FileOutputStream(
"/storage/emulated/0/Android/data/com.protect.excel_protect_example/files/Unprotected_excel.xlsx");
fos1.write(byteArray);
fos1.close();
return byteArray;
}
错误:
org.apache.poi.EncryptedDocumentException: Unable to parse
encryption descriptor
[ ] W/System.err(28825): at
org.apache.poi.poifs.crypt.agile.AgileEncryptionInfoBuilder.parseDescriptor(AgileEncryptionInfoBuilder.java:106)
[ ] W/System.err(28825): at org.apache.poi.poifs.crypt.agile.AgileEncryptionInfoBuilder.initialize(AgileEncryptionInfoBuilder.java:40)
[ ] W/System.err(28825): at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:152)
[ ] W/System.err(28825): at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:101)
[ +3 ms] W/System.err(28825): at org.apache.poi.poifs.crypt.EncryptionInfo.<init>(EncryptionInfo.java:94)
[ ] W/System.err(28825): at com.protect.excel_protect.decryptXLSX(ExcelProtect.java:182)
ExcelProtect.java Line 182中解密函数的问题:
EncryptionInfo info = new EncryptionInfo(filesystem);
【问题讨论】:
-
不管是excel文件还是其他类型的文件。此外,我们看不到 -excel- 文件开头。
-
你没有展示你如何调用decryptXLSX(),也没有展示为什么它应该有一个byte[]参数,也没有展示它的内容。
-
我已经用受加密功能保护且无法使用解密功能解密的代码和文件更新了问题。
-
我能够在桌面目录上的核心 java 文件上运行和解密文件,即在 Android 项目之外,但在 Android 项目中我遇到了上述问题。我一直在尝试从昨天晚上更改 EncryptionInfo 但无法弄清楚,帮助将节省我的一天兄弟
-
InputStream fis 未被 fs 使用,也未被写入 fos1。因此,据我所知,您的输出文件与输入文件无关。
标签: java android excel encryption apache-poi