Skip to content

Commit 3743e58

Browse files
committed
Implement icsharpcode#538
1 parent a6d103a commit 3743e58

File tree

3 files changed

+29
-79
lines changed

3 files changed

+29
-79
lines changed

src/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs

+20-67
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,18 @@ public abstract class PkzipClassic : SymmetricAlgorithm
1616
/// </summary>
1717
/// <param name="seed">The seed value to initialise keys with.</param>
1818
/// <returns>A new key value.</returns>
19-
static public byte[] GenerateKeys(byte[] seed)
19+
public static byte[] GenerateKeys(byte[] seed)
2020
{
2121
if (seed == null)
22-
{
2322
throw new ArgumentNullException(nameof(seed));
24-
}
25-
2623
if (seed.Length == 0)
27-
{
2824
throw new ArgumentException("Length is zero", nameof(seed));
29-
}
3025

3126
uint[] newKeys = {
3227
0x12345678,
3328
0x23456789,
3429
0x34567890
35-
};
30+
};
3631

3732
for (int i = 0; i < seed.Length; ++i)
3833
{
@@ -55,6 +50,7 @@ static public byte[] GenerateKeys(byte[] seed)
5550
result[9] = (byte)((newKeys[2] >> 8) & 0xff);
5651
result[10] = (byte)((newKeys[2] >> 16) & 0xff);
5752
result[11] = (byte)((newKeys[2] >> 24) & 0xff);
53+
5854
return result;
5955
}
6056
}
@@ -84,14 +80,9 @@ protected byte TransformByte()
8480
protected void SetKeys(byte[] keyData)
8581
{
8682
if (keyData == null)
87-
{
8883
throw new ArgumentNullException(nameof(keyData));
89-
}
90-
9184
if (keyData.Length != 12)
92-
{
9385
throw new InvalidOperationException("Key length is not valid");
94-
}
9586

9687
keys = new uint[3];
9788
keys[0] = (uint)((keyData[3] << 24) | (keyData[2] << 16) | (keyData[1] << 8) | keyData[0]);
@@ -154,6 +145,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
154145
{
155146
byte[] result = new byte[inputCount];
156147
TransformBlock(inputBuffer, inputOffset, inputCount, result, 0);
148+
157149
return result;
158150
}
159151

@@ -175,6 +167,7 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
175167
outputBuffer[outputOffset++] = (byte)(inputBuffer[i] ^ TransformByte());
176168
UpdateKeys(oldbyte);
177169
}
170+
178171
return inputCount;
179172
}
180173

@@ -183,43 +176,31 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
183176
/// </summary>
184177
public bool CanReuseTransform
185178
{
186-
get
187-
{
188-
return true;
189-
}
179+
get { return true; }
190180
}
191181

192182
/// <summary>
193183
/// Gets the size of the input data blocks in bytes.
194184
/// </summary>
195185
public int InputBlockSize
196186
{
197-
get
198-
{
199-
return 1;
200-
}
187+
get { return 1; }
201188
}
202189

203190
/// <summary>
204191
/// Gets the size of the output data blocks in bytes.
205192
/// </summary>
206193
public int OutputBlockSize
207194
{
208-
get
209-
{
210-
return 1;
211-
}
195+
get { return 1; }
212196
}
213197

214198
/// <summary>
215199
/// Gets a value indicating whether multiple blocks can be transformed.
216200
/// </summary>
217201
public bool CanTransformMultipleBlocks
218202
{
219-
get
220-
{
221-
return true;
222-
}
203+
get { return true; }
223204
}
224205

225206
#endregion ICryptoTransform Members
@@ -264,6 +245,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
264245
{
265246
byte[] result = new byte[inputCount];
266247
TransformBlock(inputBuffer, inputOffset, inputCount, result, 0);
248+
267249
return result;
268250
}
269251

@@ -285,6 +267,7 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
285267
outputBuffer[outputOffset++] = newByte;
286268
UpdateKeys(newByte);
287269
}
270+
288271
return inputCount;
289272
}
290273

@@ -293,43 +276,31 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
293276
/// </summary>
294277
public bool CanReuseTransform
295278
{
296-
get
297-
{
298-
return true;
299-
}
279+
get { return true; }
300280
}
301281

302282
/// <summary>
303283
/// Gets the size of the input data blocks in bytes.
304284
/// </summary>
305285
public int InputBlockSize
306286
{
307-
get
308-
{
309-
return 1;
310-
}
287+
get { return 1; }
311288
}
312289

313290
/// <summary>
314291
/// Gets the size of the output data blocks in bytes.
315292
/// </summary>
316293
public int OutputBlockSize
317294
{
318-
get
319-
{
320-
return 1;
321-
}
295+
get { return 1; }
322296
}
323297

324298
/// <summary>
325299
/// Gets a value indicating whether multiple blocks can be transformed.
326300
/// </summary>
327301
public bool CanTransformMultipleBlocks
328302
{
329-
get
330-
{
331-
return true;
332-
}
303+
get { return true; }
333304
}
334305

