wasSharpNET – Diff between revs 29 and 30

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 29 Rev 30
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.Threading.Tasks;
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;
19 private const PaddingMode AES_PADDING_MODE = PaddingMode.PKCS7; 20 private const PaddingMode AES_PADDING_MODE = PaddingMode.PKCS7;
20 private const int AES_KEY_SALT_BYTES = 16; 21 private const int AES_KEY_SALT_BYTES = 16;
21 private static readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); 22 private static readonly RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
22   23  
23 /////////////////////////////////////////////////////////////////////////// 24 ///////////////////////////////////////////////////////////////////////////
24 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // 25 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
25 /////////////////////////////////////////////////////////////////////////// 26 ///////////////////////////////////////////////////////////////////////////
26 /// <summary> 27 /// <summary>
27 /// Encrypts a string given a key and initialization vector. 28 /// Encrypts a string given a key and initialization vector.
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;
39 rijdanelManaged.Mode = AES_CIPHER_MODE; 40 rijdanelManaged.Mode = AES_CIPHER_MODE;
40 rijdanelManaged.Padding = AES_PADDING_MODE; 41 rijdanelManaged.Padding = AES_PADDING_MODE;
41   42  
42 // Compute the salt and the IV from the key. 43 // Compute the salt and the IV from the key.
43 var salt = new byte[AES_KEY_SALT_BYTES]; 44 var salt = new byte[AES_KEY_SALT_BYTES];
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);
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 streamWriter.Write(data); 58 await streamWriter.WriteAsync(data);
-   59 return string.Join(separator, Convert.ToBase64String(salt), Convert.ToBase64String(memoryStream.ToArray()));
59 } 60 }
60 } 61 }
61 encryptedData = memoryStream.ToArray(); -  
62 } 62 }
63 } 63 }
64 return string.Join(separator, Convert.ToBase64String(salt), Convert.ToBase64String(encryptedData)); -  
65 } 64 }
66 } 65 }
67   66  
68 /////////////////////////////////////////////////////////////////////////// 67 ///////////////////////////////////////////////////////////////////////////
69 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // 68 // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 //
70 /////////////////////////////////////////////////////////////////////////// 69 ///////////////////////////////////////////////////////////////////////////
71 /// <summary> 70 /// <summary>
72 /// Decrypts a Base64 encoded string using AES with a given key and initialization vector. 71 /// Decrypts a Base64 encoded string using AES with a given key and initialization vector.
73 /// </summary> 72 /// </summary>
74 /// <param name="data"> 73 /// <param name="data">
75 /// a string consisting of the cyphertext to decrypt in Base64 and the IV in Base64 separated by the 74 /// a string consisting of the cyphertext to decrypt in Base64 and the IV in Base64 separated by the
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.");
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;
93 rijdanelManaged.Mode = AES_CIPHER_MODE; 91 rijdanelManaged.Mode = AES_CIPHER_MODE;
94 rijdanelManaged.Padding = AES_PADDING_MODE; 92 rijdanelManaged.Padding = AES_PADDING_MODE;
95   93  
96 // Retrieve the key and the IV from the salt. 94 // Retrieve the key and the IV from the salt.
97 var derivedKey = new Rfc2898DeriveBytes(key, Convert.FromBase64String(segments.First().Trim())); 95 var derivedKey = new Rfc2898DeriveBytes(key, Convert.FromBase64String(segments.First().Trim()));
98 rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); 96 rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8);
99 rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); 97 rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8);
100   98  
101 using (var decryptor = rijdanelManaged.CreateDecryptor(rijdanelManaged.Key, rijdanelManaged.IV)) 99 using (var decryptor = rijdanelManaged.CreateDecryptor(rijdanelManaged.Key, rijdanelManaged.IV))
102 { 100 {
103 using (var memoryStream = new MemoryStream(Convert.FromBase64String(segments.Last().Trim()))) 101 using (var memoryStream = new MemoryStream(Convert.FromBase64String(segments.Last().Trim())))
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  
120
Generated by GNU Enscript 1.6.5.90.
117
Generated by GNU Enscript 1.6.5.90.
121   118  
122   119  
123   120