This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathManagedAES.cs
More file actions
147 lines (137 loc) · 4.85 KB
/
ManagedAES.cs
File metadata and controls
147 lines (137 loc) · 4.85 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.IO;
using System.Management;
using System.Security.Cryptography;
using System.Text;
namespace SPCode.Utils
{
public static class ManagedAES
{
private static byte[] Salt;
public static string Encrypt(string plainText)
{
if (plainText.Length < 1)
{
return string.Empty;
}
try
{
var symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
var encryptor = symmetricKey.CreateEncryptor(SaltKey(Program.OptionsObject.Program_CryptoKey), Encoding.ASCII.GetBytes("SPEdit.Utils.AES")); //so cool that this matches :D
byte[] cipherTextBytes;
using (var memoryStream = new MemoryStream())
{
using var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
var buffer = Encoding.UTF8.GetBytes(plainText);
cryptoStream.Write(buffer, 0, buffer.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
}
return Convert.ToBase64String(cipherTextBytes);
}
catch (Exception)
{
// ignored
}
return string.Empty;
}
public static string Decrypt(string encryptedText)
{
if (encryptedText.Length < 1)
{
return string.Empty;
}
try
{
var cipherTextBytes = Convert.FromBase64String(encryptedText);
var symmetricKey = new RijndaelManaged { Mode = CipherMode.CBC, Padding = PaddingMode.None };
var decryptor = symmetricKey.CreateDecryptor(SaltKey(Program.OptionsObject.Program_CryptoKey), Encoding.ASCII.GetBytes("SPEdit.Utils.AES"));
string outString;
using (var memoryStream = new MemoryStream(cipherTextBytes))
{
using var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
var plainTextBytes = new byte[cipherTextBytes.Length];
var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
outString = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd('\0');
}
return outString;
}
catch (Exception)
{
// ignored
}
return string.Empty;
}
private static byte[] SaltKey(byte[] key)
{
if (Salt == null)
{
CreateSalt();
}
if (!Program.OptionsObject.Program_UseHardwareSalts)
{
return key;
}
var buffer = new byte[16];
for (var i = 0; i < 16; ++i)
{
if (Salt != null && i < Salt.Length)
{
buffer[i] = (byte)(key[i] ^ (uint)Salt[i]);
}
else
{
buffer[i] = key[i];
}
}
return buffer;
}
private static void CreateSalt()
{
byte[] buffer;
using (MD5 md5Provider = new MD5CryptoServiceProvider())
{
var inString = $"SPEditSalt {CpuId()}{DiskId()}{Environment.ProcessorCount}{(Environment.Is64BitOperatingSystem ? "T" : "F")}";
var encoder = new UTF8Encoding();
buffer = md5Provider.ComputeHash(encoder.GetBytes(inString));
}
Salt = buffer;
}
//thanks to: http://jai-on-asp.blogspot.de/2010/03/finding-hardware-id-of-computer.html
private static string CpuId()
{
var id = string.Empty;
try
{
var mbs = new ManagementObjectSearcher("Select ProcessorId From Win32_processor");
var mbsList = mbs.Get();
foreach (var o in mbsList)
{
var mo = (ManagementObject)o;
id = mo["ProcessorId"].ToString();
break;
}
}
catch (Exception)
{
// ignored
}
return id;
}
private static string DiskId()
{
var id = string.Empty;
try
{
var dsk = new ManagementObject(@"win32_logicaldisk.deviceid=""c:""");
dsk.Get();
id = dsk["VolumeSerialNumber"].ToString();
}
catch (Exception)
{
// ignored
}
return id;
}
}
}