|
| 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 | +} |
0 commit comments