Skip to content

Commit

Permalink
#380 Use exception type WRONG_PASSWORD when password is null or empty…
Browse files Browse the repository at this point in the history
… for AES
  • Loading branch information
srikanth-lingala committed Nov 6, 2021
1 parent 4e5e2d4 commit 9eab02b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/net/lingala/zip4j/crypto/AESDecrypter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Arrays;

import static net.lingala.zip4j.crypto.AesCipherUtil.prepareBuffAESIVBytes;
import static net.lingala.zip4j.exception.ZipException.Type.WRONG_PASSWORD;
import static net.lingala.zip4j.util.InternalZipConstants.AES_BLOCK_SIZE;

/**
Expand All @@ -49,7 +50,7 @@ private void init(byte[] salt, byte[] passwordVerifier, char[] password, AESExtr
throws ZipException {

if (password == null || password.length <= 0) {
throw new ZipException("empty or null password provided for AES decryption");
throw new ZipException("empty or null password provided for AES decryption", WRONG_PASSWORD);
}

final AesKeyStrength aesKeyStrength = aesExtraDataRecord.getAesKeyStrength();
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/net/lingala/zip4j/ExtractZipFileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,16 @@ public void testExtractZipFileByFileNameWhichTheDirectoryEntryAtTheEndOfCentralD
zipFile.extractFile("test-files/", outputFolder.getPath());
}

@Test
public void testExtractZipFileThrowsExceptionOfTypeWrongPasswordForNullAesPassword() throws ZipException {
testExtractZipFileThrowsExceptionOfTypeWrongPasswordForNullOrEmptyAesPassword(null);
}

@Test
public void testExtractZipFileThrowsExceptionOfTypeWrongPasswordForEmptyAesPassword() throws ZipException {
testExtractZipFileThrowsExceptionOfTypeWrongPasswordForNullOrEmptyAesPassword("".toCharArray());
}

private void addFileToZip(ZipFile zipFile, String fileName, EncryptionMethod encryptionMethod, String password) throws ZipException {
ZipParameters zipParameters = new ZipParameters();
zipParameters.setEncryptFiles(encryptionMethod != null);
Expand Down Expand Up @@ -795,4 +805,15 @@ private List<File> getRegularFilesFromFolder(File folder) throws ZipException {
}
return regularFiles;
}

private void testExtractZipFileThrowsExceptionOfTypeWrongPasswordForNullOrEmptyAesPassword(char[] password) throws ZipException {
ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
addFileToZip(zipFile, "sample.pdf", EncryptionMethod.AES, new String(PASSWORD));

expectedException.expect(ZipException.class);
expectedException.expectMessage("empty or null password provided for AES decryption");

zipFile = new ZipFile(generatedZipFile, password);
zipFile.extractAll(outputFolder.getPath());
}
}

0 comments on commit 9eab02b

Please sign in to comment.