Bug 60363

Summary: Missing Implementation for Encryption of XSSF Workbooks
Product: POI Reporter: Mathis <smear1>
Component: POIFSAssignee: POI Developers List <dev>
Status: RESOLVED INVALID    
Severity: blocker CC: smear1
Priority: P2    
Version: 3.14-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   

Description Mathis 2016-11-11 10:05:42 UTC
To create a password protected workbook using Encryptor, an instance of POIFSFileSystem is required to create the encrypted data stream. There is no XSSF implementation of POIFSFileSystem and the encryption is therefore not possible. The following example illustrates the problem, an exception occures in the line where the POIFSFileSystem is created.

 public static void main(String[] args)
    {
        try(Workbook wb = new XSSFWorkbook())
        {
            Sheet sheet = wb.createSheet();
            Row r = sheet.createRow(0);
            Cell cell = r.createCell(0);
            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellValue("Test");
            try(ByteArrayOutputStream baos = new ByteArrayOutputStream())
            {
                wb.write(baos);
                byte[] res = baos.toByteArray();
                try(ByteArrayInputStream bais = new ByteArrayInputStream(res))
                {
                    try(POIFSFileSystem fileSystem = new POIFSFileSystem(bais);) // Exception here
                    {
                        EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
                        Encryptor enc = info.getEncryptor();
                        enc.confirmPassword("pass");
                        OutputStream encryptedDS = enc.getDataStream(fileSystem);
                        OPCPackage opc = OPCPackage.open(new File("example.xlsx"), PackageAccess.READ_WRITE);
                        opc.save(encryptedDS);
                        opc.close();
                    }
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
Comment 1 Mathis 2016-11-11 10:07:27 UTC
Please note: There is no example.xlsx, but this line is not reached anyway.
Comment 2 Nick Burch 2016-11-11 10:11:25 UTC
You don't open the POIFS with the document to be encrypted, you start with a new empty one and populate it. Working code shown at https://poi.apache.org/encryption.html#XML-based+formats+-+Encryption
Comment 3 Mathis 2016-11-11 14:05:05 UTC
Thank you very much for you help and sorry for the unnecessary ticket!