-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cs
288 lines (242 loc) · 9.86 KB
/
main.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
public class Cryptography
{
public class Asymmetric
{
public class RSA
{
/// <summary>
/// Create a public and private key.
///
/// The RSACryptoServiceProvider supports key sizes from 384
/// bits to 16384 bits in increments of 8 bits if you have the
/// Microsoft Enhanced Cryptographic Provider installed. It
/// supports key sizes from 384 bits to 512 bits in increments
/// of 8 bits if you have the Microsoft Base Cryptographic
/// Provider installed.
/// </summary>
/// <param name="publicKey">The created public key.</param>
/// <param name="privateKey">The created private key.</param>
/// <param name="keySize">Size of keys.</param>
public static void CreateKeys(out string publicKey, out string privateKey, int keySize = 4096)
{
publicKey = null;
privateKey = null;
var csp = new CspParameters
{
ProviderType = 1,
Flags = CspProviderFlags.UseArchivableKey,
KeyNumber = (int) KeyNumber.Exchange
};
using var rsa = new RSACryptoServiceProvider(keySize, csp);
publicKey = rsa.ToXmlString(false);
privateKey = rsa.ToXmlString(true);
rsa.PersistKeyInCsp = false;
}
/// <summary>
/// Encrypt data using a public key.
/// </summary>
/// <param name="bytes">Bytes to encrypt.</param>
/// <param name="publicKey">Public key to use.</param>
/// <returns>Encrypted data.</returns>
public static byte[] Encrypt(byte[] bytes, string publicKey)
{
var csp = new CspParameters
{
ProviderType = 1
};
using var rsa = new RSACryptoServiceProvider(csp);
rsa.FromXmlString(publicKey);
var data = rsa.Encrypt(bytes, false);
rsa.PersistKeyInCsp = false;
return data;
}
/// <summary>
/// Encrypt data using a public key.
/// </summary>
/// <param name="input">Data to encrypt.</param>
/// <param name="publicKey">Public key to use.</param>
/// <returns>Encrypted data.</returns>
public static string Encrypt(string input, string publicKey)
{
if (input == null)
{
throw new Exception("Input cannot be null");
}
return Convert.ToBase64String(
Encrypt(
Encoding.UTF8.GetBytes(input),
publicKey));
}
/// <summary>
/// Decrypt data using a private key.
/// </summary>
/// <param name="bytes">Bytes to decrypt.</param>
/// <param name="privateKey">Private key to use.</param>
/// <returns>Decrypted data.</returns>
public static byte[] Decrypt(byte[] bytes, string privateKey)
{
var csp = new CspParameters
{
ProviderType = 1
};
using var rsa = new RSACryptoServiceProvider(csp);
rsa.FromXmlString(privateKey);
var data = rsa.Decrypt(bytes, false);
rsa.PersistKeyInCsp = false;
return data;
}
/// <summary>
/// Decrypt data using a private key.
/// </summary>
/// <param name="input">Base64 data to decrypt.</param>
/// <param name="privateKey">Private key to use.</param>
/// <returns>Decrypted data.</returns>
public static string Decrypt(string input, string privateKey)
{
if (input == null)
{
throw new Exception("Input cannot be null");
}
return Encoding.UTF8.GetString(
Decrypt(
Convert.FromBase64String(input),
privateKey));
}
}
}
public class Symmetric
{
/// <summary>
/// Number of iterations for block chain.
/// </summary>
public static int Iterations = 2;
/// <summary>
/// Key size for encrypting/decrypting.
/// The valid key sizes for Rijndael are 128, 192 and 256 bits.
/// </summary>
public static int KeySize = 256;
/// <summary>
/// Salt for password hashing.
/// </summary>
public static byte[] Salt = {
0x26, 0xdc, 0xff, 0x00,
0xad, 0xed, 0x7a, 0xee,
0xc5, 0xfe, 0x07, 0xaf,
0x4d, 0x08, 0x22, 0x3c
};
/// <summary>
/// Encrypt data using a passphrase and a given algorithm.
/// </summary>
/// <typeparam name="T">Algorithm to use.</typeparam>
/// <param name="bytes">Bytes to encrypt.</param>
/// <param name="passphrase">Passphrase to use.</param>
/// <returns>Encrypted data.</returns>
public static byte[] Encrypt<T>(byte[] bytes, string passphrase) where T : SymmetricAlgorithm, new()
{
using var cipher = new T();
var pwdBytes = new Rfc2898DeriveBytes(
Encoding.UTF7.GetBytes(passphrase),
Salt,
Iterations);
var keyBytes = pwdBytes.GetBytes(KeySize / 8);
cipher.Mode = CipherMode.CBC;
using var encryptor = cipher.CreateEncryptor(keyBytes, pwdBytes.GetBytes(16));
using var stream = new MemoryStream();
using var writer = new CryptoStream(stream, encryptor, CryptoStreamMode.Write);
writer.Write(bytes, 0, bytes.Length);
writer.FlushFinalBlock();
var data = stream.ToArray();
cipher.Clear();
return data;
}
/// <summary>
/// Encrypt data using a passphrase and the Rijndael/AES algorithm.
/// </summary>
/// <param name="bytes">Bytes to encrypt.</param>
/// <param name="passphrase">Passphrase to use.</param>
/// <returns>Encrypted data.</returns>
public static byte[] Encrypt(byte[] bytes, string passphrase)
{
return Encrypt<RijndaelManaged>(
bytes,
passphrase);
}
/// <summary>
/// Encrypt data using a passphrase and the Rijndael/AES algorithm.
/// </summary>
/// <param name="input">Data to encrypt.</param>
/// <param name="passphrase">Passphrase to use.</param>
/// <returns>Encrypted data.</returns>
public static string Encrypt(string input, string passphrase)
{
return Convert.ToBase64String(
Encrypt(
Encoding.UTF8.GetBytes(input),
passphrase));
}
/// <summary>
/// Decrypt data using a passphrase and a given algorithm.
/// </summary>
/// <typeparam name="T">Algorithm to use.</typeparam>
/// <param name="bytes">Bytes to decrypt.</param>
/// <param name="passphrase">Passphrase to use.</param>
/// <returns>Decrypted data.</returns>
public static byte[] Decrypt<T>(byte[] bytes, string passphrase) where T : SymmetricAlgorithm, new()
{
using var cipher = new T();
var pwdBytes = new Rfc2898DeriveBytes(
Encoding.UTF7.GetBytes(passphrase),
Salt,
Iterations);
var keyBytes = pwdBytes.GetBytes(KeySize / 8);
cipher.Mode = CipherMode.CBC;
using var decryptor = cipher.CreateDecryptor(keyBytes, pwdBytes.GetBytes(16));
using var stream = new MemoryStream(bytes);
using var reader = new CryptoStream(stream, decryptor, CryptoStreamMode.Read);
var data = new byte[stream.Length];
reader.Read(data, 0, data.Length);
cipher.Clear();
while (true)
{
if (data.Last() != 0x00)
{
break;
}
data = data
.Take(data.Length - 1)
.ToArray();
}
return data;
}
/// <summary>
/// Decrypt data using a passphrase and the Rijndael/AES algorithm.
/// </summary>
/// <param name="bytes">Bytes to decrypt.</param>
/// <param name="passphrase">Passphrase to use.</param>
/// <returns>Decrypted data.</returns>
public static byte[] Decrypt(byte[] bytes, string passphrase)
{
return Decrypt<RijndaelManaged>(
bytes,
passphrase);
}
/// <summary>
/// Decrypt data using a passphrase and the Rijndael/AES algorithm.
/// </summary>
/// <param name="input">Data to decrypt.</param>
/// <param name="passphrase">Passphrase to use.</param>
/// <returns>Decrypted data.</returns>
public static string Decrypt(string input, string passphrase)
{
return Encoding.UTF8.GetString(
Decrypt(
Convert.FromBase64String(input),
passphrase));
}
}
}