Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Error reading ZIP file  (Read 1191 times)
0 Members and 1 Guest are viewing this topic.
Offline Renoria

Junior Member




...


« Posted 2008-12-31 15:55:32 »

Hi, I got this code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
            ZipEntry ze;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            dec.decrypt(new FileInputStream(gui), baos);
            byte[] buf = baos.toByteArray();
            ByteArrayInputStream bis = new ByteArrayInputStream(buf);
            zis = new ZipInputStream(bis);
          while ((ze = zis.getNextEntry()) != null) {
            System.out.println("Unzipping " + ze.getName());
            ByteArrayOutputStream fout = new ByteArrayOutputStream();
            for (int c = zis.read(); c != -1; c = zis.read()) {
              fout.write(c);
            }
//            ByteArrayInputStream bis = new ByteArrayInputStream(fout.toByteArray());
//            byte bt;
//            while ((bt = (byte)bis.read()) != -1) {
//                System.out.println(bt);
//            }
           zis.closeEntry();
            fout.close();
          }
          zis.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }


When I try to wrap bis into a ZipInputStream, calling getNextEntry() always returns null. How do I fix this?

Thanks.
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #1 - Posted 2009-01-01 00:18:18 »

Your code seems to be correct. I believe there is an error with your decrypting method and is corrupting the zip file to be read. This slightly modified source code works perfectly...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
      try
      {
           ZipEntry ze;
           ZipInputStream zis;
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           FileInputStream fis = new FileInputStream("test.zip");
           
           for (int c = fis.read(); c != -1; c = fis.read()) {
              baos.write(c);
             }
           baos.close();
           
           byte[] buf = baos.toByteArray();
           ByteArrayInputStream bis = new ByteArrayInputStream(buf);
           zis = new ZipInputStream(bis);
         while ((ze = zis.getNextEntry()) != null) {
           System.out.println("Unzipping " + ze.getName());
           ByteArrayOutputStream fout = new ByteArrayOutputStream();

           zis.closeEntry();
           fout.close();
         }
         zis.close();
       } catch (IOException e) {
           e.printStackTrace();
       } catch (Exception ex) {
           ex.printStackTrace();
       }


of course you will need a zip file named "test.zip" in your working directory.
Offline Renoria

Junior Member




...


« Reply #2 - Posted 2009-01-01 02:10:00 »

Your code seems to be correct. I believe there is an error with your decrypting method and is corrupting the zip file to be read. This slightly modified source code works perfectly...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
      try
      {
           ZipEntry ze;
           ZipInputStream zis;
           ByteArrayOutputStream baos = new ByteArrayOutputStream();
           FileInputStream fis = new FileInputStream("test.zip");
           
           for (int c = fis.read(); c != -1; c = fis.read()) {
              baos.write(c);
             }
           baos.close();
           
           byte[] buf = baos.toByteArray();
           ByteArrayInputStream bis = new ByteArrayInputStream(buf);
           zis = new ZipInputStream(bis);
         while ((ze = zis.getNextEntry()) != null) {
           System.out.println("Unzipping " + ze.getName());
           ByteArrayOutputStream fout = new ByteArrayOutputStream();

           zis.closeEntry();
           fout.close();
         }
         zis.close();
       } catch (IOException e) {
           e.printStackTrace();
       } catch (Exception ex) {
           ex.printStackTrace();
       }


of course you will need a zip file named "test.zip" in your working directory.

Thanks for your reply. I have my decryption method like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
package renoria.security;

import java.io.InputStream;
import java.io.OutputStream;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

/**
 *
 * @author David
 */

