Skip to content

Commit d968a6f

Browse files
committed
Add AES encryption support to ZipFile
1 parent f36ca37 commit d968a6f

File tree

2 files changed

+193
-11
lines changed

2 files changed

+193
-11
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.IO;
3+
using System.Security.Cryptography;
4+
5+
namespace ICSharpCode.SharpZipLib.Encryption
6+
{
7+
/// <summary>
8+
/// Encrypts AES ZIP entries.
9+
/// </summary>
10+
/// <remarks>
11+
/// Based on information from http://www.winzip.com/aes_info.htm
12+
/// and http://www.gladman.me.uk/cryptography_technology/fileencrypt/
13+
/// </remarks>
14+
internal class ZipAESEncryptionStream : Stream
15+
{
16+
// The transform to use for encryption.
17+
private ZipAESTransform transform;
18+
19+
// The output stream to write the encrypted data to.
20+
private readonly Stream outputStream;
21+
22+
// Static to help ensure that multiple files within a zip will get different random salt
23+
private static readonly RandomNumberGenerator _aesRnd = RandomNumberGenerator.Create();
24+
25+
/// <summary>
26+
/// Constructor
27+
/// </summary>
28+
/// <param name="stream">The stream on which to perform the cryptographic transformation.</param>
29+
/// <param name="rawPassword">The password used to encrypt the entry.</param>
30+
/// <param name="saltLength">The length of the salt to use.</param>
31+
/// <param name="blockSize">The block size to use for transforming.</param>
32+
public ZipAESEncryptionStream(Stream stream, string rawPassword, int saltLength, int blockSize)
33+
{
34+
// Set up stream.
35+
this.outputStream = stream;
36+
37+
// Initialise the encryption transform.
38+
var salt = new byte[saltLength];
39+
40+
// Salt needs to be cryptographically random, and unique per file
41+
_aesRnd.GetBytes(salt);
42+
43+
this.transform = new ZipAESTransform(rawPassword, salt, blockSize, true);
44+
45+
// File format for AES:
46+
// Size (bytes) Content
47+
// ------------ -------
48+
// Variable Salt value
49+
// 2 Password verification value
50+
// Variable Encrypted file data
51+
// 10 Authentication code
52+
//
53+
// Value in the "compressed size" fields of the local file header and the central directory entry
54+
// is the total size of all the items listed above. In other words, it is the total size of the
55+
// salt value, password verification value, encrypted data, and authentication code.
56+
var pwdVerifier = this.transform.PwdVerifier;
57+
this.outputStream.Write(salt, 0, salt.Length);
58+
this.outputStream.Write(pwdVerifier, 0, pwdVerifier.Length);
59+
}
60+
61+
// This stream is write only.
62+
public override bool CanRead => false;
63+
64+
// We only support writing - no seeking about.
65+
public override bool CanSeek => false;
66+
67+
// Supports writing for encrypting.
68+
public override bool CanWrite => true;
69+
70+
// We don't track this.
71+
public override long Length => throw new NotImplementedException();
72+
73+
// We don't track this, or support seeking.
74+
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
75+
76+
/// <summary>
77+
/// When the stream is disposed, write the final blocks and AES Authentication code
78+
/// </summary>
79+
protected override void Dispose(bool disposing)
80+
{
81+
if (this.transform != null)
82+
{
83+
this.WriteAuthCode();
84+
this.transform.Dispose();
85+
this.transform = null;
86+
}
87+
}
88+
89+
// <inheritdoc/>
90+
public override void Flush()
91+
{
92+
this.outputStream.Flush();
93+
}
94+
95+
// <inheritdoc/>
96+
public override int Read(byte[] buffer, int offset, int count)
97+
{
98+
// ZipAESEncryptionStream is only used for encryption.
99+
throw new NotImplementedException();
100+
}
101+
102+
// <inheritdoc/>
103+
public override long Seek(long offset, SeekOrigin origin)
104+
{
105+
// We don't support seeking.
106+
throw new NotImplementedException();
107+
}
108+
109+
// <inheritdoc/>
110+
public override void SetLength(long value)
111+
{
112+
// We don't support setting the length.
113+
throw new NotImplementedException();
114+
}
115+
116+
// <inheritdoc/>
117+
public override void Write(byte[] buffer, int offset, int count)
118+
{
119+
if (count == 0)
120+
{
121+
return;
122+
}
123+
124+
var outputBuffer = new byte[count];
125+
var outputCount = this.transform.TransformBlock(buffer, offset, count, outputBuffer, 0);
126+
this.outputStream.Write(outputBuffer, 0, outputCount);
127+
}
128+
129+
// Write the auth code for the encrypted data to the output stream
130+
private void WriteAuthCode()
131+
{
132+
// Transform the final block?
133+
134+
// Write the AES Authentication Code (a hash of the compressed and encrypted data)
135+
var authCode = this.transform.GetAuthCode();
136+
this.outputStream.Write(authCode, 0, 10);
137+
this.outputStream.Flush();
138+
}
139+
}
140+
}

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,10 +1864,10 @@ public void Add(IStaticDataSource dataSource, ZipEntry entry)
18641864

18651865
// We don't currently support adding entries with AES encryption, so throw
18661866
// up front instead of failing or falling back to ZipCrypto later on
1867-
if (entry.AESKeySize > 0)
1868-
{
1869-
throw new NotSupportedException("Creation of AES encrypted entries is not supported");
1870-
}
1867+
//if (entry.AESKeySize > 0)
1868+
//{
1869+
// throw new NotSupportedException("Creation of AES encrypted entries is not supported");
1870+
//}
18711871

