WingMan – Diff between revs 8 and 10

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 8 Rev 10
Line 1... Line 1...
1 using System; 1 using System;
2 using System.IO; 2 using System.IO;
3 using System.Security.Cryptography; 3 using System.Security.Cryptography;
4 using System.Text; 4 using System.Text;
5 using System.Threading.Tasks; -  
Line 6... Line 5...
6   5  
7 namespace WingMan.Utilities 6 namespace WingMan.Utilities
8 { 7 {
9 public static class AES 8 public static class AES
Line 22... Line 21...
22 /// </summary> 21 /// </summary>
23 /// <param name="data">the string to encrypt</param> 22 /// <param name="data">the string to encrypt</param>
24 /// <param name="key">the encryption key</param> 23 /// <param name="key">the encryption key</param>
25 /// <param name="separator">the separator to use between the cyphertext and the IV</param> 24 /// <param name="separator">the separator to use between the cyphertext and the IV</param>
26 /// <returns>Base64 encoded encrypted data</returns> 25 /// <returns>Base64 encoded encrypted data</returns>
27 public static async Task<byte[]> Encrypt(byte[] data, string key, string separator = ":") 26 public static byte[] Encrypt(byte[] data, string key, string separator = ":")
28 { 27 {
29 using (var rijdanelManaged = new RijndaelManaged()) 28 using (var rijdanelManaged = new RijndaelManaged())
30 { 29 {
31 // FIPS-197 / CBC 30 // FIPS-197 / CBC
32 rijdanelManaged.BlockSize = AesBlockSize; 31 rijdanelManaged.BlockSize = AesBlockSize;
Line 34... Line 33...
34 rijdanelManaged.Padding = AesPaddingMode; 33 rijdanelManaged.Padding = AesPaddingMode;
Line 35... Line 34...
35   34  
36 // Compute the salt and the IV from the key. 35 // Compute the salt and the IV from the key.
37 var salt = new byte[AesKeySaltBytes]; 36 var salt = new byte[AesKeySaltBytes];
38 Rng.GetBytes(salt); 37 Rng.GetBytes(salt);
39 var derivedKey = new Rfc2898DeriveBytes(key, salt); -  
40 rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); -  
41 rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); -  
42   -  
43 using (var encryptor = rijdanelManaged.CreateEncryptor(rijdanelManaged.Key, rijdanelManaged.IV)) 38 using (var derivedKey = new Rfc2898DeriveBytes(key, salt))
44 { 39 {
-   40 rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8);
-   41 rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8);
-   42  
45 using (var memoryStream = new MemoryStream()) 43 using (var encryptor = rijdanelManaged.CreateEncryptor(rijdanelManaged.Key, rijdanelManaged.IV))
46 { 44 {
47 using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) 45 using (var memoryStream = new MemoryStream())
48 { 46 {
49 using (var inputStream = new MemoryStream(data)) 47 using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
-   48 {
-   49 using (var inputStream = new MemoryStream(data))
50 { 50 {
51 await inputStream.CopyToAsync(cryptoStream).ConfigureAwait(false); 51 inputStream.CopyTo(cryptoStream);
Line 52... Line 52...
52 cryptoStream.FlushFinalBlock(); 52 cryptoStream.FlushFinalBlock();
Line 53... Line 53...
53   53  
Line 54... Line 54...
54 inputStream.Position = 0L; 54 inputStream.Position = 0L;
55   55  
Line 56... Line 56...
56 var payload = memoryStream.ToArray(); 56 var payload = memoryStream.ToArray();
-   57  
57   58 var base64Salt = Convert.ToBase64String(salt);
58 var base64Salt = Convert.ToBase64String(salt); 59 var base64Payload = Convert.ToBase64String(payload);
59 var base64Payload = Convert.ToBase64String(payload); 60  
60   61 return Encoding.UTF8.GetBytes($"{base64Salt}{separator}{base64Payload}");
61 return Encoding.UTF8.GetBytes($"{base64Salt}{separator}{base64Payload}"); 62 }
Line 77... Line 78...
77 /// separator 78 /// separator
78 /// </param> 79 /// </param>
79 /// <param name="key">the encryption key</param> 80 /// <param name="key">the encryption key</param>
80 /// <param name="separator">the separator to use between the cyphertext and the IV</param> 81 /// <param name="separator">the separator to use between the cyphertext and the IV</param>
81 /// <returns>the decrypted data</returns> 82 /// <returns>the decrypted data</returns>
82 public static async Task<byte[]> Decrypt(byte[] data, string key, string separator = ":") 83 public static byte[] Decrypt(byte[] data, string key, string separator = ":")
83 { 84 {
84 var input = Encoding.UTF8.GetString(data); 85 var input = Encoding.UTF8.GetString(data);
Line 85... Line 86...
85   86  
86 // retrieve the salt from the data. 87 // retrieve the salt from the data.
87 var segments = input.Split(new[] {separator}, StringSplitOptions.None); 88 var segments = input.Split(new[] {separator}, StringSplitOptions.None);
88 if (segments.Length != 2) 89 if (segments.Length != 2)
Line 89... Line 90...
89 throw new ArgumentException("Invalid data."); 90 throw new ArgumentException("Invalid data: " + input);
90   91  
91 using (var rijdanelManaged = new RijndaelManaged()) 92 using (var rijdanelManaged = new RijndaelManaged())
92 { 93 {
93 // FIPS-197 / CBC 94 // FIPS-197 / CBC
94 rijdanelManaged.BlockSize = AesBlockSize; 95 rijdanelManaged.BlockSize = AesBlockSize;
Line 95... Line 96...
95 rijdanelManaged.Mode = AesCipherMode; 96 rijdanelManaged.Mode = AesCipherMode;
96 rijdanelManaged.Padding = AesPaddingMode; 97 rijdanelManaged.Padding = AesPaddingMode;
97   -  
98 // Retrieve the key and the IV from the salt. -  
99 var derivedKey = new Rfc2898DeriveBytes(key, Convert.FromBase64String(segments[0].Trim())); -  
100 rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); -  
101 rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); 98  
-   99 // Retrieve the key and the IV from the salt.
-   100 using (var derivedKey = new Rfc2898DeriveBytes(key, Convert.FromBase64String(segments[0].Trim())))
-   101 {
102   102 rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8);
103 using (var decryptor = rijdanelManaged.CreateDecryptor(rijdanelManaged.Key, rijdanelManaged.IV)) 103 rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8);
104 { 104  
105 using (var memoryStream = new MemoryStream(Convert.FromBase64String(segments[1].Trim()))) 105 using (var decryptor = rijdanelManaged.CreateDecryptor(rijdanelManaged.Key, rijdanelManaged.IV))
106 { 106 {
107 using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) 107 using (var memoryStream = new MemoryStream(Convert.FromBase64String(segments[1].Trim())))
-   108 {
-   109 using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
108 { 110 {
109 using (var streamReader = new StreamReader(cryptoStream)) 111 using (var streamReader = new StreamReader(cryptoStream))
110 { 112 {
111 return Encoding.UTF8.GetBytes(await streamReader.ReadToEndAsync() 113 return Encoding.UTF8.GetBytes(streamReader.ReadToEnd());
112 .ConfigureAwait(false)); 114 }
113 } 115 }
114 } 116 }
115 } 117 }
Line 116... Line 118...
116 } 118 }
117 } 119 }
118 } 120 }
119   121  
120 public static string LinearFeedbackShiftPassword(string password, int size = 32) 122 public static string ExpandKey(string password, int size = 32)
121 { 123 {