-
Notifications
You must be signed in to change notification settings - Fork 0
/
Add_File.cs
481 lines (368 loc) · 16 KB
/
Add_File.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
using System;
using System.Security;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using MongoDB.Driver;
using MongoDB.Bson;
namespace Project_v1
{
public partial class Add_File : Form
{
public Add_File()
{
InitializeComponent();
}
string filepath, pathFolder, extension;
int sNumber;
List<string> dirList = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
filepath = dlg.FileName.ToString();
pathFolder = System.IO.Path.GetDirectoryName(dlg.FileName);
extension = Path.GetExtension(filepath);
FileInfo f = new FileInfo(filepath);
f.MoveTo(Path.ChangeExtension(filepath, ".txt"));
textBox1.Text = filepath;
filepath = Path.ChangeExtension(filepath, "txt");
}
string msg = null;
try
{
using (StreamReader streamReader = new StreamReader(filepath, Encoding.UTF8))
{
msg = streamReader.ReadToEnd();
}
textBox3.Text = msg;
}
catch (Exception ex)
{
msg = null;
//If no file in selected than it will not give error message
}
}
//AES algorithm encryption function
public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
AES.Padding = PaddingMode.PKCS7;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 5000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
//File encryption performed here
public void EncryptFile()
{
string file = filepath;
string password = textBox2.Text;
byte[] bytesToBeEncrypted = File.ReadAllBytes(file);
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
// Hash the password with SHA256
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesEncrypted = AES_Encrypt(bytesToBeEncrypted, passwordBytes);
string fileEncrypted = filepath;
File.WriteAllBytes(fileEncrypted, bytesEncrypted);
textBox3.Text = null;
byte[] encryptedText = File.ReadAllBytes(file);
string ms = Convert.ToBase64String(encryptedText);
textBox3.Text = ms;
File.WriteAllText(fileEncrypted, ms);
}
//Decryption method
public byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
public void DecryptFile()
{
string encoded = File.ReadAllText(filepath);
var utfEncoded = Convert.FromBase64String(encoded);
File.WriteAllBytes(filepath, utfEncoded);
string fileEncrypted = filepath;
string password = textBox2.Text;
byte[] bytesToBeDecrypted = File.ReadAllBytes(fileEncrypted);
byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
byte[] bytesDecrypted = AES_Decrypt(bytesToBeDecrypted, passwordBytes);
string file = filepath;
File.WriteAllBytes(file, bytesDecrypted);
textBox3.Text = null;
byte[] decryptedText = File.ReadAllBytes(file);
string msg = null;
textBox3.Text = null;
try
{
using (StreamReader streamReader = new StreamReader(filepath, Encoding.UTF8))
{
msg = streamReader.ReadToEnd();
}
textBox3.Text = msg;
}
catch (Exception ex)
{
msg = null;
MessageBox.Show(ex.ToString());
}
}
private void timer1_Tick(object sender, EventArgs e)//Unused
{
}
private void button2_Click(object sender, EventArgs e)
{
var watch = System.Diagnostics.Stopwatch.StartNew();//Counting the encryption time
EncryptFile();
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
label3.Text = elapsedMs.ToString();
}
private void label1_Click(object sender, EventArgs e)//unused
{
}
private void button4_Click(object sender, EventArgs e)
{
var watch = System.Diagnostics.Stopwatch.StartNew();//Counting decryption time
DecryptFile();
watch.Stop();
var elapsedMss = watch.ElapsedMilliseconds;
label3.Text = elapsedMss.ToString();
}
//Upload Button functionality
string baseFileName, chunkName;
private void button3_Click(object sender, EventArgs e)
{
var watchV = System.Diagnostics.Stopwatch.StartNew();
List<string> serverList = new List<string>();
serverList.Add("mongodb://localhost:27017");
serverList.Add("mongodb://eliaszamanm:[email protected]:31511/mymongodb");
serverList.Add("mongodb://eliaszamanm:[email protected]:19395/mymongodb");
serverList.Add("mongodb://eliaszamanm:[email protected]:51355/mymongodb");
serverList.Add("mongodb://eliaszamanm:[email protected]:55455/mymongodb");
serverList.Add("mongodb://eliaszamanm:[email protected]:59845/mymongodb");
int count = 0;
var fileCollection = serverList[0];
int CN = 0;
foreach (string server in serverList)//Run the loop for each server to insert data in a sequence
{
CN++;
if (CN > sNumber)
break;
else
{
MongoClient client = new MongoClient(server);
var DB1 = client.GetDatabase("mymongodb");
var collection = DB1.GetCollection<BOStore>("store");
int x1 = count;
int x2 = sNumber;
if (dirList[count] == null)
break;
else
{
string Address = dirList[count]; //This value is for File Tracking server
string partAddress = Address;
chunkName = Path.GetFileName(partAddress);
if (count < sNumber)
{
count = count + 1;
}
else
break;
string fileID = chunkName;
MongoClient fileClient = new MongoClient(serverList[0]);
var fDB = fileClient.GetDatabase("FileTracker");
var fileC = fDB.GetCollection<FileTrack>("FileDetails");
var FileInfo = new FileTrack
{
UserID = File.ReadAllText(@"C:\Users\eliaszaman\Desktop\test\install\user.x"),
FileID = fileID,
ServerID = server,
FileKey = File.ReadAllText(@"C:\Users\eliaszaman\Desktop\test\key\encr.txt")
};
string fileData = File.ReadAllText(partAddress, Encoding.UTF8);
textBox3.Text = null;
textBox3.Text = fileData;
var document = new BOStore
{
UserID = File.ReadAllText(@"C:\Users\eliaszaman\Desktop\test\install\user.x"),
BaseFileName = baseFileName,
FileID = fileID,
FileData = fileData,
FileKey = File.ReadAllText(@"C:\Users\eliaszaman\Desktop\test\key\encr.txt"),
FileExtension = extension
};
collection.InsertManyAsync(new[] { document });
fileC.InsertManyAsync(new[] { FileInfo });
File.Delete(partAddress);//Delete the Chunk after upload finished
}
}
}
watchV.Stop();
var elapsedUpload = watchV.ElapsedMilliseconds;
label5.Text = elapsedUpload.ToString();
MessageBox.Show("Successfully uploaded to cloud");
listBox1.Items.Clear();//Clear the list box that contains the chunk list
}
//Split method starts here
private void button5_Click(object sender, EventArgs e)
{
string inputFile = filepath; // Substitute this with your Input File
FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
int numberOfFiles = Convert.ToInt32(sNumber);
int sizeOfEachFile = (int)Math.Ceiling((double)fs.Length / numberOfFiles);
for (int i = 1; i <= numberOfFiles; i++)
{
baseFileName = Path.GetFileNameWithoutExtension(inputFile);
string extension = Path.GetExtension(inputFile);
FileStream outputFile = new FileStream(Path.GetDirectoryName(inputFile) + "\\" + baseFileName + "." + i.ToString().PadLeft(5, Convert.ToChar("0")) + extension + "." + "txt", FileMode.Create, FileAccess.Write);
int bytesRead = 0;
byte[] buffer = new byte[sizeOfEachFile];
if ((bytesRead = fs.Read(buffer, 0, sizeOfEachFile)) > 0)
{
outputFile.Write(buffer, 0, bytesRead);
}
outputFile.Close();
}
fs.Close();
System.IO.File.Delete(filepath);
listBox1.Items.Clear();
dirList.Clear();
string[] files = Directory.GetFiles(pathFolder);
string[] dirs = Directory.GetDirectories(pathFolder);
foreach (string file in files)
{
string dir = Path.GetFullPath(file);
dirList.Add(dir);
string f = Path.GetFileNameWithoutExtension(file);
listBox1.Items.Add(Path.GetFileNameWithoutExtension(f));
chunkName = Path.GetFileName(file);
}
}
//Merge method starts here
int lCounter = 1;
public void button6_Click(object sender, EventArgs e)
{
string outPath = "C://Users//eliaszaman//Desktop//test//"; // Substitute this with your Input Folder
string[] tmpFiles = Directory.GetFiles(outPath, "*txt");
FileStream outputFile = null;
string prevFileName = "";
foreach (string tempFile in tmpFiles)
{
string fileName = Path.GetFileNameWithoutExtension(tempFile);
baseFileName = fileName.Substring(0, fileName.IndexOf(Convert.ToChar(".")));
string extension = Path.GetExtension(fileName);
if (!prevFileName.Equals(baseFileName))
{
if (outputFile != null)
{
outputFile.Flush();
outputFile.Close();
}
outputFile = new FileStream(outPath + baseFileName + extension, FileMode.OpenOrCreate, FileAccess.Write);
}
int bytesRead = 0;
//From here each file part is accessed
byte[] buffer = new byte[1024];
FileStream inputTempFile = new FileStream(tempFile, FileMode.OpenOrCreate, FileAccess.Read);
while ((bytesRead = inputTempFile.Read(buffer, 0, 1024)) > 0)
outputFile.Write(buffer, 0, bytesRead);
inputTempFile.Close();
File.Delete(tempFile);
prevFileName = baseFileName;
}
lCounter++;
outputFile.Close();
listBox1.Items.Clear();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
this.Hide();
Home home = new Home();
home.Show();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
string KeyPath;
string PubKey;
private void button8_Click(object sender, EventArgs e)
{
OpenFileDialog dlg2 = new OpenFileDialog();
if (dlg2.ShowDialog() == DialogResult.OK)
{
KeyPath = dlg2.FileName.ToString();
PubKey = File.ReadAllText(KeyPath);
}
}
private void button9_Click(object sender, EventArgs e)
{
string enPath = @"C:\Users\eliaszaman\Desktop\test\key\encr.txt";
string encrypted = AsymmetricEncryption.EncryptText(textBox2.Text, 1024, PubKey);
MessageBox.Show("Encrypted");
File.WriteAllText(enPath, encrypted);
}
private void label2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//string sPath = @"C:\Users\eliaszaman\Desktop\test\install\sN.x";
string selected = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);
sNumber = Convert.ToInt32(selected);
}
}
}