From af2408de744f0c335dcfb70d53f3bc31150fc0c0 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 21 Nov 2016 12:02:35 +0000 Subject: [PATCH] Encryptor is stateful. It's also reused when multiple runs of open entry stream is called. Need to change that. Need Pkware test. --- .../Zip/PkwareTraditionalEncryptionData.cs | 6 +++-- src/SharpCompress/Common/Zip/ZipFilePart.cs | 1 + .../SharpCompress.Test/Zip/ZipArchiveTests.cs | 26 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/SharpCompress/Common/Zip/PkwareTraditionalEncryptionData.cs b/src/SharpCompress/Common/Zip/PkwareTraditionalEncryptionData.cs index 5d77a086..c49409bd 100644 --- a/src/SharpCompress/Common/Zip/PkwareTraditionalEncryptionData.cs +++ b/src/SharpCompress/Common/Zip/PkwareTraditionalEncryptionData.cs @@ -9,10 +9,11 @@ namespace SharpCompress.Common.Zip { private static readonly CRC32 crc32 = new CRC32(); private readonly UInt32[] _Keys = {0x12345678, 0x23456789, 0x34567890}; + private readonly string password; private PkwareTraditionalEncryptionData(string password) { - Initialize(password); + this.password = password; } private byte MagicByte @@ -28,6 +29,7 @@ namespace SharpCompress.Common.Zip byte[] encryptionHeader) { var encryptor = new PkwareTraditionalEncryptionData(password); + encryptor.InitializeKeys(); byte[] plainTextHeader = encryptor.Decrypt(encryptionHeader, encryptionHeader.Length); if (plainTextHeader[11] != (byte)((header.Crc >> 24) & 0xff)) { @@ -84,7 +86,7 @@ namespace SharpCompress.Common.Zip return cipherText; } - private void Initialize(string password) + internal void InitializeKeys() { byte[] p = StringToByteArray(password); for (int i = 0; i < password.Length; i++) diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.cs b/src/SharpCompress/Common/Zip/ZipFilePart.cs index 458ff35e..cba50c48 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.cs @@ -147,6 +147,7 @@ namespace SharpCompress.Common.Zip } if (Header.PkwareTraditionalEncryptionData != null) { + Header.PkwareTraditionalEncryptionData.InitializeKeys(); return new PkwareTraditionalCryptoStream(plainStream, Header.PkwareTraditionalEncryptionData, CryptoMode.Decrypt); } diff --git a/test/SharpCompress.Test/Zip/ZipArchiveTests.cs b/test/SharpCompress.Test/Zip/ZipArchiveTests.cs index f38e0918..cae183a7 100644 --- a/test/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/test/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -183,6 +183,32 @@ namespace SharpCompress.Test Assert.Equal(new FileInfo(scratchPath1).Length, new FileInfo(scratchPath2).Length); } + [Fact] + public void Zip_Read_Entry_Twice() + { + string scratchPath = "C:\\Users\\adam\\Downloads\\Archive1.zip"; + + using (var archive = ArchiveFactory.Open(scratchPath, new ReaderOptions() + { + Password = "12345678" + })) + { + var entries = archive.Entries.Where(entry => !entry.IsDirectory); + + foreach (var entry in entries) + { + for (int i = 0; i < 2; i++) + { + using (var memoryStream = new MemoryStream()) + using (var entryStream = entry.OpenEntryStream()) + { + entryStream.CopyTo(memoryStream); + } + } + } + } + } + [Fact] public void Zip_Removal_Poly() {