You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Nandu <su...@gmail.com> on 2014/09/13 13:38:21 UTC

Encrypt xlsx file with POI3.9

Hi,'

I have a requirement of writing data to xlsx encrypted(protected file with
password) file and I have used the following code snippet from apache to do
the same
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
// EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile,
CipherAlgorithm.aes192, HashAlgorithm.sha384, -1, -1, null);

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

OPCPackage opc = OPCPackage.open(new File("..."), PackageAccess.READ_WRITE);
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();

FileOutputStream fos = new FileOutputStream("...");
fs.writeFilesystem(fos);
fos.close();   

I got the error at EncryptionInfo class for EncryptionMode.agile. Iam using
POI3.9. Could anyone please help



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Encrypt xlsx file with POI3.9

Posted by Nandu <su...@gmail.com>.
I am using the same code as is from http://poi.apache.org/encryption.html
by passing the existing file in my local.
Please find below the stacktrace.

Requirement in my project is to create a password protected xlsx file thru
the code and user should be able to download the encrypted file instead of
plain file. Please let me know if there is any other way to achieve this
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to
save: an error occurs while saving the package : null
at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:500)
at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1417)
at com.cisco.hr.aims.offline.EncryptExcel.main(EncryptExcel.java:37)
Caused by: java.lang.NullPointerException
at
org.apache.poi.poifs.filesystem.DirectoryNode.createDocument(DirectoryNode.java:219)
at
org.apache.poi.poifs.filesystem.DirectoryNode.createDocument(DirectoryNode.java:415)
at
org.apache.poi.poifs.crypt.DataSpaceMapUtils.createEncryptionEntry(DataSpaceMapUtils.java:72)
at
org.apache.poi.poifs.crypt.DataSpaceMapUtils.addDefaultDataSpace(DataSpaceMapUtils.java:42)
at
org.apache.poi.poifs.crypt.agile.AgileEncryptor$ChunkedCipherOutputStream.writeToPOIFS(AgileEncryptor.java:343)
at
org.apache.poi.poifs.crypt.agile.AgileEncryptor$ChunkedCipherOutputStream.close(AgileEncryptor.java:339)
at java.util.zip.DeflaterOutputStream.close(DeflaterOutputStream.java:149)
at java.util.zip.ZipOutputStream.close(ZipOutputStream.java:310)
at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:498)
... 2 more

Thanks,
Sunanda

On Mon, Sep 15, 2014 at 5:16 AM, kiwiwings [via Apache POI] <
ml-node+s1045710n5716621h39@n5.nabble.com> wrote:

>
> > at the line  opc.save(os);
> > org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to
> > save: an error occurs while saving the package : null
>
> I have seen this error before, but I can't reproduce it (by using the
> trunk version).
> Are you using the example as-is (of course the example assumes, you have
> an existing OPC file ...)?
> -> if not, please post your code.
>
> Please also post the stacktrace, i.e. the cause for the exception in the
> zip-package.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> <http://user/SendEmail.jtp?type=node&node=5716621&i=0>
> For additional commands, e-mail: [hidden email]
> <http://user/SendEmail.jtp?type=node&node=5716621&i=1>
>
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716621.html
>  To unsubscribe from Encrypt xlsx file with POI3.9, click here
> <http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5716609&code=c3VuYW5kYS5yYW5pQGdtYWlsLmNvbXw1NzE2NjA5fDE2NzE5OTY3NDU=>
> .
> NAML
> <http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716622.html
Sent from the POI - User mailing list archive at Nabble.com.

Re: Encrypt xlsx file with POI3.9

Posted by Nandu <su...@gmail.com>.
This works by adding inputStream = new FileInputStream(filename); at the
end of encryption.

On Mon, Sep 15, 2014 at 9:18 PM, Sunanda Rani <su...@gmail.com>
wrote:

