Skip to content

Commit

Permalink
Move cryptoTransform setup machinery from DeflatorOutputStream to Zip…
Browse files Browse the repository at this point in the history
…OutputStream
  • Loading branch information
Numpsy committed Apr 28, 2021
1 parent 00871ba commit 2c30b8e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
Expand Up @@ -153,7 +153,10 @@ public bool CanPatchEntries

#region Encryption

private ICryptoTransform cryptoTransform_;
/// <summary>
/// The CryptoTransform currently being used to encrypt the compressed data.
/// </summary>
protected ICryptoTransform cryptoTransform_;

/// <summary>
/// Returns the 10 byte AUTH CODE to be appended immediately following the AES data stream.
Expand All @@ -177,34 +180,6 @@ protected void EncryptBlock(byte[] buffer, int offset, int length)
cryptoTransform_.TransformBlock(buffer, 0, length, buffer, 0);
}

/// <summary>
/// Initializes encryption keys based on given <paramref name="password"/>.
/// </summary>
/// <param name="password">The password.</param>
protected void InitializePassword(string password)
{
var pkManaged = new PkzipClassicManaged();
byte[] key = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(password));
cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
}

/// <summary>
/// Initializes encryption keys based on given password.
/// </summary>
protected void InitializeAESPassword(ZipEntry entry, string rawPassword,
out byte[] salt, out byte[] pwdVerifier)
{
salt = new byte[entry.AESSaltLen];
// Salt needs to be cryptographically random, and unique per file
if (_aesRnd == null)
_aesRnd = RandomNumberGenerator.Create();
_aesRnd.GetBytes(salt);
int blockSize = entry.AESKeySize / 8; // bits to bytes

cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true);
pwdVerifier = ((ZipAESTransform)cryptoTransform_).PwdVerifier;
}

#endregion Encryption

#region Deflation Support
Expand Down Expand Up @@ -459,12 +434,5 @@ public override void Write(byte[] buffer, int offset, int count)
private bool isClosed_;

#endregion Instance Fields

#region Static Fields

// Static to help ensure that multiple files within a zip will get different random salt
private static RandomNumberGenerator _aesRnd = RandomNumberGenerator.Create();

#endregion Static Fields
}
}
36 changes: 36 additions & 0 deletions src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
@@ -1,5 +1,6 @@
using ICSharpCode.SharpZipLib.Checksum;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Encryption;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using System;
Expand Down Expand Up @@ -657,6 +658,34 @@ public void CloseEntry()
curEntry = null;
}

/// <summary>
/// Initializes encryption keys based on given <paramref name="password"/>.
/// </summary>
/// <param name="password">The password.</param>
private void InitializePassword(string password)
{
var pkManaged = new PkzipClassicManaged();
byte[] key = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(password));
cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
}

/// <summary>
/// Initializes encryption keys based on given password.
/// </summary>
private void InitializeAESPassword(ZipEntry entry, string rawPassword,
out byte[] salt, out byte[] pwdVerifier)
{
salt = new byte[entry.AESSaltLen];
// Salt needs to be cryptographically random, and unique per file
if (_aesRnd == null)
_aesRnd = RandomNumberGenerator.Create();
_aesRnd.GetBytes(salt);
int blockSize = entry.AESKeySize / 8; // bits to bytes

cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true);
pwdVerifier = ((ZipAESTransform)cryptoTransform_).PwdVerifier;
}

private void WriteEncryptionHeader(long crcValue)
{
offset += ZipConstants.CryptoHeaderSize;
Expand Down Expand Up @@ -1039,5 +1068,12 @@ public override void Flush()
private string password;

#endregion Instance Fields

#region Static Fields

// Static to help ensure that multiple files within a zip will get different random salt
private static RandomNumberGenerator _aesRnd = RandomNumberGenerator.Create();

#endregion Static Fields
}
}

0 comments on commit 2c30b8e

Please sign in to comment.