diff --git a/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs b/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs index 1bd544c2d..dd752eb3c 100644 --- a/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs +++ b/src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs @@ -262,6 +262,12 @@ public void PutNextEntry(ZipEntry entry) throw new NotImplementedException("Compression method not supported"); } + // A password must have been set in order to add AES encrypted entries + if (entry.AESKeySize > 0 && string.IsNullOrEmpty(this.Password)) + { + throw new InvalidOperationException("The Password property must be set before AES encrypted entries can be added"); + } + int compressionLevel = defaultCompressionLevel; // Clear flags that the library manages internally diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs index cb2c72d16..60d7a5709 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/StreamHandling.cs @@ -502,5 +502,23 @@ public void ShouldBeAbleToReadEntriesWithInvalidFileNames() } } } + + /// + /// Test for https://github.com/icsharpcode/SharpZipLib/issues/507 + /// + [Test] + [Category("Zip")] + public void AddingAnAESEntryWithNoPasswordShouldThrow() + { + using (var memoryStream = new MemoryStream()) + { + using (var outStream = new ZipOutputStream(memoryStream)) + { + var newEntry = new ZipEntry("test") { AESKeySize = 256 }; + + Assert.Throws(() => outStream.PutNextEntry(newEntry)); + } + } + } } }