Winify – Blame information for rev 61
?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 | { |
61 | office | 31 | using var rijndael = Rijndael.Create(); |
32 | rijndael.BlockSize = 128; |
||
33 | rijndael.Mode = CipherMode.CBC; |
||
34 | rijndael.KeySize = 256; |
||
35 | rijndael.Padding = PaddingMode.PKCS7; |
||
36 | var salt = new byte[AESKeySaltBytes]; |
||
37 | Rng.GetBytes(salt); |
||
38 | using var pdb = new Rfc2898DeriveBytes(password, salt, 1000); |
||
39 | rijndael.Key = pdb.GetBytes(rijndael.KeySize / 8); |
||
40 | rijndael.IV = pdb.GetBytes(rijndael.BlockSize / 8); |
||
14 | office | 41 | |
61 | office | 42 | using var memoryStream = new MemoryStream(); |
43 | using var cryptoStream = |
||
44 | new CryptoStream(memoryStream, rijndael.CreateEncryptor(), CryptoStreamMode.Write); |
||
45 | await cryptoStream.WriteAsync(plain, 0, plain.Length); |
||
46 | cryptoStream.Close(); |
||
14 | office | 47 | |
61 | office | 48 | var ciphertext = memoryStream.ToArray(); |
14 | office | 49 | |
61 | office | 50 | return salt.Concat(ciphertext).ToArray(); |
14 | office | 51 | } |
52 | |||
53 | /////////////////////////////////////////////////////////////////////////// |
||
54 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
||
55 | /////////////////////////////////////////////////////////////////////////// |
||
41 | office | 56 | public static async Task<byte[]> Decrypt(byte[] cipher, string password) |
14 | office | 57 | { |
61 | office | 58 | using var rijndael = Rijndael.Create(); |
59 | rijndael.BlockSize = 128; |
||
60 | rijndael.Mode = CipherMode.CBC; |
||
61 | rijndael.KeySize = 256; |
||
62 | rijndael.Padding = PaddingMode.PKCS7; |
||
14 | office | 63 | |
61 | office | 64 | var salt = cipher.Take(AESKeySaltBytes).ToArray(); |
14 | office | 65 | |
61 | office | 66 | var ciphertext = cipher.Skip(AESKeySaltBytes).ToArray(); |
14 | office | 67 | |
61 | office | 68 | using var pdb = new Rfc2898DeriveBytes(password, salt, 1000); |
69 | rijndael.Key = pdb.GetBytes(rijndael.KeySize / 8); |
||
70 | rijndael.IV = pdb.GetBytes(rijndael.BlockSize / 8); |
||
14 | office | 71 | |
61 | office | 72 | using var memoryStream = new MemoryStream(); |
73 | using var cryptoStream = |
||
74 | new CryptoStream(memoryStream, rijndael.CreateDecryptor(), CryptoStreamMode.Write); |
||
75 | await cryptoStream.WriteAsync(ciphertext, 0, ciphertext.Length); |
||
76 | cryptoStream.Close(); |
||
14 | office | 77 | |
61 | office | 78 | return memoryStream.ToArray(); |
14 | office | 79 | } |
80 | |||
81 | #endregion |
||
82 | } |
||
83 | } |