private void btnEncrypt_Click(object sender, EventArgs e) { bool isFolder = true; bool check = true; if (txtE_Password.Text == "" || txtE_Plaintext.Text == "") { MessageBox.Show("Please complete all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //|| txtE_CipherText.Text == "" FileAttributes attr = File.GetAttributes(txtE_Plaintext.Text); if ((attr & FileAttributes.Directory) == FileAttributes.Directory) { isFolder = true; this.Invoke((Action)(() => txtE_CipherText.Text = txtE_Plaintext.Text)); } else { isFolder = false; } //detect whether its a directory or file if (isFolder == true) { this.Invoke((Action)(() => txtE_Status.Text = " =========ENCRYPT A FOLDER========= \r\n")); //MessageBox.Show("Its a directory"); string[] filePaths = Directory.GetFiles(txtE_Plaintext.Text, "*.*", SearchOption.AllDirectories); foreach (var item in filePaths) { if (radioE_AES.Checked) { try { var watchfl = System.Diagnostics.Stopwatch.StartNew(); Aes.EncryptFile(item, item + ".enc", txtE_Password.Text); watchfl.Stop(); var elapsedMsfl = watchfl.ElapsedMilliseconds; this.Invoke((Action)(() => txtE_Status.Text += item + " => OK. Time: " + elapsedMsfl.ToString() + " ms \r\n")); } catch { check = false; this.Invoke((Action)(() => txtE_Status.Text += item + "=> Some thing went wrong \r\n")); } } else if (radioE_TripleDES.Checked) { try { var watchfl = System.Diagnostics.Stopwatch.StartNew(); TrippleDES.EncryptFile(item, item + ".enc", txtE_Password.Text); watchfl.Stop(); var elapsedMsfl = watchfl.ElapsedMilliseconds; this.Invoke((Action)(() => txtE_Status.Text += item + " => OK. Time: " + elapsedMsfl.ToString() + " ms \r\n")); } catch { check = false; this.Invoke((Action)(() => txtE_Status.Text += item + "=> Some thing went wrong \r\n")); } } // delete source files if (checkBoxDel.Checked && check) { //System.Security.Permissions.FileIOPermission fp = new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.Write, txtE_Plaintext.Text); new FileInfo(item + ".enc").CopyTo(item, true); //File.Copy(item + ".enc", item, true); FileInfo f = new FileInfo(item); f.Delete(); } } //Open Target if (checkBoxOpenTarg.Checked == true) { Process.Start("explorer.exe", txtD_Ciphertext.Text); } } else { this.Invoke((Action)(() => txtE_Status.Text = " =========ENCRYPT A FILE========= \n")); //Measure time executing var watch = System.Diagnostics.Stopwatch.StartNew(); string pathE = txtE_CipherText.Text + "\\" + Path.GetFileName(txtE_Plaintext.Text) + ".enc"; //path of ciphertext DirectoryInfo di = new DirectoryInfo(pathE); string folderpathE = di.Parent.FullName; if (radioE_RSA.Checked) { // RSA EncryptFile //string path_file = @txtE_Plaintext.Text; //string ext_file = Path.GetExtension(str); try { RSA.EncryptFile(txtE_Plaintext.Text, pathE, txtE_Password.Text); } catch { check = false; } } else if (radioE_AES.Checked) { try { //MessageBox.Show(pathE); Aes.EncryptFile(txtE_Plaintext.Text, pathE, txtE_Password.Text); } catch { check = false; txtE_Status.Text += "Some thing went wrong"; } } else //DES Algorithm { try { TrippleDES.EncryptFile(txtE_Plaintext.Text, pathE, txtE_Password.Text); } catch (Exception eee) { check = false; txtE_Status.Text += $"{eee}\n"; } } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; if (check) { txtE_Status.Text = "Successful!" + "\r\n"; } txtE_Status.Text += "Time executing: " + elapsedMs.ToString() + " ms"; //Open Target if (checkBoxOpenTarg.Checked == true) { Process.Start("explorer.exe", folderpathE); } // delete source files if (checkBoxDel.Checked && check) { File.Copy(pathE, txtE_Plaintext.Text, true); FileInfo f = new FileInfo(txtE_Plaintext.Text); f.Delete(); } } }
private void btnDecrypt_Click(object sender, EventArgs e) { bool isFolder = true; bool check = true; if (txtD_Ciphertext.Text == "" || txtD_Password.Text == "") { MessageBox.Show("Please complete all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } //|| txtE_CipherText.Text == "" FileAttributes attr = File.GetAttributes(txtD_Ciphertext.Text); if ((attr & FileAttributes.Directory) == FileAttributes.Directory) { isFolder = true; this.Invoke((Action)(() => txtD_Plaintext.Text = txtD_Ciphertext.Text)); } else { isFolder = false; } //detect whether its a directory or file if (isFolder == true) { this.Invoke((Action)(() => txtD_Status.Text = " =========DECRYPT A FOLDER========= \r\n")); //MessageBox.Show("Its a directory"); string[] filePaths = Directory.GetFiles(txtD_Ciphertext.Text, "*.enc", SearchOption.AllDirectories); foreach (var item in filePaths) { ////Measure time executing //var watch = System.Diagnostics.Stopwatch.StartNew(); string pathD = item.Substring(0, (item.Length - 4)); //.enc = 4 if (radioD_AES.Checked) { try { var watchfl = System.Diagnostics.Stopwatch.StartNew(); check = Aes.DecryptFile(item, pathD, txtD_Password.Text); watchfl.Stop(); //check = true; var elapsedMsfl = watchfl.ElapsedMilliseconds; if (check) { this.Invoke((Action)(() => txtD_Status.Text += item + " => OK. Time: " + elapsedMsfl.ToString() + " ms \r\n")); } else { txtD_Status.ForeColor = Color.Red; txtD_Status.Text += "Failed!" + "\r\n"; } } catch { check = false; this.Invoke((Action)(() => txtD_Status.Text += item + "=> Some thing went wrong \r\n")); } } else if (radioD_TripleDES.Checked) { try { var watchfl = System.Diagnostics.Stopwatch.StartNew(); check = TrippleDES.DecryptFile(item, pathD, txtD_Password.Text); watchfl.Stop(); //check = true; var elapsedMsfl = watchfl.ElapsedMilliseconds; if (check) { this.Invoke((Action)(() => txtD_Status.Text += item + " => OK. Time: " + elapsedMsfl.ToString() + " ms \r\n")); } else { txtD_Status.ForeColor = Color.Red; txtD_Status.Text += "Failed!" + "\r\n"; } } catch { check = false; this.Invoke((Action)(() => txtD_Status.Text += item + "=> Some thing went wrong \r\n")); } } // Del all source files in Folder and its subfolder if (checkBoxDelSrc.Checked && check) { FileInfo f = new FileInfo(txtD_Ciphertext.Text); f.Delete(); } } //Open Target if (checkBoxOpenTarg.Checked == true) { Process.Start("explorer.exe", txtD_Ciphertext.Text); } } else { string namecipher = Path.GetFileName(txtD_Ciphertext.Text); string pathD = txtD_Plaintext.Text + "\\" + namecipher.Substring(0, (namecipher.Length - 4)); //.enc = 4 //Measure time executing var watch = System.Diagnostics.Stopwatch.StartNew(); if (radioD_RSA.Checked) { // RSA DecryptFile try { check = RSA.DecryptFile(txtD_Ciphertext.Text, pathD, txtD_Password.Text); } catch { check = false; txtD_Status.Text += "Some thing went wrong\n"; } } else if (radioD_AES.Checked) { try { check = Aes.DecryptFile(txtD_Ciphertext.Text, pathD, txtD_Password.Text); } catch { check = false; txtD_Status.Text += "Some thing went wrong\n"; } } else //DES Algorithm { try { check = TrippleDES.DecryptFile(txtD_Ciphertext.Text, pathD, txtD_Password.Text); } catch (Exception eee) { Console.WriteLine($"{eee}\n"); txtE_Status.Text += $"{eee}\n"; } } watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; if (check) { txtD_Status.ForeColor = Color.GreenYellow; txtD_Status.Text = "Successful!" + "\r\n"; } else { txtD_Status.ForeColor = Color.Red; txtD_Status.Text = "Failed!" + "\r\n"; } txtD_Status.Text += "Time executing: " + elapsedMs.ToString() + " ms"; //Open Associated Application if (checkBoxOpenAssociatedA.Checked == true && check) { try { Process.Start(pathD); } catch { MessageBox.Show("File not found"); } } //checkBoxOpenTargF if (checkBoxOpenTargF.Checked == true && check) { Process.Start("explorer.exe", txtD_Plaintext.Text); } // delete source file if (checkBoxDelSrc.Checked && check) { FileInfo f = new FileInfo(txtD_Ciphertext.Text); f.Delete(); } } }