Skip to content

Commit f410b4f

Browse files
committed
Completed and Tested Mass File encryption/decryption
1 parent 3de0e42 commit f410b4f

File tree

1 file changed

+117
-7
lines changed

1 file changed

+117
-7
lines changed

FileSecure-v3/Program.cs

+117-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace FileSecure_v3
1616
class Program
1717
{
1818
//static int CycleSize = 1024 * 1024 * 100; // Increasing this value can reduce the amount of cycle that is required to encrypt an file but also require higher memory consumption
19-
static async void Encrypt(string Password,string OpenPath, string SavePath)
19+
static void Encrypt(string Password,string OpenPath, string SavePath)
2020
{
2121
// Generate a random Nonce
2222
byte[] nonce = new byte[128 / 8];
@@ -39,22 +39,27 @@ static async void Encrypt(string Password,string OpenPath, string SavePath)
3939
{
4040
cryptstream.CopyTo(encryptfile);
4141
Console.WriteLine("The File (" + OpenPath + ") has been successfully encrypted");
42-
} catch(Exception ex)
42+
} catch (UnauthorizedAccessException)
43+
{
44+
Console.WriteLine("Permission Denied while accessing the file (Save/Open location for: "+OpenPath+"). Please try again by running the application as administrator. Press enter to close the application.");
45+
Console.ReadLine();
46+
return;
47+
} catch (Exception ex)
4348
{
4449
Console.WriteLine("The File ("+ OpenPath+") has encounter an unknown error while encrypting. Error: "+ex.Message);
4550
}
4651
}
4752
}
4853
}
49-
static async public void Decrypt(string Password, string OpenPath, string SavePath)
54+
static public void Decrypt(string Password, string OpenPath, string SavePath)
5055
{
5156
using (FileStream encryptfile = File.OpenRead(OpenPath))
5257
{
5358
// Get the IV/Nonce from the file
5459
byte[] nonce = new byte[16];
5560
encryptfile.Read(nonce, 0, nonce.Length);
5661
// Create the password
57-
byte[] PasswordToKey = new PasswordDeriveBytes(Encoding.UTF8.GetBytes(Password), null).GetBytes(256 / 8);
62+
byte[] PasswordToKey = new PasswordDeriveBytes(Encoding.UTF8.GetBytes(Password), nonce).GetBytes(256 / 8);
5863
using (FileStream plainfile = new FileStream(SavePath, FileMode.OpenOrCreate))
5964
{
6065
// Setup the Crypto Engine and start the encryption process
@@ -65,10 +70,17 @@ static async public void Decrypt(string Password, string OpenPath, string SavePa
6570
{
6671
cryptstream.CopyTo(plainfile);
6772
Console.WriteLine("The File ("+OpenPath+") has been successfully decrypted.");
68-
} catch(InvalidCipherTextException)
73+
} catch (UnauthorizedAccessException)
74+
{
75+
Console.WriteLine("Permission Denied while accessing the file (Save/Open location for: " + OpenPath + "). Please try again by running the application as administrator. Press enter to close the application.");
76+
Console.ReadLine();
77+
return;
78+
} catch (InvalidCipherTextException)
6979
{
70-
Console.Clear();
7180
Console.WriteLine("The file ("+ OpenPath +") cannot be decrypted because you supplied an incorrect key or the file has been tampered.");
81+
} catch(Exception ex)
82+
{
83+
Console.WriteLine("The File (" + OpenPath + ") has encounter an unknown error while decrypting. Error: " + ex.Message);
7284
}
7385

7486
}
@@ -156,7 +168,105 @@ static void Main(string[] args)
156168
Console.Clear();
157169
if (AllFileInFolder == true)
158170
{
159-
171+
// Multi-File Encryption
172+
// Let the user select the folder where they want all the files to be encrypted, subfolder will be excluded though.
173+
Console.WriteLine("Now type or paste the folder that you want all the files to be encrypted/decrypted: ");
174+
string OpenPath = Console.ReadLine();
175+
while(true)
176+
{
177+
if (string.IsNullOrEmpty(OpenPath))
178+
{
179+
// Empty stuff not gonna work
180+
Console.Clear();
181+
Console.WriteLine("An empty or whitespace only path name is not allowed, please type/paste your path again: ");
182+
OpenPath = Console.ReadLine();
183+
}
184+
else if (!Directory.Exists(OpenPath))
185+
{
186+
// Directory Not Found
187+
Console.Clear();
188+
Console.WriteLine("The directory was not found, please try again: ");
189+
OpenPath = Console.ReadLine();
190+
}
191+
else if (Directory.GetFiles(OpenPath).Length <= 0)
192+
{
193+
// There is no files in the directory
194+
Console.Clear();
195+
Console.WriteLine("There isn't files in the directory, try a different directory instead: ");
196+
OpenPath = Console.ReadLine();
197+
}
198+
else
199+
break;
200+
}
201+
// Now let them choose the location to store the file.
202+
Console.Clear();
203+
Console.WriteLine("Now type or paste the folder that you want the file to be saved in: ");
204+
string SavePath = Console.ReadLine();
205+
while (true)
206+
{
207+
if (string.IsNullOrEmpty(SavePath))
208+
{
209+
// Empty stuff not gonna work
210+
Console.Clear();
211+
Console.WriteLine("An empty or whitespace only path name is not allowed, please type/paste your path again: ");
212+
SavePath = Console.ReadLine();
213+
}
214+
else if (!Directory.Exists(SavePath))
215+
{
216+
// Directory Not Found
217+
Console.Clear();
218+
Console.WriteLine("The directory was not found, please try again: ");
219+
SavePath = Console.ReadLine();
220+
}
221+
else if (Directory.GetFiles(SavePath).Length > 0)
222+
{
223+
// There is file detected in the directory, warn user
224+
Console.Clear();
225+
Console.WriteLine("WARNING: There is already file in the directory that you try to save the file in. The files will be overwritten if there is a same name. Press Enter to confirm that you want to proceed, otherwise terminate the application.");
226+
Console.ReadLine();
227+
break;
228+
}
229+
else
230+
break;
231+
}
232+
Console.Clear();
233+
if(IsEncryption == true)
234+
{
235+
// Encrypting mass file
236+
string[] Files = Directory.GetFiles(OpenPath);
237+
int TotalProcessedFile = 0;
238+
Console.WriteLine("Mass File Encryption Started...");
239+
foreach(string file in Files)
240+
{
241+
Task.Run(() => { Encrypt(Password, file, SavePath + "/" + Path.GetFileName(file)); TotalProcessedFile = TotalProcessedFile + 1; if (TotalProcessedFile >= Files.Length) { Console.WriteLine("Mass File Encryption Process completed, press enter to exit."); }});
242+
}
243+
while(true)
244+
{
245+
Console.ReadLine();
246+
if(TotalProcessedFile >= Files.Length)
247+
{
248+
break;
249+
}
250+
}
251+
} else if(IsEncryption == false)
252+
{
253+
// Decrypting mass file
254+
string[] Files = Directory.GetFiles(OpenPath);
255+
int TotalProcessedFile = 0;
256+
Console.WriteLine("Mass File Decryption Started...");
257+
foreach (string file in Files)
258+
{
259+
Task.Run(() => { Decrypt(Password, file, SavePath+"/"+Path.GetFileName(file)); TotalProcessedFile = TotalProcessedFile + 1; if (TotalProcessedFile >= Files.Length) { Console.WriteLine("Mass File Decryption Process completed, press enter to exit."); } });
260+
}
261+
while (true)
262+
{
263+
Console.ReadLine();
264+
if (TotalProcessedFile >= Files.Length)
265+
{
266+
break;
267+
}
268+
}
269+
}
160270
} else if(AllFileInFolder == false)
161271
{
162272
// Single File Encryption Method

0 commit comments

Comments
 (0)