From 9eab02b675d23079fc87fe9a6c0d5503465d2e3e Mon Sep 17 00:00:00 2001 From: Srikanth Reddy Lingala Date: Sat, 6 Nov 2021 05:58:42 +0100 Subject: [PATCH] #380 Use exception type WRONG_PASSWORD when password is null or empty for AES --- .../lingala/zip4j/crypto/AESDecrypter.java | 3 ++- .../net/lingala/zip4j/ExtractZipFileIT.java | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/lingala/zip4j/crypto/AESDecrypter.java b/src/main/java/net/lingala/zip4j/crypto/AESDecrypter.java index aed08c8b..2daa7e30 100755 --- a/src/main/java/net/lingala/zip4j/crypto/AESDecrypter.java +++ b/src/main/java/net/lingala/zip4j/crypto/AESDecrypter.java @@ -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; /** @@ -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(); diff --git a/src/test/java/net/lingala/zip4j/ExtractZipFileIT.java b/src/test/java/net/lingala/zip4j/ExtractZipFileIT.java index 7009271e..95eed798 100644 --- a/src/test/java/net/lingala/zip4j/ExtractZipFileIT.java +++ b/src/test/java/net/lingala/zip4j/ExtractZipFileIT.java @@ -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); @@ -795,4 +805,15 @@ private List 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()); + } }