Skip to content

Commit 9eab02b

Browse files
#380 Use exception type WRONG_PASSWORD when password is null or empty for AES
1 parent 4e5e2d4 commit 9eab02b

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/main/java/net/lingala/zip4j/crypto/AESDecrypter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Arrays;
2626

2727
import static net.lingala.zip4j.crypto.AesCipherUtil.prepareBuffAESIVBytes;
28+
import static net.lingala.zip4j.exception.ZipException.Type.WRONG_PASSWORD;
2829
import static net.lingala.zip4j.util.InternalZipConstants.AES_BLOCK_SIZE;
2930

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

5152
if (password == null || password.length <= 0) {
52-
throw new ZipException("empty or null password provided for AES decryption");
53+
throw new ZipException("empty or null password provided for AES decryption", WRONG_PASSWORD);
5354
}
5455

5556
final AesKeyStrength aesKeyStrength = aesExtraDataRecord.getAesKeyStrength();

src/test/java/net/lingala/zip4j/ExtractZipFileIT.java

+21
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,16 @@ public void testExtractZipFileByFileNameWhichTheDirectoryEntryAtTheEndOfCentralD
668668
zipFile.extractFile("test-files/", outputFolder.getPath());
669669
}
670670

671+
@Test
672+
public void testExtractZipFileThrowsExceptionOfTypeWrongPasswordForNullAesPassword() throws ZipException {
673+
testExtractZipFileThrowsExceptionOfTypeWrongPasswordForNullOrEmptyAesPassword(null);
674+
}
675+
676+
@Test
677+
public void testExtractZipFileThrowsExceptionOfTypeWrongPasswordForEmptyAesPassword() throws ZipException {
678+
testExtractZipFileThrowsExceptionOfTypeWrongPasswordForNullOrEmptyAesPassword("".toCharArray());
679+
}
680+
671681
private void addFileToZip(ZipFile zipFile, String fileName, EncryptionMethod encryptionMethod, String password) throws ZipException {
672682
ZipParameters zipParameters = new ZipParameters();
673683
zipParameters.setEncryptFiles(encryptionMethod != null);
@@ -795,4 +805,15 @@ private List<File> getRegularFilesFromFolder(File folder) throws ZipException {
795805
}
796806
return regularFiles;
797807
}
808+
809+
private void testExtractZipFileThrowsExceptionOfTypeWrongPasswordForNullOrEmptyAesPassword(char[] password) throws ZipException {
810+
ZipFile zipFile = new ZipFile(generatedZipFile, PASSWORD);
811+
addFileToZip(zipFile, "sample.pdf", EncryptionMethod.AES, new String(PASSWORD));
812+
813+
expectedException.expect(ZipException.class);
814+
expectedException.expectMessage("empty or null password provided for AES decryption");
815+
816+
zipFile = new ZipFile(generatedZipFile, password);
817+
zipFile.extractAll(outputFolder.getPath());
818+
}
798819
}

0 commit comments

Comments
 (0)