Skip to content

Commit 73aa23a

Browse files
authored
Merge PR #472: Allow ZipFile to accept empty strings as passwords when decrypting AES entries
* Add unit test for reading an AES encrypted entry with an empty password * Allow ZipFile to accept empty strings as passwords when decrypting AES entries
1 parent 4bbcb4b commit 73aa23a

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,10 @@ public string Password
367367
}
368368
else
369369
{
370-
rawPassword_ = value;
371370
key = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(value));
372371
}
372+
373+
rawPassword_ = value;
373374
}
374375
}
375376

@@ -3612,9 +3613,9 @@ private Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
36123613
{
36133614
if (entry.Version >= ZipConstants.VERSION_AES)
36143615
{
3615-
//
3616+
// Issue #471 - accept an empty string as a password, but reject null.
36163617
OnKeysRequired(entry.Name);
3617-
if (HaveKeys == false)
3618+
if (rawPassword_ == null)
36183619
{
36193620
throw new ZipException("No password available for AES encrypted stream");
36203621
}

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,40 @@ public void ZipFileAesDelete()
365365
}
366366
}
367367

368+
// This is a zip file with one AES encrypted entry, whose password in an empty string.
369+
const string TestFileWithEmptyPassword = @"UEsDBDMACQBjACaj0FAyKbop//////////8EAB8AdGVzdAEAEAA4AAAA
370+
AAAAAFIAAAAAAAAAAZkHAAIAQUUDCABADvo3YqmCtIE+lhw26kjbqkGsLEOk6bVA+FnSpVD4yGP4Mr66Hs14aTtsPUaANX2
371+
Z6qZczEmwoaNQpNBnKl7p9YOG8GSHDfTCUU/AZvT4yGFhUEsHCDIpuilSAAAAAAAAADgAAAAAAAAAUEsBAjMAMwAJAGMAJq
372+
PQUDIpuin//////////wQAHwAAAAAAAAAAAAAAAAAAAHRlc3QBABAAOAAAAAAAAABSAAAAAAAAAAGZBwACAEFFAwgAUEsFBgAAAAABAAEAUQAAAKsAAAAAAA==";
373+
374+
/// <summary>
375+
/// Test reading an AES encrypted entry whose password is an empty string.
376+
/// </summary>
377+
/// <remarks>
378+
/// Test added for https://github.com/icsharpcode/SharpZipLib/issues/471.
379+
/// </remarks>
380+
[Test]
381+
[Category("Zip")]
382+
public void ZipFileAESReadWithEmptyPassword()
383+
{
384+
var fileBytes = Convert.FromBase64String(TestFileWithEmptyPassword);
385+
386+
using (var ms = new MemoryStream(fileBytes))
387+
using (var zipFile = new ZipFile(ms, leaveOpen: true))
388+
{
389+
zipFile.Password = string.Empty;
390+
391+
var entry = zipFile.FindEntry("test", true);
392+
393+
using (var inputStream = zipFile.GetInputStream(entry))
394+
using (var sr = new StreamReader(inputStream, Encoding.UTF8))
395+
{
396+
var content = sr.ReadToEnd();
397+
Assert.That(content, Is.EqualTo("Lorem ipsum dolor sit amet, consectetur adipiscing elit."), "Decompressed content does not match expected data");
398+
}
399+
}
400+
}
401+
368402
private static readonly string[] possible7zPaths = new[] {
369403
// Check in PATH
370404
"7z", "7za",

0 commit comments

Comments
 (0)