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