为了账号安全,请及时绑定邮箱和手机立即绑定

Apache POI 加密的 xlsx 无法在 excel 中打开

Apache POI 加密的 xlsx 无法在 excel 中打开

长风秋雁 2021-09-29 17:44:41
我正在尝试使用 Apache POI 创建加密的 xlsx 文件。这是我的代码,它运行得很好:public static void Encrypt(String data) throws IOException, GeneralSecurityException, InvalidFormatException {    Workbook wb = new XSSFWorkbook();    Sheet sheet1 = wb.createSheet("sheet1");    sheet1.createRow(0).createCell(0).setCellValue(data);    ByteArrayOutputStream bos = new ByteArrayOutputStream();    wb.write(bos);    bos.close();    POIFSFileSystem fs = new POIFSFileSystem();    EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes256, HashAlgorithm.sha512, 256, 16, ChainingMode.cbc);    Encryptor enc = info.getEncryptor();    enc.confirmPassword("pass");    OPCPackage opc = OPCPackage.open(new ByteArrayInputStream(bos.toByteArray()));    OutputStream os = enc.getDataStream(fs);    opc.save(os);    opc.close();    FileOutputStream fos = new FileOutputStream("provawrite.xlsx");    fs.writeFilesystem(fos);    fos.close();}问题是当我打开生成的文件时,excel 一直抱怨文件已损坏。我还尝试EncryptionInfo使用不同的加密模式更改实例化,但没有任何变化。有人可以给我一个提示吗?!?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 279 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信