> Thanks,. Another quick help needed on the same
> My application is strut2 so, I generate inputstream in action class and
> pass inputstream and filename to struts2.
> I will not have the file as such in action class. How do I protect xlsx in
> this scenario.  I can't use the code mentioned earlier as it expects the
> file to be present.
> my struts.xml sample
>
> <action name="download" class="com.developer.am.action.DownloadAction">
> <result name="success" type="stream">
> <param name="contentType">
> application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</param>
> <param name="inputName">fileInputStream</param>
> <param name="contentDisposition">attachment;filename="${filename}"</param>
> <param name="bufferSize">1024</param>
> </result>
> </action>
>
> Thanks
>
> On Mon, Sep 15, 2014 at 8:50 PM, kiwiwings [via Apache POI] <
> ml-node+s1045710n5716627h25@n5.nabble.com> wrote:
>
>>  Nandu wrote
>> It worked when I used POIFSFileSystem fs = new POIFSFileSystem();
>> Earlier i tried using NPOIFSFileSystem which was throwing error.
>> Thanks a lot for immediate help.
>>
>> There were a few fixes in version 3.11-beta1 (around #56437, which I've
>> tested with encryption).
>> What happens when you are using a recent POI version + NPOIFS?
>> (I'll try it tonight again with the trunk and NPOIFS ...)
>>
>> ------------------------------
>>  If you reply to this email, your message will be added to the
>> discussion below:
>>
>> http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716627.html
>>  To unsubscribe from Encrypt xlsx file with POI3.9, click here
>> <http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5716609&code=c3VuYW5kYS5yYW5pQGdtYWlsLmNvbXw1NzE2NjA5fDE2NzE5OTY3NDU=>
>> .
>> NAML
>> <http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>




--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716634.html
Sent from the POI - User mailing list archive at Nabble.com.

Re: Encrypt xlsx file with POI3.9

Posted by Nandu <su...@gmail.com>.
Thanks,. Another quick help needed on the same
My application is strut2 so, I generate inputstream in action class and
pass inputstream and filename to struts2.
I will not have the file as such in action class. How do I protect xlsx in
this scenario.  I can't use the code mentioned earlier as it expects the
file to be present.
my struts.xml sample

<action name="download" class="com.developer.am.action.DownloadAction">
<result name="success" type="stream">
<param name="contentType">
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename="${filename}"</param>
<param name="bufferSize">1024</param>
</result>
</action>

Thanks

On Mon, Sep 15, 2014 at 8:50 PM, kiwiwings [via Apache POI] <
ml-node+s1045710n5716627h25@n5.nabble.com> wrote:

>  Nandu wrote
> It worked when I used POIFSFileSystem fs = new POIFSFileSystem();
> Earlier i tried using NPOIFSFileSystem which was throwing error.
> Thanks a lot for immediate help.
>
> There were a few fixes in version 3.11-beta1 (around #56437, which I've
> tested with encryption).
> What happens when you are using a recent POI version + NPOIFS?
> (I'll try it tonight again with the trunk and NPOIFS ...)
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716627.html
>  To unsubscribe from Encrypt xlsx file with POI3.9, click here
> <http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5716609&code=c3VuYW5kYS5yYW5pQGdtYWlsLmNvbXw1NzE2NjA5fDE2NzE5OTY3NDU=>
> .
> NAML
> <http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716628.html
Sent from the POI - User mailing list archive at Nabble.com.

Re: Encrypt xlsx file with POI3.9

Posted by kiwiwings <ki...@apache.org>.
Nandu wrote
> It worked when I used POIFSFileSystem fs = new POIFSFileSystem();
> Earlier i tried using NPOIFSFileSystem which was throwing error.
> Thanks a lot for immediate help.

There were a few fixes in version 3.11-beta1 (around #56437, which I've
tested with encryption).
What happens when you are using a recent POI version + NPOIFS?
(I'll try it tonight again with the trunk and NPOIFS ...)



--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716627.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Encrypt xlsx file with POI3.9

Posted by Nandu <su...@gmail.com>.
It worked when I used POIFSFileSystem fs = new POIFSFileSystem();
Earlier i tried using NPOIFSFileSystem which was throwing error.
Thanks a lot for immediate help.

On Mon, Sep 15, 2014 at 10:03 AM, Sunanda Rani <su...@gmail.com>
wrote:

> I am using the same code as is from http://poi.apache.org/encryption.html
> by passing the existing file in my local.
> Please find below the stacktrace.
>
> Requirement in my project is to create a password protected xlsx file thru
> the code and user should be able to download the encrypted file instead of
> plain file. Please let me know if there is any other way to achieve this
> org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to
> save: an error occurs while saving the package : null
> at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:500)
> at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1417)
> at com.cisco.hr.aims.offline.EncryptExcel.main(EncryptExcel.java:37)
> Caused by: java.lang.NullPointerException
> at
> org.apache.poi.poifs.filesystem.DirectoryNode.createDocument(DirectoryNode.java:219)
> at
> org.apache.poi.poifs.filesystem.DirectoryNode.createDocument(DirectoryNode.java:415)
> at
> org.apache.poi.poifs.crypt.DataSpaceMapUtils.createEncryptionEntry(DataSpaceMapUtils.java:72)
> at
> org.apache.poi.poifs.crypt.DataSpaceMapUtils.addDefaultDataSpace(DataSpaceMapUtils.java:42)
> at
> org.apache.poi.poifs.crypt.agile.AgileEncryptor$ChunkedCipherOutputStream.writeToPOIFS(AgileEncryptor.java:343)
> at
> org.apache.poi.poifs.crypt.agile.AgileEncryptor$ChunkedCipherOutputStream.close(AgileEncryptor.java:339)
> at java.util.zip.DeflaterOutputStream.close(DeflaterOutputStream.java:149)
> at java.util.zip.ZipOutputStream.close(ZipOutputStream.java:310)
> at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:498)
> ... 2 more
>
> Thanks,
> Sunanda
>
> On Mon, Sep 15, 2014 at 5:16 AM, kiwiwings [via Apache POI] <
> ml-node+s1045710n5716621h39@n5.nabble.com> wrote:
>
>>
>> > at the line  opc.save(os);
>> > org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to
>> > save: an error occurs while saving the package : null
>>
>> I have seen this error before, but I can't reproduce it (by using the
>> trunk version).
>> Are you using the example as-is (of course the example assumes, you have
>> an existing OPC file ...)?
>> -> if not, please post your code.
>>
>> Please also post the stacktrace, i.e. the cause for the exception in the
>> zip-package.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> <http://user/SendEmail.jtp?type=node&node=5716621&i=0>
>> For additional commands, e-mail: [hidden email]
>> <http://user/SendEmail.jtp?type=node&node=5716621&i=1>
>>
>>
>>
>> ------------------------------
>>  If you reply to this email, your message will be added to the
>> discussion below:
>>
>> http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716621.html
>>  To unsubscribe from Encrypt xlsx file with POI3.9, click here
>> <http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5716609&code=c3VuYW5kYS5yYW5pQGdtYWlsLmNvbXw1NzE2NjA5fDE2NzE5OTY3NDU=>
>> .
>> NAML
>> <http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>




