Skip to content

Commit

Permalink
Merge PR #472: Allow ZipFile to accept empty strings as passwords whe…
Browse files Browse the repository at this point in the history
…n 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
  • Loading branch information
Numpsy authored Jun 19, 2020
1 parent 4bbcb4b commit 73aa23a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,10 @@ public string Password
}
else
{
rawPassword_ = value;
key = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(value));
}

rawPassword_ = value;
}
}

Expand Down Expand Up @@ -3612,9 +3613,9 @@ private Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
{
if (entry.Version >= ZipConstants.VERSION_AES)
{
//
// Issue #471 - accept an empty string as a password, but reject null.
OnKeysRequired(entry.Name);
if (HaveKeys == false)
if (rawPassword_ == null)
{
throw new ZipException("No password available for AES encrypted stream");
}
Expand Down
34 changes: 34 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipEncryptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,40 @@ public void ZipFileAesDelete()
}
}

// This is a zip file with one AES encrypted entry, whose password in an empty string.
const string TestFileWithEmptyPassword = @"UEsDBDMACQBjACaj0FAyKbop//////////8EAB8AdGVzdAEAEAA4AAAA
AAAAAFIAAAAAAAAAAZkHAAIAQUUDCABADvo3YqmCtIE+lhw26kjbqkGsLEOk6bVA+FnSpVD4yGP4Mr66Hs14aTtsPUaANX2
Z6qZczEmwoaNQpNBnKl7p9YOG8GSHDfTCUU/AZvT4yGFhUEsHCDIpuilSAAAAAAAAADgAAAAAAAAAUEsBAjMAMwAJAGMAJq
PQUDIpuin//////////wQAHwAAAAAAAAAAAAAAAAAAAHRlc3QBABAAOAAAAAAAAABSAAAAAAAAAAGZBwACAEFFAwgAUEsFBgAAAAABAAEAUQAAAKsAAAAAAA==";

/// <summary>
/// Test reading an AES encrypted entry whose password is an empty string.
/// </summary>
/// <remarks>
/// Test added for https://github.com/icsharpcode/SharpZipLib/issues/471.
/// </remarks>
[Test]
[Category("Zip")]
public void ZipFileAESReadWithEmptyPassword()
{
var fileBytes = Convert.FromBase64String(TestFileWithEmptyPassword);

using (var ms = new MemoryStream(fileBytes))
using (var zipFile = new ZipFile(ms, leaveOpen: true))
{
zipFile.Password = string.Empty;

var entry = zipFile.FindEntry("test", true);

using (var inputStream = zipFile.GetInputStream(entry))
using (var sr = new StreamReader(inputStream, Encoding.UTF8))
{
var content = sr.ReadToEnd();
Assert.That(content, Is.EqualTo("Lorem ipsum dolor sit amet, consectetur adipiscing elit."), "Decompressed content does not match expected data");
}
}
}

private static readonly string[] possible7zPaths = new[] {
// Check in PATH
"7z", "7za",
Expand Down

0 comments on commit 73aa23a

Please sign in to comment.