Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong password extracting zip file inside a zip file #87

Closed
Windolian opened this issue Oct 14, 2019 · 8 comments
Closed

Wrong password extracting zip file inside a zip file #87

Windolian opened this issue Oct 14, 2019 · 8 comments
Assignees
Labels
bug Something isn't working resolved

Comments

@Windolian
Copy link

Windolian commented Oct 14, 2019

Hi.
I'm trying to extract a zip file (1234.zip - Password:1234) inside another zip file (123.zip, Password:1234), without extracting the first.
On myInputStreamFirmwarePackage.getNextEntry()
and after like 6 files done correctly it crashes with error: "Wrong password...".

Here's the zip file
123.zip

This is my code:

ZipFile myZipFile = new ZipFile("123.zip");
myZipFile.setPassword(caPasswordZip);

try {
    
			List<FileHeader> myFileHeaderList = myZipFile.getFileHeaders();

			for (int i = 0; i < myFileHeaderList.size(); i++) {

				FileHeader myFileHeader = (FileHeader) myFileHeaderList.get(i);
				if (myFileHeader != null) {

					if (myFileHeader.getFileName().equals("1234.zip")) {

						if (myZipFile.isEncrypted() == true) {

							myZipFile.setPassword("123".toCharArray());

							ZipInputStream myInputStream = new ZipInputStream(myZipFile
									.getInputStream(myFileHeader), "1234".toCharArray());

							LocalFileHeader localFileHeader;
							
							while ((localFileHeader = myInputStream.getNextEntry()) != null) { // After some files it crashes here with Wrong password even thought the password is all the same
                                while ((readLen = myInputStream.read(readBuffer)) != -1) {
								    
								  }
								System.out.println(localFileHeader.getFileName());
							}
							myInputStream.close();

						} else {

							Log.error("This is an unprotected file. Canceling file.");
							return false;
						}

						return true;
					}
				} else {

					Log.error("Fileheader is null. Shouldn't be here");
				}
			}
		} catch (ZipException e) {
		      e.printStackTrace();
			if (e.getType().equals(ZipException.Type.WRONG_PASSWORD)) {

				Log.error("Wrong password.");
				Log.error(e.getLocalizedMessage(), e);

			} else {

				Log.error(e.getLocalizedMessage(), e);
			}
		} catch (Exception e) {

			Log.error(e.getLocalizedMessage(), e);
		}

		return false;

Error is:

net.lingala.zip4j.exception.ZipException: Wrong Password
	at net.lingala.zip4j.crypto.AESDecrypter.init(AESDecrypter.java:76)
	at net.lingala.zip4j.crypto.AESDecrypter.<init>(AESDecrypter.java:50)
	at net.lingala.zip4j.io.inputstream.AesCipherInputStream.initializeDecrypter(AesCipherInputStream.java:36)
	at net.lingala.zip4j.io.inputstream.AesCipherInputStream.initializeDecrypter(AesCipherInputStream.java:17)
	at net.lingala.zip4j.io.inputstream.CipherInputStream.<init>(CipherInputStream.java:21)
	at net.lingala.zip4j.io.inputstream.AesCipherInputStream.<init>(AesCipherInputStream.java:31)

Those files are just a example. I'm doing it with bigger files and the error a bit diferent but the error type is still ZipException.Type.WRONG_PASSWORD:

java.util.zip.DataFormatException: invalid stored block lengths
java.io.IOException: java.util.zip.DataFormatException: invalid stored block lengths
	at net.lingala.zip4j.io.inputstream.InflaterInputStream.read(InflaterInputStream.java:52)
	at net.lingala.zip4j.io.inputstream.ZipInputStream.read(ZipInputStream.java:155)
	at net.lingala.zip4j.io.inputstream.ZipInputStream.read(ZipInputStream.java:127)
	at net.lingala.zip4j.io.inputstream.ZipInputStream.readUntilEndOfEntry(ZipInputStream.java:325)
	at net.lingala.zip4j.io.inputstream.ZipInputStream.getNextEntry(ZipInputStream.java:84)
	at net.lingala.zip4j.io.inputstream.ZipInputStream.getNextEntry(ZipInputStream.java:79)

@Windolian Windolian changed the title Wrong password on a extracting zip file inside a zip file Wrong password extracting zip file inside a zip file Oct 14, 2019
@srikanth-lingala
Copy link
Owner

Before I try your example, I want to clarify if there is a typo in your code or in the issue mentioned here. In your issue you say that the password is 1234, but in the code it is set as myZipFile.setPassword("123".toCharArray());

@Windolian
Copy link
Author

Windolian commented Oct 15, 2019

Yes, my bad sorry.
( I changed the code so that it includes the test zip file that i sent you and forget to include those changes)
But it doesn't change anything
This line myZipFile.setPassword(caPasswordZip); is doing the same as this
myZipFile.setPassword("123".toCharArray());

I set the 1234 password here

ZipInputStream myInputStream = new ZipInputStream(myZipFile.getInputStream(myFileHeader), "1234".toCharArray());

@srikanth-lingala
Copy link
Owner

I was able to reproduce your issue. However, I have created a new zip file (from some other files) on my machine with similar scenario (zip inside an encrypted zip), and I was able to extract it fine with your code. Seems like this issue has something to do with this zip file or the way it was generated.

@srikanth-lingala
Copy link
Owner

I will investigate and update this thread.

@srikanth-lingala
Copy link
Owner

srikanth-lingala commented Oct 15, 2019

I made a fix and released a snapshot version 2.2.3.5-SNAPSHOT. Please try with this version and let me know if that fixes it. If yes, I will include this fix in the next final release. Please try not just with the zip file you attached, but also with other combinations if possible. Thanks.

If you are unable to get the snapshot from maven, use the below in your pom in distributionManagementtag:

<snapshotRepository>
    <id>ossrh</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>

@Windolian
Copy link
Author

I made a fix and released a snapshot version 2.2.3.5-SNAPSHOT. Please try with this version and let me know if that fixes it. If yes, I will include this fix in the next final release. Please try not just with the zip file you attached, but also with other combinations if possible. Thanks.

If you are unable to get the snapshot from maven, use the below in your pom in distributionManagementtag:

<snapshotRepository>
    <id>ossrh</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>

Can you give me the .jar file?

@srikanth-lingala
Copy link
Owner

You can download it from here. Sometimes the page loads a while to load.

@srikanth-lingala srikanth-lingala added the bug Something isn't working label Oct 18, 2019
@srikanth-lingala srikanth-lingala self-assigned this Oct 18, 2019
@srikanth-lingala
Copy link
Owner

srikanth-lingala commented Oct 19, 2019

Fixed in v2.2.3 released today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working resolved
Projects
None yet
Development

No branches or pull requests

2 participants