WingMan – Diff between revs 32 and 34
?pathlinks?
Rev 32 | Rev 34 | |||
---|---|---|---|---|
Line 17... | Line 17... | |||
17 | |
17 | |
|
18 | /////////////////////////////////////////////////////////////////////////// |
18 | /////////////////////////////////////////////////////////////////////////// |
|
19 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
19 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
|
20 | /////////////////////////////////////////////////////////////////////////// |
20 | /////////////////////////////////////////////////////////////////////////// |
|
21 | /// <summary> |
21 | /// <summary> |
|
22 | /// Encrypts a string given a key and initialization vector. |
22 | /// Encrypts an input stream given a key and initialization vector. |
|
23 | /// </summary> |
23 | /// </summary> |
|
24 | /// <param name="data">the string to encrypt</param> |
24 | /// <param name="inputStream">the stream to encrypt</param> |
|
- | 25 | /// <param name="key">the encryption key</param> |
||
- | 26 | /// <returns>an encrypted stream containing the salt and the data</returns> |
||
- | 27 | public static async Task<MemoryStream> Encrypt(Stream inputStream, string key) |
||
- | 28 | { |
||
- | 29 | var outputStream = new MemoryStream(); |
||
- | 30 | |
||
- | 31 | using (var rijdanelManaged = new RijndaelManaged()) |
||
- | 32 | { |
||
- | 33 | // FIPS-197 / CBC |
||
- | 34 | rijdanelManaged.BlockSize = AesBlockSize; |
||
- | 35 | rijdanelManaged.Mode = AesCipherMode; |
||
- | 36 | rijdanelManaged.Padding = AesPaddingMode; |
||
- | 37 | |
||
- | 38 | // Compute the salt and the IV from the key. |
||
- | 39 | var salt = new byte[AesKeySaltBytes]; |
||
- | 40 | Rng.GetBytes(salt); |
||
- | 41 | var derivedKey = new Rfc2898DeriveBytes(key, salt, AesKeyIterations); |
||
- | 42 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); |
||
- | 43 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); |
||
- | 44 | |
||
- | 45 | using (var encryptor = rijdanelManaged.CreateEncryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
||
- | 46 | { |
||
- | 47 | using (var memoryStream = new MemoryStream()) |
||
- | 48 | { |
||
- | 49 | using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) |
||
- | 50 | { |
||
- | 51 | await inputStream.CopyToAsync(cryptoStream); |
||
- | 52 | cryptoStream.FlushFinalBlock(); |
||
- | 53 | |
||
- | 54 | memoryStream.Position = 0L; |
||
- | 55 | |
||
- | 56 | await outputStream.WriteAsync(salt, 0, AesKeySaltBytes); |
||
- | 57 | await memoryStream.CopyToAsync(outputStream); |
||
- | 58 | |
||
- | 59 | outputStream.Position = 0L; |
||
- | 60 | |
||
- | 61 | return outputStream; |
||
- | 62 | } |
||
- | 63 | } |
||
- | 64 | } |
||
- | 65 | } |
||
- | 66 | } |
||
- | 67 | |
||
- | 68 | /////////////////////////////////////////////////////////////////////////// |
||
- | 69 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
||
- | 70 | /////////////////////////////////////////////////////////////////////////// |
||
- | 71 | /// <summary> |
||
- | 72 | /// Encrypts a byte array given a key and initialization vector. |
||
25 | /// <param name="key">the encryption key</param> |
73 | /// </summary> |
|
- | 74 | /// <param name="data">the byte array to encrypt</param> |
||
26 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
75 | /// <param name="key">the encryption key</param> |
|
27 | /// <returns>Base64 encoded encrypted data</returns> |
76 | /// <returns>an encrypted byte array</returns> |
|
28 | public static async Task<byte[]> Encrypt(byte[] data, string key, string separator = ":") |
77 | public static async Task<byte[]> Encrypt(byte[] data, string key) |
|
29 | { |
78 | { |
|
30 | using (var rijdanelManaged = new RijndaelManaged()) |
79 | using (var rijdanelManaged = new RijndaelManaged()) |
|
31 | { |
80 | { |
|
32 | // FIPS-197 / CBC |
81 | // FIPS-197 / CBC |
|
Line 50... | Line 99... | |||
50 | using (var inputStream = new MemoryStream(data)) |
99 | using (var inputStream = new MemoryStream(data)) |
|
51 | { |
100 | { |
|
52 | await inputStream.CopyToAsync(cryptoStream); |
101 | await inputStream.CopyToAsync(cryptoStream); |
|
53 | cryptoStream.FlushFinalBlock(); |
102 | cryptoStream.FlushFinalBlock(); |
|
Line 54... | Line 103... | |||
54 | |
103 | |
|
Line 55... | Line 104... | |||
55 | inputStream.Position = 0L; |
104 | memoryStream.Position = 0L; |
|
56 | |
105 | |
|
57 | return salt.Concat(memoryStream.ToArray()).ToArray(); |
106 | return salt.Concat(memoryStream.ToArray()).ToArray(); |
|
58 | } |
107 | } |
|
Line 64... | Line 113... | |||
64 | |
113 | |
|
65 | /////////////////////////////////////////////////////////////////////////// |
114 | /////////////////////////////////////////////////////////////////////////// |
|
66 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
115 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
|
67 | /////////////////////////////////////////////////////////////////////////// |
116 | /////////////////////////////////////////////////////////////////////////// |
|
- | 117 | /// <summary> |
||
- | 118 | /// Decrypts a byte array using a salt and a key. |
||
- | 119 | /// </summary> |
||
- | 120 | /// <param name="inputStream">a salt and data stream to decrypt</param> |
||
- | 121 | /// <param name="key">the encryption key</param> |
||
- | 122 | /// <returns>a memory stream containing the decrypted data</returns> |
||
- | 123 | public static async Task<MemoryStream> Decrypt(Stream inputStream, string key) |
||
- | 124 | { |
||
- | 125 | var outputStream = new MemoryStream(); |
||
- | 126 | |
||
- | 127 | var salt = new byte[AesKeySaltBytes]; |
||
- | 128 | await inputStream.ReadAsync(salt, 0, AesKeySaltBytes); |
||
- | 129 | |
||
- | 130 | var text = new byte[inputStream.Length - AesKeySaltBytes]; |
||
- | 131 | await inputStream.ReadAsync(text, 0, (int) (inputStream.Length - AesKeySaltBytes)); |
||
- | 132 | |
||
- | 133 | //var salt = data.Take(AesKeySaltBytes).ToArray(); |
||
- | 134 | //var text = data.Skip(AesKeySaltBytes).ToArray(); |
||
- | 135 | |
||
- | 136 | using (var rijdanelManaged = new RijndaelManaged()) |
||
- | 137 | { |
||
- | 138 | // FIPS-197 / CBC |
||
- | 139 | rijdanelManaged.BlockSize = AesBlockSize; |
||
- | 140 | rijdanelManaged.Mode = AesCipherMode; |
||
- | 141 | rijdanelManaged.Padding = AesPaddingMode; |
||
- | 142 | |
||
- | 143 | // Retrieve the key and the IV from the salt. |
||
- | 144 | var derivedKey = new Rfc2898DeriveBytes(key, salt, AesKeyIterations); |
||
- | 145 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); |
||
- | 146 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); |
||
- | 147 | |
||
- | 148 | using (var decryptor = rijdanelManaged.CreateDecryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
||
- | 149 | { |
||
- | 150 | using (var memoryStream = new MemoryStream(text)) |
||
- | 151 | { |
||
- | 152 | using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) |
||
- | 153 | { |
||
- | 154 | await cryptoStream.CopyToAsync(outputStream); |
||
- | 155 | |
||
- | 156 | outputStream.Position = 0L; |
||
- | 157 | |
||
- | 158 | return outputStream; |
||
- | 159 | } |
||
- | 160 | } |
||
- | 161 | } |
||
- | 162 | } |
||
- | 163 | } |
||
- | 164 | |
||
- | 165 | /////////////////////////////////////////////////////////////////////////// |
||
- | 166 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
||
- | 167 | /////////////////////////////////////////////////////////////////////////// |
||
68 | /// <summary> |
168 | /// <summary> |
|
69 | /// Decrypts a Base64 encoded string using AES with a given key and initialization vector. |
169 | /// Decrypts a byte array using a salt and a key. |
|
70 | /// </summary> |
170 | /// </summary> |
|
71 | /// <param name="data"> |
- | ||
72 | /// a string consisting of the cyphertext to decrypt in Base64 and the IV in Base64 separated by the |
- | ||
73 | /// separator |
- | ||
74 | /// </param> |
171 | /// <param name="data">a salt and data byte array to decrypt</param> |
|
75 | /// <param name="key">the encryption key</param> |
- | ||
76 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
172 | /// <param name="key">the encryption key</param> |
|
77 | /// <returns>the decrypted data</returns> |
173 | /// <returns>a byte array containing the decrypted data</returns> |
|
78 | public static async Task<byte[]> Decrypt(byte[] data, string key, string separator = ":") |
174 | public static async Task<byte[]> Decrypt(byte[] data, string key) |
|
79 | { |
175 | { |
|
80 | var salt = data.Take(AesKeySaltBytes).ToArray(); |
176 | var salt = data.Take(AesKeySaltBytes).ToArray(); |
|
Line 81... | Line 177... | |||
81 | var text = data.Skip(AesKeySaltBytes).ToArray(); |
177 | var text = data.Skip(AesKeySaltBytes).ToArray(); |