remicuson / winzipaes

Automatically exported from code.google.com/p/winzipaes
0 stars 0 forks source link

Generating a mail with an encrypted zip attachment without any temp file #52

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi:
I want to produce an encrypted zip file and send it by E-mail. Therefore I 
generate the attachment by ByteArrayOutputStream and it does not generate any 
temp file in the disc. However, the attach zip file cannot be opened 
successfully. Then I try to use different approach to verify if the 
ByteArrayOutputStream may have some problem while generating zip data.
/** It generates a zip file by the following code successfully **/
String password = "1234";
AESEncrypter aesEncrypter = new AESEncrypterBC();
aesEncrypter.init(password, 0);
AesZipFileEncrypter ze = new AesZipFileEncrypter("D:/VrsBill/20150305.zip", 
aesEncrypter);
//AesZipFileEncrypter ze = new AesZipFileEncrypter(baos, aesEncrypter);
InputStream inputStream = new FileInputStream("D:/VrsBill/20150305.txt");                    

ze.add("abc.txt", inputStream, password);
inputStream.close();
ze.close();
baos.close();   
/** Code End **/

/** It generates a zip file by ByteArrayOutputStream indirectly and the zip 
file cannot be opened successfully **/
ByteArrayOutputStream baos = new ByteArrayOutputStream();               
String password = "1234";
AESEncrypter aesEncrypter = new AESEncrypterBC();
aesEncrypter.init(password, 0);
//AesZipFileEncrypter ze = new AesZipFileEncrypter("D:/VrsBill/20150305.zip", 
aesEncrypter);
AesZipFileEncrypter ze = new AesZipFileEncrypter(baos, aesEncrypter);
InputStream inputStream = new FileInputStream("D:/VrsBill/20150305.txt");                        

ze.add("abc.txt", inputStream, password);                   
FileOutputStream fos = new FileOutputStream("D:/VrsBill/20150305.zip");
fos.write(baos.toByteArray());
fos.close();
inputStream.close();
ze.close();
baos.close();
/** Code End **/

What version of the product are you using? On what operating system?
Eclipse 3.2 with JDK 1.6.0_45, Windows 7 64 Bit.

Please provide any additional information below.
The source file is just a pure text file. This project use bcprov-jdk16-146.jar 
and winzipaes-1.0.1.jar to zip and encrypt the file.

I really need your help and thanks a lot.

Original issue reported on code.google.com by aluba0...@gmail.com on 30 Jun 2015 at 9:09

GoogleCodeExporter commented 8 years ago

Original comment by aluba0...@gmail.com on 30 Jun 2015 at 9:25

Attachments:

GoogleCodeExporter commented 8 years ago
closed as requested

Original comment by olaf.merkert on 30 Jun 2015 at 1:25

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
Why close this issue? Please close issue #51 but keep issue #52 open, thanks.

Original comment by aluba0...@gmail.com on 1 Jul 2015 at 1:31

GoogleCodeExporter commented 8 years ago

Original comment by olaf.merkert on 1 Jul 2015 at 7:46

GoogleCodeExporter commented 8 years ago
did you have a look at this encrypter testcase already?

https://code.google.com/p/winzipaes/source/browse/trunk/winzipaes/tests/src.test
/de/idyl/winzipaes/TestAesZipFileEncrypter.java

Original comment by olaf.merkert on 1 Jul 2015 at 8:15

GoogleCodeExporter commented 8 years ago
HI
Thanks for your response.
I rewrite the method testWithOutputStream() of your sample. 
/*********************/
    public class TestAesZipFileEncrypter
    {
    public void test() throws Exception
    {
        String PASSWORD = "1234";
        OutputStream bao = new ByteArrayOutputStream();
        AESEncrypter encrypter = new AESEncrypterBC();
        AesZipFileEncrypter enc = new AesZipFileEncrypter(bao, encrypter);
        enc.add("20150305.txt", new FileInputStream("D:/VrsBill/20150305.txt"), PASSWORD);
        //Convert data saved in ByteArrayOutputStream to File 
        FileOutputStream fos = new FileOutputStream("D:/VrsBill/20150305.zip");
        fos.write(((ByteArrayOutputStream)bao).toByteArray());
        //Comment End
        fos.close();        
        enc.close();

    }
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {
        // TODO Auto-generated method stub
        TestAesZipFileEncrypter test = new TestAesZipFileEncrypter();
        test.test();
    }
    }
/**********************************/
I try to write data which is saved in the ByteArrayOutputStream to the 
FileOutputStream (file). However, the output file (20150305.zip) can not be 
opened successfully. It means that the generated file is an invalid zip file.  

Original comment by aluba0...@gmail.com on 3 Jul 2015 at 2:30

GoogleCodeExporter commented 8 years ago
I'm don't quite understand, what you're trying to achieve:
Do you simply want to create a zip file that contains the file 
D:/VrsBill/20150305.txt as an encrypted entry or is it something else?

Original comment by olaf.merkert on 3 Jul 2015 at 5:27

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
I want to send mail with an encrypted zip attachment, and all steps are 
processed in the memory. Therefore, I use ByteArrayOutputStream instead of 
FileOutputStream sending mail. However, the zip attachment which is generated 
by your API (using ByteArrayOutputStream) is corrupted. Therefore, at the final 
step of above code, I use FileOutputStream to verify if the encrypted zip data 
saved in the ByteArrayOutputStream is valid zip format. Unfortunately, the file 
is corrupted, and I can not open it successfully. That means there is some 
problem happening while using ByteArrayOutputStream to generate encrypted zip 
data.

Original comment by aluba0...@gmail.com on 6 Jul 2015 at 2:02

GoogleCodeExporter commented 8 years ago
In TestAesZipFileEncrypter.testWithOutputStream() you can see, that the test 
ends with a call to "enc.close()". This call is required before you write the 
bytes of the ByteArrayOutputStream anywhere, as it adds the "central directory" 
at the end of the zip "file".

Original comment by olaf.merkert on 6 Jul 2015 at 7:36

GoogleCodeExporter commented 8 years ago
HI
I use your suggestion and correct the code. (Closing AesZipFileEncrypter before 
writing the bytes of the ByteArrayOutputStream) It works good now.
Thanks a lot. 

Original comment by aluba0...@gmail.com on 9 Jul 2015 at 3:09

GoogleCodeExporter commented 8 years ago

Original comment by olaf.merkert on 9 Jul 2015 at 8:06