wasSharpNET – Diff between revs 3 and 11
?pathlinks?
Rev 3 | Rev 11 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
6 | |
|
7 | using System; |
7 | using System; |
|
8 | using System.Collections.Generic; |
8 | using System.Collections.Generic; |
|
9 | using System.IO; |
9 | using System.IO; |
|
10 | using System.Linq; |
10 | using System.Linq; |
|
11 | using System.Security.Cryptography; |
11 | using System.Security.Cryptography; |
|
12 | |
12 | |
|
13 | namespace wasSharpNET.Cryptography |
13 | namespace wasSharpNET.Cryptography |
|
14 | { |
14 | { |
|
15 | public class AES |
15 | public class AES |
|
16 | { |
16 | { |
|
17 | private const int AES_BLOCK_SIZE = 128; |
17 | private const int AES_BLOCK_SIZE = 128; |
|
18 | private const CipherMode AES_CIPHER_MODE = CipherMode.CBC; |
18 | private const CipherMode AES_CIPHER_MODE = CipherMode.CBC; |
|
19 | private const PaddingMode AES_PADDING_MODE = PaddingMode.PKCS7; |
19 | private const PaddingMode AES_PADDING_MODE = PaddingMode.PKCS7; |
|
20 | private const int AES_KEY_SALT_BYTES = 16; |
20 | private const int AES_KEY_SALT_BYTES = 16; |
|
21 | private static readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); |
21 | private static readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); |
|
22 | |
22 | |
|
23 | /////////////////////////////////////////////////////////////////////////// |
23 | /////////////////////////////////////////////////////////////////////////// |
|
24 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
24 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
|
25 | /////////////////////////////////////////////////////////////////////////// |
25 | /////////////////////////////////////////////////////////////////////////// |
|
26 | /// <summary> |
26 | /// <summary> |
|
27 | /// Encrypts a string given a key and initialization vector. |
27 | /// Encrypts a string given a key and initialization vector. |
|
28 | /// </summary> |
28 | /// </summary> |
|
29 | /// <param name="data">the string to encrypt</param> |
29 | /// <param name="data">the string to encrypt</param> |
|
30 | /// <param name="key">the encryption key</param> |
30 | /// <param name="key">the encryption key</param> |
|
31 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
31 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
|
32 | /// <returns>Base64 encoded encrypted data</returns> |
32 | /// <returns>Base64 encoded encrypted data</returns> |
|
33 | public string wasAESEncrypt(string data, string key, string separator = ":") |
33 | public string wasAESEncrypt(string data, string key, string separator = ":") |
|
34 | { |
34 | { |
|
35 | using (var rijdanelManaged = new RijndaelManaged()) |
35 | using (var rijdanelManaged = new RijndaelManaged()) |
|
36 | { |
36 | { |
|
37 | // FIPS-197 / CBC |
37 | // FIPS-197 / CBC |
|
38 | rijdanelManaged.BlockSize = AES_BLOCK_SIZE; |
38 | rijdanelManaged.BlockSize = AES_BLOCK_SIZE; |
|
39 | rijdanelManaged.Mode = AES_CIPHER_MODE; |
39 | rijdanelManaged.Mode = AES_CIPHER_MODE; |
|
40 | rijdanelManaged.Padding = AES_PADDING_MODE; |
40 | rijdanelManaged.Padding = AES_PADDING_MODE; |
|
41 | |
41 | |
|
42 | // Compute the salt and the IV from the key. |
42 | // Compute the salt and the IV from the key. |
|
43 | var salt = new byte[AES_KEY_SALT_BYTES]; |
43 | var salt = new byte[AES_KEY_SALT_BYTES]; |
|
44 | rng.GetBytes(salt); |
44 | rng.GetBytes(salt); |
|
45 | var derivedKey = new Rfc2898DeriveBytes(key, salt); |
45 | var derivedKey = new Rfc2898DeriveBytes(key, salt); |
|
46 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize/8); |
46 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); |
|
47 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize/8); |
47 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); |
|
48 | |
48 | |
|
49 | byte[] encryptedData; |
49 | byte[] encryptedData; |
|
50 | using (var encryptor = rijdanelManaged.CreateEncryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
50 | using (var encryptor = rijdanelManaged.CreateEncryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
|
51 | { |
51 | { |
|
52 | using (var memoryStream = new MemoryStream()) |
52 | using (var memoryStream = new MemoryStream()) |
|
53 | { |
53 | { |
|
54 | using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) |
54 | using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) |
|
55 | { |
55 | { |
|
56 | using (var streamWriter = new StreamWriter(cryptoStream)) |
56 | using (var streamWriter = new StreamWriter(cryptoStream)) |
|
57 | { |
57 | { |
|
58 | streamWriter.Write(data); |
58 | streamWriter.Write(data); |
|
59 | } |
59 | } |
|
60 | } |
60 | } |
|
61 | encryptedData = memoryStream.ToArray(); |
61 | encryptedData = memoryStream.ToArray(); |
|
62 | } |
62 | } |
|
63 | } |
63 | } |
|
64 | return string.Join(separator, Convert.ToBase64String(salt), Convert.ToBase64String(encryptedData)); |
64 | return string.Join(separator, Convert.ToBase64String(salt), Convert.ToBase64String(encryptedData)); |
|
65 | } |
65 | } |
|
66 | } |
66 | } |
|
67 | |
67 | |
|
68 | /////////////////////////////////////////////////////////////////////////// |
68 | /////////////////////////////////////////////////////////////////////////// |
|
69 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
69 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
|
70 | /////////////////////////////////////////////////////////////////////////// |
70 | /////////////////////////////////////////////////////////////////////////// |
|
71 | /// <summary> |
71 | /// <summary> |
|
72 | /// Decrypts a Base64 encoded string using AES with a given key and initialization vector. |
72 | /// Decrypts a Base64 encoded string using AES with a given key and initialization vector. |
|
73 | /// </summary> |
73 | /// </summary> |
|
74 | /// <param name="data"> |
74 | /// <param name="data"> |
|
75 | /// a string consisting of the cyphertext to decrypt in Base64 and the IV in Base64 separated by the |
75 | /// a string consisting of the cyphertext to decrypt in Base64 and the IV in Base64 separated by the |
|
76 | /// separator |
76 | /// separator |
|
77 | /// </param> |
77 | /// </param> |
|
78 | /// <param name="key">the encryption key</param> |
78 | /// <param name="key">the encryption key</param> |
|
79 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
79 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
|
80 | /// <returns>the decrypted data</returns> |
80 | /// <returns>the decrypted data</returns> |
|
81 | public string wasAESDecrypt(string data, string key, string separator = ":") |
81 | public string wasAESDecrypt(string data, string key, string separator = ":") |
|
82 | { |
82 | { |
|
83 | // retrieve the salt from the data. |
83 | // retrieve the salt from the data. |
|
84 | var segments = new List<string>(data.Split(new[] {separator}, StringSplitOptions.None)); |
84 | var segments = new List<string>(data.Split(new[] { separator }, StringSplitOptions.None)); |
|
85 | if (!segments.Count.Equals(2)) |
85 | if (!segments.Count.Equals(2)) |
|
86 | throw new ArgumentException("Invalid data."); |
86 | throw new ArgumentException("Invalid data."); |
|
87 | |
87 | |
|
88 | string plaintext; |
88 | string plaintext; |
|
89 | using (var rijdanelManaged = new RijndaelManaged()) |
89 | using (var rijdanelManaged = new RijndaelManaged()) |
|
90 | { |
90 | { |
|
91 | // FIPS-197 / CBC |
91 | // FIPS-197 / CBC |
|
92 | rijdanelManaged.BlockSize = AES_BLOCK_SIZE; |
92 | rijdanelManaged.BlockSize = AES_BLOCK_SIZE; |
|
93 | rijdanelManaged.Mode = AES_CIPHER_MODE; |
93 | rijdanelManaged.Mode = AES_CIPHER_MODE; |
|
94 | rijdanelManaged.Padding = AES_PADDING_MODE; |
94 | rijdanelManaged.Padding = AES_PADDING_MODE; |
|
95 | |
95 | |
|
96 | // Retrieve the key and the IV from the salt. |
96 | // Retrieve the key and the IV from the salt. |
|
97 | var derivedKey = new Rfc2898DeriveBytes(key, Convert.FromBase64String(segments.First().Trim())); |
97 | var derivedKey = new Rfc2898DeriveBytes(key, Convert.FromBase64String(segments.First().Trim())); |
|
98 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize/8); |
98 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); |
|
99 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize/8); |
99 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); |
|
100 | |
100 | |
|
101 | using (var decryptor = rijdanelManaged.CreateDecryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
101 | using (var decryptor = rijdanelManaged.CreateDecryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
|
102 | { |
102 | { |
|
103 | using (var memoryStream = new MemoryStream(Convert.FromBase64String(segments.Last().Trim()))) |
103 | using (var memoryStream = new MemoryStream(Convert.FromBase64String(segments.Last().Trim()))) |
|
104 | { |
104 | { |
|
105 | using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) |
105 | using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) |
|
106 | { |
106 | { |
|
107 | using (var streamReader = new StreamReader(cryptoStream)) |
107 | using (var streamReader = new StreamReader(cryptoStream)) |
|
108 | { |
108 | { |
|
109 | plaintext = streamReader.ReadToEnd(); |
109 | plaintext = streamReader.ReadToEnd(); |
|
110 | } |
110 | } |
|
111 | } |
111 | } |
|
112 | } |
112 | } |
|
113 | } |
113 | } |
|
114 | } |
114 | } |
|
115 | return plaintext; |
115 | return plaintext; |
|
116 | } |
116 | } |
|
117 | } |
117 | } |
|
118 | } |
- | ||
119 | |
118 | } |
|
- | 119 | |
||
120 |
|
- | ||
121 | |
- | ||
122 | |
- | ||
123 | |
- |