Discussion:
Question: Problem with Encryption in POI
A***@Instinet.com
2018-06-12 20:16:51 UTC
Permalink
Hello,

I have xlsx spreadsheet which does not have password. My goal is to read
it, add some entries and save as password protected in another (new) file.
After I run my program new file gets password set, but new entries are
absent from it, only original entries are there. Please help and point out
what I am doing wrong.
I use latest POI 3.17 jars. Below is simplified version of my program
reading empty spreadsheet.
Thanks

public static void main(String[] args) {

try {
POIFSFileSystem fs = new POIFSFileSystem();

EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("passw");


File is = new File("./empty.xlsx");
OPCPackage opc = OPCPackage.open(is, PackageAccess.READ_WRITE);

Workbook wb = WorkbookFactory.create(opc);

Sheet sheet = wb.getSheetAt(0);
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("CRYPT");

OutputStream encos = enc.getDataStream(fs);
opc.save(encos);
opc.close();

OutputStream fos = new FileOutputStream(new File("./f.xlsx"));
fs.writeFilesystem(fos);
fos.close();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}


----------------------------------------------------------------
Alex Broytman
========================================================================================================= <<<< Disclaimer >>>> This message, including all attachments, is private and confidential, may contain proprietary or privileged information and material and is intended solely for use by the named addressee(s). If you receive this transmission in error, please immediately notify the sender and destroy this message in its entirety, whether in electronic or hard copy format. Any unauthorized use (and reliance thereon), copying, disclosure, retention, or distribution of this transmission or the material herein is forbidden. We reserve the right to retain, monitor, intercept and archive electronic communications. This message does not constitute an offer or solicitation with respect to the purchase or sale of any security. It should not be construed to contain any recommendation regarding any security or strategy unless expressly stated therein. Any reference to the terms of executed transactions should be treated as preliminary only and subject to formal written confirmation. Any views expressed are those of the individual sender, except where the message states otherwise and the sender is authorized to state them to be the views of any such entity. This message is provided on an “as is” basis. It contains material that is owned by Instinet Incorporated, its subsidiaries or its or their licensors, and may not, in whole or in part, be (i) copied, photocopied or duplicated in any form, by any means, or (ii) redistributed, posted, published, excerpted, or quoted without Instinet Incorporated's prior written consent. No confidentiality or privilege is waived or lost by any mistransmission of this message. Instinet, LLC (member SIPC) and Instinet Canada Limited (member IIROC/CIPF) are subsidiaries of Instinet Incorporated that are locally registered or otherwise authorized to provide securities brokerage products and services. Please refer to the following links for additional disclosures and disclaimers that apply to this message. In the United States: http://instinet.com/docs/legal/le_disclaimers.html . In Canada: http://www.instinet.com/docs/legal/le_ca_disclosures.html . Effective July 1, 2014, Canada introduced Canadian Anti-Spam Legislation ("CASL"). As a Canadian resident you are receiving this electronic communication because of your existing relationship with Instinet Canada Limited or an authorized affiliate. Canadian residents who wish to unsubscribe from commercial electronic messages: please e-mail ***@instinet.com. Please note that you will continue to receive non-commercial electronic messages, such as account statements, invoices, client communications, and other similar factual electronic communications.

=========================================================================================================
pj.fanning
2018-06-12 21:29:28 UTC
Permalink
https://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/streaming/examples/SavePasswordProtectedXlsx.java?view=markup

This sample uses a custom SXSSF workbook but you could use a plain
XSSFWorkbook instead.
Basically you save the XSSFWorkbook to a file using write(OutputStream) and
the save method in the class above reads that file in and saves a new file
that is password protected.



--
Sent from: http://apache-poi.1045710.n5.nabble.com/POI-User-f2280730.html

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@poi.apache.org
For additional commands, e-mail: user-***@poi.apache.org
Andreas Beeker
2018-06-12 21:46:50 UTC
Permalink
Hi Alex,

you haven't closed the encos stream and its better to use the file directly with WorkbookFactory

The below worked for me.

Cheers, Andi


public static void main(String[] args) throws Exception {
EncryptionInfo info = new EncryptionInfo(EncryptionMode.standard);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("passw");

// create/save dummy .xlsx try (XSSFWorkbook wb2 = new XSSFWorkbook();
FileOutputStream fos = new FileOutputStream("empty.xlsx")) {
wb2.createSheet();
wb2.write(fos);
}

try (POIFSFileSystem fs = new POIFSFileSystem()) {
try (
Workbook wb = WorkbookFactory.create(new File("empty.xlsx"));
OutputStream encos = enc.getDataStream(fs);
) {
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellValue("CRYPT");

wb.write(encos);
}

try (OutputStream fos = new FileOutputStream(new File("f.xlsx"))) {
fs.writeFilesystem(fos);
}
}
}

Loading...