public class Decrypter {
    private Cipher ecipher;
    private Cipher dcipher;
    private static final byte[] iv = new byte[] { (byte) 0xCE, 0x4F, 0x77, (byte) 0xA4, 0x45, (byte) 0xD0,
            0x71, (byte) 0xBF, (byte) 0xB7, (byte) 0x98, 0x20, (byte) 0xFC, 0x4B, (byte) 0xE9, (byte) 0xB3, (byte) 0xE1,
            0x5C, 0x22, (byte) 0xF7, 0x0C, 0x44, 0x1B, (byte) 0x81, (byte) 0xBD, 0x63, (byte) 0x8D, (byte) 0xD4,
            (byte) 0xC3, (byte) 0xF2, 0x10, 0x19, (byte) 0xE0, (byte) 0xFB, (byte) 0xA1, 0x6E, 0x66, (byte) 0xEA,
            (byte) 0xAE, (byte) 0xD6, (byte) 0xCE, 0x06, 0x18, 0x4E, (byte) 0xEB, 0x78, (byte) 0x95, (byte) 0xDB,
            (byte) 0xBA, (byte) 0xB6, 0x42, 0x7A, 0x2A, (byte) 0x83, 0x0B, 0x54, 0x67, 0x6D, (byte) 0xE8, 0x65,
            (byte) 0xE7, 0x2F, 0x07, (byte) 0xF3, (byte) 0xA4, 0x27, 0x7B, (byte) 0x85, (byte) 0xB0, 0x26, (byte) 0xFD,
            (byte) 0x8B, (byte) 0xA9, (byte) 0xFA, (byte) 0xBE, (byte) 0xA8, (byte) 0xD7, (byte) 0xCB, (byte) 0xCC,
            (byte) 0x92, (byte) 0xDA, (byte) 0xF9, (byte) 0x93, 0x60, 0x2D, (byte) 0xDD, (byte) 0xD2, (byte) 0xA2,
            (byte) 0x9B, 0x39, 0x5F, (byte) 0x72, 0x11, 0x4C, 0x69, (byte) 0xF8, 0x31, (byte) 0x87, (byte) 0xEE,
            (byte) 0x8E, (byte) 0xAD, (byte) 0x8C, 0x6A, (byte) 0xBC, (byte) 0xB5, 0x6B, 0x59, 0x13, (byte) 0xF1, 0x04,
            0x00, (byte) 0xF6, 0x5A, 0x35, 0x79, 0x48, (byte) 0x8F, 0x15, (byte) 0xCD, (byte) 0x97, 0x57, 0x12, 0x3E, 0x37,
            (byte) 0xFF, (byte) 0x9D, 0x4F, 0x51, (byte) 0xF1, (byte) 0xA3, 0x70, (byte) 0xBB, 0x14, 0x75, (byte) 0xC2,
            (byte) 0xB8, 0x72, (byte) 0xC0, (byte) 0xED, 0x7D, 0x68, (byte) 0xC9, 0x2E, 0x0D, 0x62, 0x46, 0x17, 0x11, 0x4D,
            0x6C, (byte) 0xC4, 0x7E, 0x52, (byte) 0xC1, 0x54, (byte) 0xC7, (byte) 0x9A, 0x1C, (byte) 0x88, 0x58, 0x2C,
            (byte) 0x89, (byte) 0xDC, 0x02, 0x64, 0x40, 0x01, 0x5D, 0x38, (byte) 0xA5, (byte) 0xE2, (byte) 0xAF, 0x55,
            (byte) 0xD5, (byte) 0xEF, 0x2A, 0x7C, (byte) 0xA7, 0x5B, (byte) 0xA6, 0x6F, (byte) 0x86, (byte) 0x9F, 0x73,
            (byte) 0xE6, 0x0A, (byte) 0xDE, 0x2B, (byte) 0x99, 0x4A, 0x47, (byte) 0x9C, (byte) 0xDF, 0x09, 0x76,
            (byte) 0x9E, 0x30, 0x0E, (byte) 0xE4, (byte) 0xB2, (byte) 0x94, (byte) 0xA0, 0x3B, 0x34, 0x1D, 0x28, 0x0F,
            0x36, (byte) 0xE3, 0x23, (byte) 0xB4, 0x03, (byte) 0xD8, (byte) 0x90, (byte) 0xC8, 0x3C, (byte) 0xFE, 0x5E,
            0x32, 0x24, 0x50, 0x1F, 0x3A, 0x43, (byte) 0x8A, (byte) 0x96, 0x41, 0x74, (byte) 0xAC, 0x52, 0x33, (byte) 0xF0,
            (byte) 0xD9, 0x29, (byte) 0x50, (byte) 0xB1, 0x16, (byte) 0xD3, (byte) 0xAB, (byte) 0x91, (byte) 0xB9,
            (byte) 0x84, 0x7F, 0x61, 0x1E, (byte) 0xCF, (byte) 0xC5, (byte) 0xD1, 0x56, 0x3D, (byte) 0xCA, (byte) 0xF4,
            0x05, (byte) 0xC6, (byte) 0xD5, 0x08, 0x49, 0x4F, 0x64, 0x69, 0x6E, 0x4D, 0x53, 0x7E, 0x46, 0x72, 0x7A
    };
   
    // 8-byte Salt
   byte[] salt = {
        (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
        (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
    };

    // Iteration count
   int iterationCount = 1000;
   
    public Decrypter(String passPhrase) {
        try {
            // Create the key
           KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
            SecretKey key = SecretKeyFactory.getInstance(
                "PBEWithMD5AndDES").generateSecret(keySpec);
            ecipher = Cipher.getInstance(key.getAlgorithm());
            dcipher = Cipher.getInstance(key.getAlgorithm());

            // Prepare the parameter to the ciphers
           AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);

            // Create the ciphers
           ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        } catch (java.security.InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (java.security.spec.InvalidKeySpecException e) {
            e.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (java.security.NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (java.security.InvalidKeyException e) {
            e.printStackTrace();
        }
    }

    // Buffer used to transport the bytes from one stream to another
   byte[] buf = new byte[1024];

    public void encrypt(InputStream in, OutputStream out) {
        try {
            // Bytes written to out will be encrypted
           out = new CipherOutputStream(out, ecipher);

            // Read in the cleartext bytes and write to out to encrypt
           int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            out.close();
        } catch (java.io.IOException e) {
        }
    }

    public void decrypt(InputStream in, OutputStream out) {
        try {
            // Bytes read from in will be decrypted
           in = new CipherInputStream(in, dcipher);

            // Read in the decrypted bytes and write the cleartext to out
           int numRead = 0;
            while ((numRead = in.read(buf)) >= 0) {
                out.write(buf, 0, numRead);
            }
            out.close();
        } catch (java.io.IOException e) {
        }
    }
}


I use a String as a password. Maybe it is because I don't flush the stream properly? Or is it because I'm closing it after I write it out?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #3 - Posted 2009-01-01 03:04:52 »

I am not familiar with java's encryption classes. I suggest you read tutorials and perform simple tests to isolate your problem.
Offline Renoria

Junior Member




...


« Reply #4 - Posted 2009-01-01 03:20:51 »

I am not familiar with java's encryption classes. I suggest you read tutorials and perform simple tests to isolate your problem.

Okay. Thanks for your help.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (37 views)
2014-07-24 01:59:36

Riven (38 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (50 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23

MustardPeter (44 views)
2014-07-16 23:30:00

Cero (60 views)
2014-07-16 00:42:17

Riven (57 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!