I'm trying to create an encrypted xlsx file using Apache POI. Here's my code, which runs just fine:
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();
}
The issue is when I open the generated file, excel keeps complaining that the file is corrupted.
I've also tried to change the EncryptionInfo
instantiation with different Encryption modes but nothing changes.
Can someone give me a hint?!?