Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RNGCryptoServiceProvider for crypto headers #538

Merged
merged 2 commits into from
Nov 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/ICSharpCode.SharpZipLib/Encryption/PkzipClassic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,10 @@ public override byte[] Key
public override void GenerateKey()
{
key_ = new byte[12];
var rnd = new Random();
rnd.NextBytes(key_);
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(key_);
}
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3713,8 +3713,10 @@ private static void CheckClassicPassword(CryptoStream classicCryptoStream, ZipEn
private static void WriteEncryptionHeader(Stream stream, long crcValue)
{
byte[] cryptBuffer = new byte[ZipConstants.CryptoHeaderSize];
var rnd = new Random();
rnd.NextBytes(cryptBuffer);
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(cryptBuffer);
}
cryptBuffer[11] = (byte)(crcValue >> 24);
stream.Write(cryptBuffer, 0, cryptBuffer.Length);
}
Expand Down
8 changes: 6 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;

namespace ICSharpCode.SharpZipLib.Zip
{
Expand Down Expand Up @@ -627,8 +628,11 @@ private void WriteEncryptionHeader(long crcValue)
InitializePassword(Password);

byte[] cryptBuffer = new byte[ZipConstants.CryptoHeaderSize];
var rnd = new Random();
rnd.NextBytes(cryptBuffer);
using (var rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(cryptBuffer);
}

cryptBuffer[11] = (byte)(crcValue >> 24);

EncryptBlock(cryptBuffer, 0, cryptBuffer.Length);
Expand Down