Winify – Diff between revs 14 and 28
?pathlinks?
Rev 14 | Rev 28 | |||
---|---|---|---|---|
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.IO; |
7 | using System.IO; |
|
8 | using System.Linq; |
8 | using System.Linq; |
|
9 | using System.Security.Cryptography; |
9 | using System.Security.Cryptography; |
|
10 | |
10 | |
|
11 | namespace Winify.Utilities |
11 | namespace Winify.Utilities |
|
12 | { |
12 | { |
|
13 | public static class AES |
13 | public static class AES |
|
14 | { |
14 | { |
|
15 | #region Static Fields and Constants |
15 | #region Static Fields and Constants |
|
16 | |
16 | |
|
17 | private const int AESKeySaltBytes = 16; |
17 | private const int AESKeySaltBytes = 16; |
|
18 | |
18 | |
|
19 | private static readonly RNGCryptoServiceProvider Rng = new RNGCryptoServiceProvider(); |
19 | private static readonly RNGCryptoServiceProvider Rng = new RNGCryptoServiceProvider(); |
|
20 | |
20 | |
|
21 | #endregion |
21 | #endregion |
|
22 | |
22 | |
|
23 | #region Public Methods |
23 | #region Public Methods |
|
24 | |
24 | |
|
25 | /////////////////////////////////////////////////////////////////////////// |
25 | /////////////////////////////////////////////////////////////////////////// |
|
26 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
26 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
|
27 | /////////////////////////////////////////////////////////////////////////// |
27 | /////////////////////////////////////////////////////////////////////////// |
|
28 | public static byte[] Encrypt(byte[] plain, string password) |
28 | public static byte[] Encrypt(byte[] plain, string password) |
|
29 | { |
29 | { |
|
30 | using (var rijndael = Rijndael.Create()) |
30 | using (var rijndael = Rijndael.Create()) |
|
31 | { |
31 | { |
|
32 | rijndael.BlockSize = 128; |
32 | rijndael.BlockSize = 128; |
|
33 | rijndael.Mode = CipherMode.CBC; |
33 | rijndael.Mode = CipherMode.CBC; |
|
34 | rijndael.KeySize = 256; |
34 | rijndael.KeySize = 256; |
|
35 | rijndael.Padding = PaddingMode.PKCS7; |
35 | rijndael.Padding = PaddingMode.PKCS7; |
|
36 | var salt = new byte[AESKeySaltBytes]; |
36 | var salt = new byte[AESKeySaltBytes]; |
|
37 | Rng.GetBytes(salt); |
37 | Rng.GetBytes(salt); |
|
38 | var pdb = new Rfc2898DeriveBytes(password, salt, 1000); |
38 | var pdb = new Rfc2898DeriveBytes(password, salt, 1000); |
|
39 | rijndael.Key = pdb.GetBytes(rijndael.KeySize / 8); |
39 | rijndael.Key = pdb.GetBytes(rijndael.KeySize / 8); |
|
40 | rijndael.IV = pdb.GetBytes(rijndael.BlockSize / 8); |
40 | rijndael.IV = pdb.GetBytes(rijndael.BlockSize / 8); |
|
41 | |
41 | |
|
42 | using (var memoryStream = new MemoryStream()) |
42 | using (var memoryStream = new MemoryStream()) |
|
43 | { |
43 | { |
|
44 | using (var cryptoStream = |
44 | using (var cryptoStream = |
|
45 | new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write)) |
45 | new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write)) |
|
46 | { |
46 | { |
|
47 | cryptoStream.Write(plain, 0, plain.Length); |
47 | cryptoStream.Write(plain, 0, plain.Length); |
|
48 | cryptoStream.Close(); |
48 | cryptoStream.Close(); |
|
49 | |
49 | |
|
50 | var ciphertext = memoryStream.ToArray(); |
50 | var ciphertext = memoryStream.ToArray(); |
|
51 | |
51 | |
|
52 | return salt.Concat(ciphertext) |
52 | return salt.Concat(ciphertext) |
|
53 | .ToArray(); |
53 | .ToArray(); |
|
54 | } |
54 | } |
|
55 | } |
55 | } |
|
56 | } |
56 | } |
|
57 | } |
57 | } |
|
58 | |
58 | |
|
59 | /////////////////////////////////////////////////////////////////////////// |
59 | /////////////////////////////////////////////////////////////////////////// |
|
60 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
60 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
|
61 | /////////////////////////////////////////////////////////////////////////// |
61 | /////////////////////////////////////////////////////////////////////////// |
|
62 | public static byte[] Decrypt(byte[] cipher, string password) |
62 | public static byte[] Decrypt(byte[] cipher, string password) |
|
63 | { |
63 | { |
|
64 | using (var rijndael = Rijndael.Create()) |
64 | using (var rijndael = Rijndael.Create()) |
|
65 | { |
65 | { |
|
66 | rijndael.BlockSize = 128; |
66 | rijndael.BlockSize = 128; |
|
67 | rijndael.Mode = CipherMode.CBC; |
67 | rijndael.Mode = CipherMode.CBC; |
|
68 | rijndael.KeySize = 256; |
68 | rijndael.KeySize = 256; |
|
69 | rijndael.Padding = PaddingMode.PKCS7; |
69 | rijndael.Padding = PaddingMode.PKCS7; |
|
70 | |
70 | |
|
71 | var salt = cipher.Take(16) |
71 | var salt = cipher.Take(16) |
|
72 | .ToArray(); |
72 | .ToArray(); |
|
73 | |
73 | |
|
74 | var ciphertext = cipher.Skip(16) |
74 | var ciphertext = cipher.Skip(16) |
|
75 | .ToArray(); |
75 | .ToArray(); |
|
76 | |
76 | |
|
77 | var pdb = new Rfc2898DeriveBytes(password, salt, 1000); |
77 | var pdb = new Rfc2898DeriveBytes(password, salt, 1000); |
|
78 | rijndael.Key = pdb.GetBytes(rijndael.KeySize / 8); |
78 | rijndael.Key = pdb.GetBytes(rijndael.KeySize / 8); |
|
79 | rijndael.IV = pdb.GetBytes(rijndael.BlockSize / 8); |
79 | rijndael.IV = pdb.GetBytes(rijndael.BlockSize / 8); |
|
80 | |
80 | |
|
81 | using (var memoryStream = new MemoryStream()) |
81 | using (var memoryStream = new MemoryStream()) |
|
82 | { |
82 | { |
|
83 | using (var cryptoStream = |
83 | using (var cryptoStream = |
|
84 | new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write)) |
84 | new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write)) |
|
85 | { |
85 | { |
|
86 | cryptoStream.Write(ciphertext, 0, ciphertext.Length); |
86 | cryptoStream.Write(ciphertext, 0, ciphertext.Length); |
|
87 | cryptoStream.Close(); |
87 | cryptoStream.Close(); |
|
88 | |
88 | |
|
89 | return memoryStream.ToArray(); |
89 | return memoryStream.ToArray(); |
|
90 | } |
90 | } |
|
91 | } |
91 | } |
|
92 | } |
92 | } |
|
93 | } |
93 | } |
|
94 | |
94 | |
|
95 | #endregion |
95 | #endregion |
|
96 | } |
96 | } |
|
97 | } |
97 | } |
|
98 | |
98 | |
|
99 |
|
99 |
|
|
100 | |
100 | |
|
101 | |
101 | |
|
102 | |
102 | |