Skip to content

Commit 501f99b

Browse files
committed
1 parent ffe6f95 commit 501f99b

File tree

2 files changed

+68
-61
lines changed

2 files changed

+68
-61
lines changed

src/ICSharpCode.SharpZipLib/Zip/Compression/Streams/DeflaterOutputStream.cs

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -154,37 +154,15 @@ public bool CanPatchEntries
154154

155155
#region Encryption
156156

157-
private string password;
158-
159-
private ICryptoTransform cryptoTransform_;
160-
161157
/// <summary>
162-
/// Returns the 10 byte AUTH CODE to be appended immediately following the AES data stream.
158+
/// The CryptoTransform currently being used to encrypt the compressed data.
163159
/// </summary>
164-
protected byte[] AESAuthCode;
160+
protected ICryptoTransform cryptoTransform_;
165161

166162
/// <summary>
167-
/// Get/set the password used for encryption.
163+
/// Returns the 10 byte AUTH CODE to be appended immediately following the AES data stream.
168164
/// </summary>
169-
/// <remarks>When set to null or if the password is empty no encryption is performed</remarks>
170-
public string Password
171-
{
172-
get
173-
{
174-
return password;
175-
}
176-
set
177-
{
178-
if ((value != null) && (value.Length == 0))
179-
{
180-
password = null;
181-
}
182-
else
183-
{
184-
password = value;
185-
}
186-
}
187-
}
165+
protected byte[] AESAuthCode;
188166

189167
/// <summary>
190168
/// Encrypt a block of data
@@ -203,34 +181,6 @@ protected void EncryptBlock(byte[] buffer, int offset, int length)
203181
cryptoTransform_.TransformBlock(buffer, 0, length, buffer, 0);
204182
}
205183

206-
/// <summary>
207-
/// Initializes encryption keys based on given <paramref name="password"/>.
208-
/// </summary>
209-
/// <param name="password">The password.</param>
210-
protected void InitializePassword(string password)
211-
{
212-
var pkManaged = new PkzipClassicManaged();
213-
byte[] key = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(password));
214-
cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
215-
}
216-
217-
/// <summary>
218-
/// Initializes encryption keys based on given password.
219-
/// </summary>
220-
protected void InitializeAESPassword(ZipEntry entry, string rawPassword,
221-
out byte[] salt, out byte[] pwdVerifier)
222-
{
223-
salt = new byte[entry.AESSaltLen];
224-
// Salt needs to be cryptographically random, and unique per file
225-
if (_aesRnd == null)
226-
_aesRnd = RandomNumberGenerator.Create();
227-
_aesRnd.GetBytes(salt);
228-
int blockSize = entry.AESKeySize / 8; // bits to bytes
229-
230-
cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true);
231-
pwdVerifier = ((ZipAESTransform)cryptoTransform_).PwdVerifier;
232-
}
233-
234184
#endregion Encryption
235185

236186
#region Deflation Support
@@ -485,12 +435,5 @@ public override void Write(byte[] buffer, int offset, int count)
485435
private bool isClosed_;
486436

487437
#endregion Instance Fields
488-
489-
#region Static Fields
490-
491-
// Static to help ensure that multiple files within a zip will get different random salt
492-
private static RandomNumberGenerator _aesRnd = RandomNumberGenerator.Create();
493-
494-
#endregion Static Fields
495438
}
496439
}

src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ICSharpCode.SharpZipLib.Checksum;
22
using ICSharpCode.SharpZipLib.Core;
3+
using ICSharpCode.SharpZipLib.Encryption;
34
using ICSharpCode.SharpZipLib.Zip.Compression;
45
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
56
using System;
@@ -150,6 +151,29 @@ public UseZip64 UseZip64
150151
/// </summary>
151152
public INameTransform NameTransform { get; set; } = new PathTransformer();
152153

154+
/// <summary>
155+
/// Get/set the password used for encryption.
156+
/// </summary>
157+
/// <remarks>When set to null or if the password is empty no encryption is performed</remarks>
158+
public string Password
159+
{
160+
get
161+
{
162+
return password;
163+
}
164+
set
165+
{
166+
if ((value != null) && (value.Length == 0))
167+
{
168+
password = null;
169+
}
170+
else
171+
{
172+
password = value;
173+
}
174+
}
175+
}
176+
153177
/// <summary>
154178
/// Write an unsigned short in little endian byte order.
155179
/// </summary>
@@ -630,6 +654,34 @@ public void CloseEntry()
630654
curEntry = null;
631655
}
632656

657+
/// <summary>
658+
/// Initializes encryption keys based on given <paramref name="password"/>.
659+
/// </summary>
660+
/// <param name="password">The password.</param>
661+
private void InitializePassword(string password)
662+
{
663+
var pkManaged = new PkzipClassicManaged();
664+
byte[] key = PkzipClassic.GenerateKeys(ZipStrings.ConvertToArray(password));
665+
cryptoTransform_ = pkManaged.CreateEncryptor(key, null);
666+
}
667+
668+
/// <summary>
669+
/// Initializes encryption keys based on given password.
670+
/// </summary>
671+
private void InitializeAESPassword(ZipEntry entry, string rawPassword,
672+
out byte[] salt, out byte[] pwdVerifier)
673+
{
674+
salt = new byte[entry.AESSaltLen];
675+
676+
// Salt needs to be cryptographically random, and unique per file
677+
_aesRnd.GetBytes(salt);
678+
679+
int blockSize = entry.AESKeySize / 8; // bits to bytes
680+
681+
cryptoTransform_ = new ZipAESTransform(rawPassword, salt, blockSize, true);
682+
pwdVerifier = ((ZipAESTransform)cryptoTransform_).PwdVerifier;
683+
}
684+
633685
private void WriteEncryptionHeader(long crcValue)
634686
{
635687
offset += ZipConstants.CryptoHeaderSize;
@@ -999,6 +1051,18 @@ public override void Flush()
9991051
// NOTE: Setting the size for entries before they are added is the best solution!
10001052
private UseZip64 useZip64_ = UseZip64.Dynamic;
10011053

1054+
/// <summary>
1055+
/// The password to use when encrypting archive entries.
1056+
/// </summary>
1057+
private string password;
1058+
10021059
#endregion Instance Fields
1060+
1061+
#region Static Fields
1062+
1063+
// Static to help ensure that multiple files within a zip will get different random salt
1064+
private static RandomNumberGenerator _aesRnd = RandomNumberGenerator.Create();
1065+
1066+
#endregion Static Fields
10031067
}
10041068
}

0 commit comments

Comments
 (0)