-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataEncryptAndSave.cs
368 lines (368 loc) · 11.7 KB
/
DataEncryptAndSave.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace cryvis
{
namespace Encryption
{
/// <summary>
/// Used to Endcode to respective Bytes
/// </summary>
public class Encoder
{
/// Change this values according to your convinience
static readonly string PasswordHash = "R@s$W!%*";
static readonly string SaltKey = "I@$pPSLR";
//This should be 16bit long.
static readonly string VIKey = "BT@$%LK&^PP!RTY@";
/// <summar y>
/// Encodes Data to MD5Hash
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string EncodeToMD5(Data value)
{
StringBuilder builder = new StringBuilder();
var input = ASCIIEncoding.ASCII.GetBytes((string)value);
MD5 md = MD5.Create();
var Hash = md.ComputeHash(input);
foreach (byte b in Hash)
{
builder.Append(b.ToString("X2").ToLower());
}
return builder.ToString();
}
//Encryption Using RiJndael Method
public static string Encrypt(string Password)
{
byte[] PasswordBytes = Encoding.UTF8.GetBytes(Password);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
byte[] cipherTextBytes;
using (var memoryStream = new System.IO.MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(PasswordBytes, 0, PasswordBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
cryptoStream.Close();
}
memoryStream.Close();
}
return System.Convert.ToBase64String(cipherTextBytes);
}
public static string Decrypt(string encryptedText)
{
byte[] cipherTextBytes = System.Convert.FromBase64String(encryptedText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };
var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
var memoryStream = new System.IO.MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
}
}
}
namespace DataSaving
{
public class Saver
{
public static void SaveData(string FileName, string Password, Data data)
{
BinaryFormatter formater = new BinaryFormatter();
FileStream stream = File.Create(FileName);
data = Encryption.Encoder.EncodeToMD5(Password) + Encryption.Encoder.Encrypt((string)data);
formater.Serialize(stream, data);
stream.Close();
}
public static void LoadData(string FileName, string Password, out Data data)
{
data = null;
if (File.Exists(FileName))
{
BinaryFormatter formater = new BinaryFormatter();
FileStream stream = File.Open(FileName, FileMode.Open);
var output = (Data)formater.Deserialize(stream);
string Data = (string)output;
if (Data.Contains(Encryption.Encoder.EncodeToMD5(Password)))
{
var RequiredData = Data.Remove(0, Encryption.Encoder.EncodeToMD5(Password).Length);
data = Encryption.Encoder.Decrypt(RequiredData);
}
else
{
data = null;
throw new System.Exception("Wrong Password");
}
stream.Close();
}else
{
throw new System.Exception("File Not Found");
}
}
}
[System.Serializable]
public struct Data
{
public int IntData;
public float FloatData;
public string StringData;
public long LongData;
public decimal DecimalData;
public double DoubleData;
#region Constructors
public Data(int Data)
{
IntData = Data;
FloatData = (float)Data;
StringData = Data.ToString();
LongData = (long)Data;
DecimalData = (decimal)Data;
DoubleData = (double)Data;
}
public Data(float Data)
{
FloatData = Data;
IntData = (int)Data;
StringData = Data.ToString();
LongData = (long)Data;
DecimalData = (decimal)Data;
DoubleData = (double)Data;
}
public Data(string Data)
{
StringData = Data;
int.TryParse(Data, out IntData);
float.TryParse(Data, out FloatData);
long.TryParse (Data, out LongData);
decimal.TryParse (Data, out DecimalData);
double.TryParse (Data, out DoubleData);
}
public Data(long Data)
{
StringData = Data.ToString();
IntData = (int)Data;
FloatData = (float)Data;
LongData = (long)Data;
DecimalData = (decimal)Data;
DoubleData = (double)Data;
}
public Data(decimal Data)
{
StringData = Data.ToString();
IntData = (int)Data;
FloatData = (float)Data;
LongData = (long)Data;
DecimalData = (decimal)Data;
DoubleData = (double)Data;
}
public Data(double Data)
{
StringData = Data.ToString();
IntData = (int)Data;
FloatData = (float)Data;
LongData = (long)Data;
DecimalData = (decimal)Data;
DoubleData = (double)Data;
}
#endregion
#region Conversions
public static implicit operator Data(int Data)
{
return new Data(Data);
}
public static implicit operator Data(float Data)
{
return new Data(Data);
}
public static implicit operator Data(string Data)
{
return new Data(Data);
}
public static implicit operator Data(long Data)
{
return new Data(Data);
}
public static implicit operator Data(double Data)
{
return new Data(Data);
}
public static implicit operator Data(decimal Data)
{
return new Data(Data);
}
/// <summary>
/// returns IntegerDataStored
/// </summary>
/// <param name="D"></param>
public static explicit operator int(Data D)
{
return D.IntData;
}
/// <summary>
/// returns FloatDataStored
/// </summary>
/// <param name="D"></param>
public static explicit operator float(Data D)
{
return D.FloatData;
}
/// <summary>
/// returns StringDataStored
/// </summary>
/// <param name="D"></param>
public static explicit operator string(Data D)
{
return D.StringData;
}
public static explicit operator long(Data D)
{
return D.LongData;
}
public static explicit operator decimal(Data D)
{
return D.DecimalData;
}
public static explicit operator double(Data D)
{
return D.DoubleData;
}
#endregion
}
}
[System.Serializable]
public struct Data
{
public int IntData;
public float FloatData;
public string StringData;
public long LongData;
public decimal DecimalData;
public double DoubleData;
#region Constructors
public Data(int Data)
{
IntData = Data;
FloatData = (float)Data;
StringData = Data.ToString();
LongData = (long)Data;
DecimalData = (decimal)Data;
DoubleData = (double)Data;
}
public Data(float Data)
{
FloatData = Data;
IntData = (int)Data;
StringData = Data.ToString();
LongData = (long)Data;
DecimalData = (decimal)Data;
DoubleData = (double)Data;
}
public Data(string Data)
{
StringData = Data;
int.TryParse(Data, out IntData);
float.TryParse(Data, out FloatData);
long.TryParse (Data, out LongData);
decimal.TryParse (Data, out DecimalData);
double.TryParse (Data, out DoubleData);
}
public Data(long Data)
{
StringData = Data.ToString();
IntData = (int)Data;
FloatData = (float)Data;
LongData = (long)Data;
DecimalData = (decimal)Data;
DoubleData = (double)Data;
}
public Data(decimal Data)
{
StringData = Data.ToString();
IntData = (int)Data;
FloatData = (float)Data;
LongData = (long)Data;
DecimalData = (decimal)Data;
DoubleData = (double)Data;
}
public Data(double Data)
{
StringData = Data.ToString();
IntData = (int)Data;
FloatData = (float)Data;
LongData = (long)Data;
DecimalData = (decimal)Data;
DoubleData = (double)Data;
}
#endregion
#region Conversions
public static implicit operator Data(int Data)
{
return new Data(Data);
}
public static implicit operator Data(float Data)
{
return new Data(Data);
}
public static implicit operator Data(string Data)
{
return new Data(Data);
}
public static implicit operator Data(long Data)
{
return new Data(Data);
}
public static implicit operator Data(double Data)
{
return new Data(Data);
}
public static implicit operator Data(decimal Data)
{
return new Data(Data);
}
/// <summary>
/// returns IntegerDataStored
/// </summary>
/// <param name="D"></param>
public static explicit operator int(Data D)
{
return D.IntData;
}
/// <summary>
/// returns FloatDataStored
/// </summary>
/// <param name="D"></param>
public static explicit operator float(Data D)
{
return D.FloatData;
}
/// <summary>
/// returns StringDataStored
/// </summary>
/// <param name="D"></param>
public static explicit operator string(Data D)
{
return D.StringData;
}
public static explicit operator long(Data D)
{
return D.LongData;
}
public static explicit operator decimal(Data D)
{
return D.DecimalData;
}
public static explicit operator double(Data D)
{
return D.DoubleData;
}
#endregion
}
}