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