wasSharpNET – Blame information for rev 27

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