18721872
CheckSupportedCompressionMethod(entry.CompressionMethod);
18731873
CheckUpdating();
@@ -2158,6 +2158,12 @@ private void WriteLocalEntryHeader(ZipUpdate update)
21582158
ed.Delete(1);
21592159
}
21602160

2161+
// Write AES Data if needed
2162+
if (entry.AESKeySize > 0)
2163+
{
2164+
AddExtraDataAES(entry, ed);
2165+
}
2166+
21612167
entry.ExtraData = ed.GetEntryData();
21622168

21632169
WriteLEShort(name.Length);
@@ -2281,6 +2287,11 @@ private int WriteCentralDirectoryHeader(ZipEntry entry)
22812287
ed.Delete(1);
22822288
}
22832289

2290+
if (entry.AESKeySize > 0)
2291+
{
2292+
AddExtraDataAES(entry, ed);
2293+
}
2294+
22842295
byte[] centralExtraData = ed.GetEntryData();
22852296

22862297
WriteLEShort(centralExtraData.Length);
@@ -2335,6 +2346,22 @@ private int WriteCentralDirectoryHeader(ZipEntry entry)
23352346
return ZipConstants.CentralHeaderBaseSize + name.Length + centralExtraData.Length + rawComment.Length;
23362347
}
23372348

2349+
private static void AddExtraDataAES(ZipEntry entry, ZipExtraData extraData)
2350+
{
2351+
// Vendor Version: AE-1 IS 1. AE-2 is 2. With AE-2 no CRC is required and 0 is stored.
2352+
const int VENDOR_VERSION = 2;
2353+
// Vendor ID is the two ASCII characters "AE".
2354+
const int VENDOR_ID = 0x4541; //not 6965;
2355+
extraData.StartNewEntry();
2356+
// Pack AES extra data field see http://www.winzip.com/aes_info.htm
2357+
//extraData.AddLeShort(7); // Data size (currently 7)
2358+
extraData.AddLeShort(VENDOR_VERSION); // 2 = AE-2
2359+
extraData.AddLeShort(VENDOR_ID); // "AE"
2360+
extraData.AddData(entry.AESEncryptionStrength); // 1 = 128, 2 = 192, 3 = 256
2361+
extraData.AddLeShort((int)entry.CompressionMethod); // The actual compression method used to compress the file
2362+
extraData.AddNewEntry(0x9901);
2363+
}
2364+
23382365
#endregion Writing Values/Headers
23392366

23402367
private void PostUpdateCleanup()
@@ -2621,13 +2648,20 @@ private Stream GetOutputStream(ZipEntry entry)
26212648
switch (entry.CompressionMethod)
26222649
{
26232650
case CompressionMethod.Stored:
2624-
result = new UncompressedStream(result);
2651+
if (!entry.IsCrypted)
2652+
{
2653+
// If there is an encryption stream in use, that can be written to directly
2654+
// otherwise, wrap it in an UncompressedStream instead of returning the base stream directly
2655+
result = new UncompressedStream(result);
2656+
}
26252657
break;
26262658

26272659
case CompressionMethod.Deflated:
26282660
var dos = new DeflaterOutputStream(result, new Deflater(9, true))
26292661
{
2630-
IsStreamOwner = false
2662+
// If there is an encryption stream in use, then we want that to be disposed when the deflator stream is disposed
2663+
// If not, then we don't want it to dispose the base stream
2664+
IsStreamOwner = entry.IsCrypted
26312665
};
26322666
result = dos;
26332667
break;
@@ -3667,9 +3701,16 @@ private Stream CreateAndInitDecryptionStream(Stream baseStream, ZipEntry entry)
36673701

36683702
private Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
36693703
{
3670-
CryptoStream result = null;
3671-
if ((entry.Version < ZipConstants.VersionStrongEncryption)
3672-
|| (entry.Flags & (int)GeneralBitFlags.StrongEncryption) == 0)
3704+
if (entry.CompressionMethodForHeader == CompressionMethod.WinZipAES)
3705+
{
3706+
int blockSize = entry.AESKeySize / 8; // bits to bytes
3707+
3708+
var aesStream =
3709+
new ZipAESEncryptionStream(baseStream, rawPassword_, entry.AESSaltLen, blockSize);
3710+
3711+
return aesStream;
3712+
}
3713+
else
36733714
{
36743715
var classicManaged = new PkzipClassicManaged();
36753716

@@ -3681,7 +3722,7 @@ private Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
36813722

36823723
// Closing a CryptoStream will close the base stream as well so wrap it in an UncompressedStream
36833724
// which doesnt do this.
3684-
result = new CryptoStream(new UncompressedStream(baseStream),
3725+
CryptoStream result = new CryptoStream(new UncompressedStream(baseStream),
36853726
classicManaged.CreateEncryptor(key, null), CryptoStreamMode.Write);
36863727

36873728
if ((entry.Crc < 0) || (entry.Flags & 8) != 0)
@@ -3692,8 +3733,9 @@ private Stream CreateAndInitEncryptionStream(Stream baseStream, ZipEntry entry)
36923733
{
36933734
WriteEncryptionHeader(result, entry.Crc);
36943735
}
3736+
3737+
return result;
36953738
}
3696-
return result;
36973739
}
36983740

36993741
private static void CheckClassicPassword(CryptoStream classicCryptoStream, ZipEntry entry)

0 commit comments

Comments
 (0)