335306
#endregion ICryptoTransform Members
@@ -359,17 +330,11 @@ public sealed class PkzipClassicManaged : PkzipClassic
359330
/// <remarks>The only valid block size is 8.</remarks>
360331
public override int BlockSize
361332
{
362-
get
363-
{
364-
return 8;
365-
}
366-
333+
get { return 8; }
367334
set
368335
{
369336
if (value != 8)
370-
{
371337
throw new CryptographicException("Block size is invalid");
372-
}
373338
}
374339
}
375340

@@ -415,24 +380,16 @@ public override byte[] Key
415380
get
416381
{
417382
if (key_ == null)
418-
{
419383
GenerateKey();
420-
}
421384

422385
return (byte[])key_.Clone();
423386
}
424-
425387
set
426388
{
427389
if (value == null)
428-
{
429390
throw new ArgumentNullException(nameof(value));
430-
}
431-
432391
if (value.Length != 12)
433-
{
434392
throw new CryptographicException("Key size is illegal");
435-
}
436393

437394
key_ = (byte[])value.Clone();
438395
}
@@ -444,8 +401,8 @@ public override byte[] Key
444401
public override void GenerateKey()
445402
{
446403
key_ = new byte[12];
447-
var rnd = new Random();
448-
rnd.NextBytes(key_);
404+
using var rnd = new RNGCryptoServiceProvider();
405+
rnd.GetBytes(key_);
449406
}
450407

451408
/// <summary>
@@ -454,9 +411,7 @@ public override void GenerateKey()
454411
/// <param name="rgbKey">The key to use for this encryptor.</param>
455412
/// <param name="rgbIV">Initialisation vector for the new encryptor.</param>
456413
/// <returns>Returns a new PkzipClassic encryptor</returns>
457-
public override ICryptoTransform CreateEncryptor(
458-
byte[] rgbKey,
459-
byte[] rgbIV)
414+
public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
460415
{
461416
key_ = rgbKey;
462417
return new PkzipClassicEncryptCryptoTransform(Key);
@@ -468,9 +423,7 @@ public override ICryptoTransform CreateEncryptor(
468423
/// <param name="rgbKey">Keys to use for this new decryptor.</param>
469424
/// <param name="rgbIV">Initialisation vector for the new decryptor.</param>
470425
/// <returns>Returns a new decryptor.</returns>
471-
public override ICryptoTransform CreateDecryptor(
472-
byte[] rgbKey,
473-
byte[] rgbIV)
426+
public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
474427
{
475428
key_ = rgbKey;
476429
return new PkzipClassicDecryptCryptoTransform(Key);

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3682,8 +3682,8 @@ private static void CheckClassicPassword(CryptoStream classicCryptoStream, ZipEn
36823682
private static void WriteEncryptionHeader(Stream stream, long crcValue)
36833683
{
36843684
byte[] cryptBuffer = new byte[ZipConstants.CryptoHeaderSize];
3685-
var rnd = new Random();
3686-
rnd.NextBytes(cryptBuffer);
3685+
using var rnd = new RNGCryptoServiceProvider();
3686+
rnd.GetBytes(cryptBuffer);
36873687
cryptBuffer[11] = (byte)(crcValue >> 24);
36883688
stream.Write(cryptBuffer, 0, cryptBuffer.Length);
36893689
}

src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.IO;
8+
using System.Security.Cryptography;
89

910
namespace ICSharpCode.SharpZipLib.Zip
1011
{
@@ -84,10 +85,7 @@ public ZipOutputStream(Stream baseOutputStream, int bufferSize)
8485
/// <remarks>No further entries can be added once this has been done.</remarks>
8586
public bool IsFinished
8687
{
87-
get
88-
{
89-
return entries == null;
90-
}
88+
get { return entries == null; }
9189
}
9290

9391
/// <summary>
@@ -104,9 +102,8 @@ public void SetComment(string comment)
104102
// TODO: Its not yet clear how to handle unicode comments here.
105103
byte[] commentBytes = ZipStrings.ConvertToArray(comment);
106104
if (commentBytes.Length > 0xffff)
107-
{
108105
throw new ArgumentOutOfRangeException(nameof(comment));
109-
}
106+
110107
zipComment = commentBytes;
111108
}
112109

@@ -633,8 +630,8 @@ private void WriteEncryptionHeader(long crcValue)
633630
InitializePassword(Password);
634631

635632
byte[] cryptBuffer = new byte[ZipConstants.CryptoHeaderSize];
636-
var rnd = new Random();
637-
rnd.NextBytes(cryptBuffer);
633+
using var rnd = new RNGCryptoServiceProvider();
634+
rnd.GetBytes(cryptBuffer);
638635
cryptBuffer[11] = (byte)(crcValue >> 24);
639636

640637
EncryptBlock(cryptBuffer, 0, cryptBuffer.Length);
@@ -925,10 +922,10 @@ public override void Finish()
925922
/// </summary>
926923
public override void Flush()
927924
{
928-
if(curMethod == CompressionMethod.Stored)
925+
if (curMethod == CompressionMethod.Stored)
929926
{
930927
baseOutputStream_.Flush();
931-
}
928+
}
932929
else
933930
{
934931
base.Flush();

0 commit comments

Comments
 (0)