Bug 62811

Summary: POI Encryption didn't work with 4.0.0 but did work with 3.17
Product: POI Reporter: John Kleiser <john.kleiser>
Component: POI OverallAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal CC: john.kleiser
Priority: P2    
Version: 4.0.0-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: Example of file produced.

Description John Kleiser 2018-10-09 13:54:46 UTC
Created attachment 36193 [details]
Example of file produced.

Using the code from https://poi.apache.org/encryption.html

POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
// EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile, CipherAlgorithm.aes192, HashAlgorithm.sha384, -1, -1, null);

Encryptor enc = info.getEncryptor();
enc.confirmPassword("foobaa");

// Read in an existing OOXML file
OPCPackage opc = OPCPackage.open(new File("..."), PackageAccess.READ_WRITE);
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();

// Write out the encrypted version
FileOutputStream fos = new FileOutputStream("...");
fs.writeFilesystem(fos);
fos.close();

This works fine with 3.17 but doesn't work with 4.0.0. It just creates a 2k file that Microsoft Excel won't open.
Comment 1 Andreas Beeker 2018-10-09 18:18:07 UTC
Before I try to reproduce it, please add that "os.close()" before calling "fs.writeFilesystem()".
Comment 2 John Kleiser 2018-10-09 19:33:40 UTC
Yes adding os.close() as below resolves the issue. Thank you.

        POIFSFileSystem fs = new POIFSFileSystem( );
        EncryptionInfo info = new EncryptionInfo( EncryptionMode.agile );
        // EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile,
        // CipherAlgorithm.aes192, HashAlgorithm.sha384, -1, -1, null);

        Encryptor enc = info.getEncryptor( );
        enc.confirmPassword( "foobar" );

        // Read in an existing OOXML file
        OPCPackage opc = OPCPackage.open( new File( "input.xlsx" ), PackageAccess.READ_WRITE );
        OutputStream os = enc.getDataStream( fs );
        opc.save( os );
        opc.close( );
        os.close( );

        // Write out the encrypted version
        FileOutputStream fos = new FileOutputStream( "output.xlsx" );
        fs.writeFilesystem( fos );
        fos.close( );
Comment 3 Andreas Beeker 2018-10-09 21:41:50 UTC
fixed the documentation via r1843348