private void encryptClicked() { string password = textBox1.Text; string confirmPassword = textBox2.Text; //If fields are empty if (password.Equals("") || confirmPassword.Equals("")) { MessageBox.Show("Dont't leave it blank!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } //If password is not equal to confirm password else if (password != confirmPassword) { MessageBox.Show("Passwords don't match", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { //Encrypt content of rich text box saveFileDialog.Filter = "Textual Encrypted|*.texx"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { //Task to encrypt and save file Task <string> task = new Task <string>(() => { try { string filename = saveFileDialog.FileName; //Add "ENCRYPTED" to the start of content to be encrypted rtb_content = "ENCRYPTED" + rtb_content; //Encrypt content and save string encrypted_content = Encryptor.Encrypt(rtb_content, password); File.WriteAllText(filename, ""); StreamWriter strw = new StreamWriter(filename); strw.Write(encrypted_content); strw.Close(); strw.Dispose(); return(filename); } catch (Exception ex) { throw ex; } }); //When task is over call changeSavedState method on parent form to remove * from tab title task.ContinueWith(t => { parent.Enabled = true; parent.changeSavedState(); parent.changeTitleLabel(t.Result); this.Dispose(); }, TaskScheduler.FromCurrentSynchronizationContext()); //Handle any exceptions that occured in the task task.ContinueWith(t => { task.Exception.Handle(ex => { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); }); parent.Enabled = true; this.Dispose(); }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext()); task.Start(); } } }
//Method called by DecryptForm class after user inputs a password public void decryptAndOpen(string password, string filename) { //Task used to read document and decrypt it Task <string> task = new Task <string>(() => { try { StreamReader streamReader = new StreamReader(filename); string rtb_content = streamReader.ReadToEnd(); streamReader.Close(); string decrypted = Encryptor.Decrypt(rtb_content, password); return(decrypted); }catch (Exception ex) { throw ex; } }); //After task ends task.ContinueWith(t => { string decrypted = t.Result; //If successfully decrypted if (decrypted.StartsWith("ENCRYPTED")) { //If no unedited untitled tab exists or if no tabs are open if (tabControl.SelectedTab == null || !filename_toolStripLabel.Text.Equals("untitled") || tabControl.SelectedTab.Text.Contains("*")) { makeNewTab(); } decrypted = decrypted.Substring(9); getRichTextBox().Rtf = decrypted; TabPage tabPage = tabControl.SelectedTab; tabPage.Tag = filename; //Simplified filename string fname = filename.Substring(filename.LastIndexOf("\\") + 1); tabPage.Text = fname; filename_toolStripLabel.Text = filename; } else //If not decrypted successfully { throw new CryptographicException(); } }, TaskScheduler.FromCurrentSynchronizationContext()); //Catch any errors and display (This occurs only when an error is thrown from the task) task.ContinueWith(t => { task.Exception.Handle(ex => { //CryptographicException is thrown if the password is incorrect if (ex is CryptographicException) { MessageBox.Show("Invalid Password!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(false); }); }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext()); task.Start(); }