wasSharpNET – Diff between revs 3 and 11
?pathlinks?
Rev 3 | Rev 11 | |||
---|---|---|---|---|
Line 41... | Line 41... | |||
41 | |
41 | |
|
42 | // Compute the salt and the IV from the key. |
42 | // Compute the salt and the IV from the key. |
|
43 | var salt = new byte[AES_KEY_SALT_BYTES]; |
43 | var salt = new byte[AES_KEY_SALT_BYTES]; |
|
44 | rng.GetBytes(salt); |
44 | rng.GetBytes(salt); |
|
45 | var derivedKey = new Rfc2898DeriveBytes(key, salt); |
45 | var derivedKey = new Rfc2898DeriveBytes(key, salt); |
|
46 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize/8); |
46 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); |
|
Line 47... | Line 47... | |||
47 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize/8); |
47 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); |
|
48 | |
48 | |
|
49 | byte[] encryptedData; |
49 | byte[] encryptedData; |
|
50 | using (var encryptor = rijdanelManaged.CreateEncryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
50 | using (var encryptor = rijdanelManaged.CreateEncryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
|
Line 79... | Line 79... | |||
79 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
79 | /// <param name="separator">the separator to use between the cyphertext and the IV</param> |
|
80 | /// <returns>the decrypted data</returns> |
80 | /// <returns>the decrypted data</returns> |
|
81 | public string wasAESDecrypt(string data, string key, string separator = ":") |
81 | public string wasAESDecrypt(string data, string key, string separator = ":") |
|
82 | { |
82 | { |
|
83 | // retrieve the salt from the data. |
83 | // retrieve the salt from the data. |
|
84 | var segments = new List<string>(data.Split(new[] {separator}, StringSplitOptions.None)); |
84 | var segments = new List<string>(data.Split(new[] { separator }, StringSplitOptions.None)); |
|
85 | if (!segments.Count.Equals(2)) |
85 | if (!segments.Count.Equals(2)) |
|
86 | throw new ArgumentException("Invalid data."); |
86 | throw new ArgumentException("Invalid data."); |
|
Line 87... | Line 87... | |||
87 | |
87 | |
|
88 | string plaintext; |
88 | string plaintext; |
|
Line 93... | Line 93... | |||
93 | rijdanelManaged.Mode = AES_CIPHER_MODE; |
93 | rijdanelManaged.Mode = AES_CIPHER_MODE; |
|
94 | rijdanelManaged.Padding = AES_PADDING_MODE; |
94 | rijdanelManaged.Padding = AES_PADDING_MODE; |
|
Line 95... | Line 95... | |||
95 | |
95 | |
|
96 | // Retrieve the key and the IV from the salt. |
96 | // Retrieve the key and the IV from the salt. |
|
97 | var derivedKey = new Rfc2898DeriveBytes(key, Convert.FromBase64String(segments.First().Trim())); |
97 | var derivedKey = new Rfc2898DeriveBytes(key, Convert.FromBase64String(segments.First().Trim())); |
|
98 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize/8); |
98 | rijdanelManaged.Key = derivedKey.GetBytes(rijdanelManaged.KeySize / 8); |
|
Line 99... | Line 99... | |||
99 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize/8); |
99 | rijdanelManaged.IV = derivedKey.GetBytes(rijdanelManaged.BlockSize / 8); |
|
100 | |
100 | |
|
101 | using (var decryptor = rijdanelManaged.CreateDecryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
101 | using (var decryptor = rijdanelManaged.CreateDecryptor(rijdanelManaged.Key, rijdanelManaged.IV)) |
|
102 | { |
102 | { |
|
Line 113... | Line 113... | |||
113 | } |
113 | } |
|
114 | } |
114 | } |
|
115 | return plaintext; |
115 | return plaintext; |
|
116 | } |
116 | } |
|
117 | } |
117 | } |
|
118 | } |
- | ||
119 | |
118 | } |
|
- | 119 | |