--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716624.html
Sent from the POI - User mailing list archive at Nabble.com.

Re: Encrypt xlsx file with POI3.9

Posted by Andreas Beeker <an...@gmx.de>.
> at the line  opc.save(os);
> org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to
> save: an error occurs while saving the package : null

I have seen this error before, but I can't reproduce it (by using the trunk version).
Are you using the example as-is (of course the example assumes, you have an existing OPC file ...)?
-> if not, please post your code.

Please also post the stacktrace, i.e. the cause for the exception in the zip-package.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: Encrypt xlsx file with POI3.9

Posted by Nandu <su...@gmail.com>.
Thanks for the quick response. I have tried with 3.10 final libraries now
and getting the below error when I try to encrypt the xlsx
at the line  opc.save(os);
org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to
save: an error occurs while saving the package : null

Appreciate your help

Thanks again

On Sat, Sep 13, 2014 at 10:21 PM, kiwiwings [via Apache POI] <
ml-node+s1045710n5716610h98@n5.nabble.com> wrote:

> Hi,
>
> I've added encryption support with POI 3.10-Final (
> http://poi.apache.org/changes.html - #55818)
>
> Is there a restriction to stay on 3.9?
>
> Andi
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> <http://user/SendEmail.jtp?type=node&node=5716610&i=0>
> For additional commands, e-mail: [hidden email]
> <http://user/SendEmail.jtp?type=node&node=5716610&i=1>
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716610.html
>  To unsubscribe from Encrypt xlsx file with POI3.9, click here
> <http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5716609&code=c3VuYW5kYS5yYW5pQGdtYWlsLmNvbXw1NzE2NjA5fDE2NzE5OTY3NDU=>
> .
> NAML
> <http://apache-poi.1045710.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Encrypt-xlsx-file-with-POI3-9-tp5716609p5716617.html
Sent from the POI - User mailing list archive at Nabble.com.