-
Notifications
You must be signed in to change notification settings - Fork 9
/
AES.cs
91 lines (84 loc) · 3.72 KB
/
AES.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* FOG Service : A computer management client for the FOG Project
* Copyright (C) 2014-2023 FOG Project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Zazzles;
using Zazzles.Data;
namespace FOG.Handlers.Data
{
public static class AES
{
private const string LogName = "Data::AES";
/// <summary>
/// AES decrypts a string
/// </summary>
/// <param name="toDecode">The string to be decrypted</param>
/// <param name="key">The AES pass key</param>
/// <param name="iv">The AES initialization vector</param>
/// <returns>A decrypted version of the data</returns>
public static string Decrypt(byte[] toDecode, byte[] key, byte[] iv)
{
try
{
using (var rijndaelManaged = new RijndaelManaged { Key = key, IV = iv, Mode = CipherMode.CBC, Padding = PaddingMode.Zeros })
using (var memoryStream = new MemoryStream(toDecode))
using (var cryptoStream = new CryptoStream(memoryStream, rijndaelManaged.CreateDecryptor(), CryptoStreamMode.Read))
{
//Return the stream, but trim null bytes due to reading too far
return new StreamReader(cryptoStream).ReadToEnd().Replace("\0", string.Empty).Trim();
}
}
catch (Exception ex)
{
Log.Error(LogName, "Could not decrypt AES");
Log.Error(LogName, ex);
}
return "";
}
/// <summary>
/// AES decrypts a string
/// <param name="toDecode">The hex-code string to be decrypted</param>
/// <param name="passKey">The AES pass key</param>
/// <param name="initializationVector">The AES initialization vector</param>
/// <returns>An decrypted string of toDecode</returns>
/// </summary>
public static string Decrypt(string toDecode, string passKey, string initializationVector)
{
//Convert the initialization vector and key into a byte array
var key = Encoding.UTF8.GetBytes(passKey);
var iv = Encoding.UTF8.GetBytes(initializationVector);
var msg = Transform.HexStringToByteArray(toDecode);
return Decrypt(msg, key, iv);
}
/// <summary>
/// Decrypts a string using AES, and automatically extracts the initialization vector
/// <param name="toDecode">The string to be decrypted</param>
/// <param name="key">The AES pass key to use</param>
/// <returns>A decrypted version of toDecode</returns>
/// </summary>
public static string Decrypt(string toDecode, byte[] key)
{
var iv = Transform.HexStringToByteArray(toDecode.Substring(0, toDecode.IndexOf("|")));
var data = Transform.HexStringToByteArray(toDecode.Substring(toDecode.IndexOf("|") + 1));
return Decrypt(data, key, iv);
}
}
}