wasSharpNET – Diff between revs 1 and 3

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