@@ -16,23 +16,18 @@ public abstract class PkzipClassic : SymmetricAlgorithm
16
16
/// </summary>
17
17
/// <param name="seed">The seed value to initialise keys with.</param>
18
18
/// <returns>A new key value.</returns>
19
- static public byte [ ] GenerateKeys ( byte [ ] seed )
19
+ public static byte [ ] GenerateKeys ( byte [ ] seed )
20
20
{
21
21
if ( seed == null )
22
- {
23
22
throw new ArgumentNullException ( nameof ( seed ) ) ;
24
- }
25
-
26
23
if ( seed . Length == 0 )
27
- {
28
24
throw new ArgumentException ( "Length is zero" , nameof ( seed ) ) ;
29
- }
30
25
31
26
uint [ ] newKeys = {
32
27
0x12345678 ,
33
28
0x23456789 ,
34
29
0x34567890
35
- } ;
30
+ } ;
36
31
37
32
for ( int i = 0 ; i < seed . Length ; ++ i )
38
33
{
@@ -55,6 +50,7 @@ static public byte[] GenerateKeys(byte[] seed)
55
50
result [ 9 ] = ( byte ) ( ( newKeys [ 2 ] >> 8 ) & 0xff ) ;
56
51
result [ 10 ] = ( byte ) ( ( newKeys [ 2 ] >> 16 ) & 0xff ) ;
57
52
result [ 11 ] = ( byte ) ( ( newKeys [ 2 ] >> 24 ) & 0xff ) ;
53
+
58
54
return result ;
59
55
}
60
56
}
@@ -84,14 +80,9 @@ protected byte TransformByte()
84
80
protected void SetKeys ( byte [ ] keyData )
85
81
{
86
82
if ( keyData == null )
87
- {
88
83
throw new ArgumentNullException ( nameof ( keyData ) ) ;
89
- }
90
-
91
84
if ( keyData . Length != 12 )
92
- {
93
85
throw new InvalidOperationException ( "Key length is not valid" ) ;
94
- }
95
86
96
87
keys = new uint [ 3 ] ;
97
88
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
154
145
{
155
146
byte [ ] result = new byte [ inputCount ] ;
156
147
TransformBlock ( inputBuffer , inputOffset , inputCount , result , 0 ) ;
148
+
157
149
return result ;
158
150
}
159
151
@@ -175,6 +167,7 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
175
167
outputBuffer [ outputOffset ++ ] = ( byte ) ( inputBuffer [ i ] ^ TransformByte ( ) ) ;
176
168
UpdateKeys ( oldbyte ) ;
177
169
}
170
+
178
171
return inputCount ;
179
172
}
180
173
@@ -183,43 +176,31 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
183
176
/// </summary>
184
177
public bool CanReuseTransform
185
178
{
186
- get
187
- {
188
- return true ;
189
- }
179
+ get { return true ; }
190
180
}
191
181
192
182
/// <summary>
193
183
/// Gets the size of the input data blocks in bytes.
194
184
/// </summary>
195
185
public int InputBlockSize
196
186
{
197
- get
198
- {
199
- return 1 ;
200
- }
187
+ get { return 1 ; }
201
188
}
202
189
203
190
/// <summary>
204
191
/// Gets the size of the output data blocks in bytes.
205
192
/// </summary>
206
193
public int OutputBlockSize
207
194
{
208
- get
209
- {
210
- return 1 ;
211
- }
195
+ get { return 1 ; }
212
196
}
213
197
214
198
/// <summary>
215
199
/// Gets a value indicating whether multiple blocks can be transformed.
216
200
/// </summary>
217
201
public bool CanTransformMultipleBlocks
218
202
{
219
- get
220
- {
221
- return true ;
222
- }
203
+ get { return true ; }
223
204
}
224
205
225
206
#endregion ICryptoTransform Members
@@ -264,6 +245,7 @@ public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int input
264
245
{
265
246
byte [ ] result = new byte [ inputCount ] ;
266
247
TransformBlock ( inputBuffer , inputOffset , inputCount , result , 0 ) ;
248
+
267
249
return result ;
268
250
}
269
251
@@ -285,6 +267,7 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
285
267
outputBuffer [ outputOffset ++ ] = newByte ;
286
268
UpdateKeys ( newByte ) ;
287
269
}
270
+
288
271
return inputCount ;
289
272
}
290
273
@@ -293,43 +276,31 @@ public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, b
293
276
/// </summary>
294
277
public bool CanReuseTransform
295
278
{
296
- get
297
- {
298
- return true ;
299
- }
279
+ get { return true ; }
300
280
}
301
281
302
282
/// <summary>
303
283
/// Gets the size of the input data blocks in bytes.
304
284
/// </summary>
305
285
public int InputBlockSize
306
286
{
307
- get
308
- {
309
- return 1 ;
310
- }
287
+ get { return 1 ; }
311
288
}
312
289
313
290
/// <summary>
314
291
/// Gets the size of the output data blocks in bytes.
315
292
/// </summary>
316
293
public int OutputBlockSize
317
294
{
318
- get
319
- {
320
- return 1 ;
321
- }
295
+ get { return 1 ; }
322
296
}
323
297
324
298
/// <summary>
325
299
/// Gets a value indicating whether multiple blocks can be transformed.
326
300
/// </summary>
327
301
public bool CanTransformMultipleBlocks
328
302
{
329
- get
330
- {
331
- return true ;
332
- }
303
+ get { return true ; }
333
304
}
334
305
335
306
#endregion ICryptoTransform Members
@@ -359,17 +330,11 @@ public sealed class PkzipClassicManaged : PkzipClassic
359
330
/// <remarks>The only valid block size is 8.</remarks>
360
331
public override int BlockSize
361
332
{
362
- get
363
- {
364
- return 8 ;
365
- }
366
-
333
+ get { return 8 ; }
367
334
set
368
335
{
369
336
if ( value != 8 )
370
- {
371
337
throw new CryptographicException ( "Block size is invalid" ) ;
372
- }
373
338
}
374
339
}
375
340
@@ -415,24 +380,16 @@ public override byte[] Key
415
380
get
416
381
{
417
382
if ( key_ == null )
418
- {
419
383
GenerateKey ( ) ;
420
- }
421
384
422
385
return ( byte [ ] ) key_ . Clone ( ) ;
423
386
}
424
-
425
387
set
426
388
{
427
389
if ( value == null )
428
- {
429
390
throw new ArgumentNullException ( nameof ( value ) ) ;
430
- }
431
-
432
391
if ( value . Length != 12 )
433
- {
434
392
throw new CryptographicException ( "Key size is illegal" ) ;
435
- }
436
393
437
394
key_ = ( byte [ ] ) value . Clone ( ) ;
438
395
}
@@ -444,8 +401,8 @@ public override byte[] Key
444
401
public override void GenerateKey ( )
445
402
{
446
403
key_ = new byte [ 12 ] ;
447
- var rnd = new Random ( ) ;
448
- rnd . NextBytes ( key_ ) ;
404
+ using var rnd = new RNGCryptoServiceProvider ( ) ;
405
+ rnd . GetBytes ( key_ ) ;
449
406
}
450
407
451
408
/// <summary>
@@ -454,9 +411,7 @@ public override void GenerateKey()
454
411
/// <param name="rgbKey">The key to use for this encryptor.</param>
455
412
/// <param name="rgbIV">Initialisation vector for the new encryptor.</param>
456
413
/// <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 )
460
415
{
461
416
key_ = rgbKey ;
462
417
return new PkzipClassicEncryptCryptoTransform ( Key ) ;
@@ -468,9 +423,7 @@ public override ICryptoTransform CreateEncryptor(
468
423
/// <param name="rgbKey">Keys to use for this new decryptor.</param>
469
424
/// <param name="rgbIV">Initialisation vector for the new decryptor.</param>
470
425
/// <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 )
474
427
{
475
428
key_ = rgbKey ;
476
429
return new PkzipClassicDecryptCryptoTransform ( Key ) ;
0 commit comments