wasSharpNET – Diff between revs 29 and 30
?pathlinks?
Rev 29 | Rev 30 | |||
---|---|---|---|---|
Line 6... | Line 6... | |||
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.Threading.Tasks; |
|
Line 11... | Line 12... | |||
11 | using System.Security.Cryptography; |
12 | using System.Security.Cryptography; |
|
12 | |
13 | |
|
13 | namespace wasSharpNET.Cryptography |
14 | namespace wasSharpNET.Cryptography |
|
14 | { |
15 | { |
|
15 | public class AES |
16 | public static class AES |
|
16 | { |
17 | { |
|
17 | private const int AES_BLOCK_SIZE = 128; |
18 | private const int AES_BLOCK_SIZE = 128; |
|
18 | private const CipherMode AES_CIPHER_MODE = CipherMode.CBC; |
19 | private const CipherMode AES_CIPHER_MODE = CipherMode.CBC; |
|
Line 28... | Line 29... | |||
28 | /// </summary> |
29 | /// </summary> |
|
29 | /// <param name="data">the string to encrypt</param> |
30 | /// <param name="data">the string to encrypt</param> |
|
30 | /// <param name="key">the encryption key</param> |
31 | /// <param name="key">the encryption key</param> |
|
31 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
32 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
|
32 | /// <returns>Base64 encoded encrypted data</returns> |
33 | /// <returns>Base64 encoded encrypted data</returns> |
|
33 | public string wasAESEncrypt(string data, string key, string separator = ":") |
34 | public static async Task<string> Encrypt(string data, string key, string separator = ":") |
|
34 | { |
35 | { |
|
35 | using (var rijdanelManaged = new RijndaelManaged()) |
36 | using (var rijdanelManaged = new RijndaelManaged()) |
|
36 | { |
37 | { |
|
37 | // FIPS-197 / CBC |
38 | // FIPS-197 / CBC |
|
38 | rijdanelManaged.BlockSize = AES_BLOCK_SIZE; |
39 | rijdanelManaged.BlockSize = AES_BLOCK_SIZE; |
|
Line 44... | Line 45... | |||
44 | rng.GetBytes(salt); |
45 | rng.GetBytes(salt); |
|
45 | var derivedKey = new Rfc2898DeriveBytes(key, salt); |
46 | var derivedKey = new Rfc2898DeriveBytes(key, salt); |
|
46 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); |
47 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); |
|
47 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); |
48 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); |
|
Line 48... | Line -... | |||
48 | |
- | ||
49 | byte[] encryptedData; |
49 | |
|
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 | await streamWriter.WriteAsync(data); |
||
58 | streamWriter.Write(data); |
59 | return string.Join(separator, Convert.ToBase64String(salt), Convert.ToBase64String(memoryStream.ToArray())); |
|
59 | } |
60 | } |
|
60 | } |
- | ||
61 | encryptedData = memoryStream.ToArray(); |
61 | } |
|
62 | } |
62 | } |
|
63 | } |
- | ||
64 | return string.Join(separator, Convert.ToBase64String(salt), Convert.ToBase64String(encryptedData)); |
63 | } |
|
65 | } |
64 | } |
|
Line 66... | Line 65... | |||
66 | } |
65 | } |
|
67 | |
66 | |
|
Line 76... | Line 75... | |||
76 | /// separator |
75 | /// separator |
|
77 | /// </param> |
76 | /// </param> |
|
78 | /// <param name="key">the encryption key</param> |
77 | /// <param name="key">the encryption key</param> |
|
79 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
78 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
|
80 | /// <returns>the decrypted data</returns> |
79 | /// <returns>the decrypted data</returns> |
|
81 | public string wasAESDecrypt(string data, string key, string separator = ":") |
80 | public static async Task<string> Decrypt(string data, string key, string separator = ":") |
|
82 | { |
81 | { |
|
83 | // retrieve the salt from the data. |
82 | // retrieve the salt from the data. |
|
84 | var segments = new List<string>(data.Split(new[] {separator}, StringSplitOptions.None)); |
83 | var segments = new List<string>(data.Split(new[] {separator}, StringSplitOptions.None)); |
|
85 | if (!segments.Count().Equals(2)) |
84 | if (!segments.Count().Equals(2)) |
|
86 | throw new ArgumentException("Invalid data."); |
85 | throw new ArgumentException("Invalid data."); |
|
Line 87... | Line -... | |||
87 | |
- | ||
88 | string plaintext; |
86 | |
|
89 | using (var rijdanelManaged = new RijndaelManaged()) |
87 | using (var rijdanelManaged = new RijndaelManaged()) |
|
90 | { |
88 | { |
|
91 | // FIPS-197 / CBC |
89 | // FIPS-197 / CBC |
|
92 | rijdanelManaged.BlockSize = AES_BLOCK_SIZE; |
90 | rijdanelManaged.BlockSize = AES_BLOCK_SIZE; |
|
Line 104... | Line 102... | |||
104 | { |
102 | { |
|
105 | using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) |
103 | using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) |
|
106 | { |
104 | { |
|
107 | using (var streamReader = new StreamReader(cryptoStream)) |
105 | using (var streamReader = new StreamReader(cryptoStream)) |
|
108 | { |
106 | { |
|
109 | plaintext = streamReader.ReadToEnd(); |
107 | return await streamReader.ReadToEndAsync(); |
|
110 | } |
108 | } |
|
111 | } |
109 | } |
|
112 | } |
110 | } |
|
113 | } |
111 | } |
|
114 | } |
112 | } |
|
115 | return plaintext; |
- | ||
116 | } |
113 | } |
|
117 | } |
114 | } |
|
118 | } |
115 | } |
|
119 | |
116 | |