private void BtnDecrypt_Click(object sender, EventArgs e) { TypesOfCiphers typesOfCipher = TypesOfCiphers.RailFence; if (rbRailFence.Checked) { typesOfCipher = TypesOfCiphers.RailFence; } else if (rbRotatingSquare.Checked) { typesOfCipher = TypesOfCiphers.RotatingSquare; } else if (rbVigenerCipher.Checked) { typesOfCipher = TypesOfCiphers.Vigenere; } tbKey.Text = KeyValidation.ModifyKey(tbKey.Text, typesOfCipher).ToString(); rtbSrcText.Text = TextValidation.ModifyText(rtbSrcText.Text, typesOfCipher); if (cbUseDataInRcb.Checked) { rtbResText.Text = MainController.Decrypt(rtbSrcText.Text, tbKey.Text, typesOfCipher); } else { try { MainController.Decrypt(tbPathToSrcFile.Text, tbPathToResFile.Text, tbKey.Text, typesOfCipher); } catch (ArgumentException) { MessageBox.Show("Incorrect file path entered!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (IOException) { MessageBox.Show("Incorrect file path entered!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// Encrypts data from a file using the selected algorithm and writes the result to another file. /// </summary> /// <param name="pathToSrcFile">The path to the file with the text to be encrypted.</param> /// <param name="pathToDestFile">The path to the file where you want to save the encryption result.</param> /// <param name="key">The key for the encryption algorithm.</param> /// <param name="typeOfChiper">The name of the encryption algorithm.</param> public static void Encrypt(string pathToSrcFile, string pathToDestFile, string key, TypesOfCiphers typeOfChiper) { string plaintext; try { plaintext = File.ReadAllText(pathToSrcFile); } catch (ArgumentException) { throw new ArgumentException(); } catch (IOException) { throw new IOException(); } plaintext = TextValidation.ModifyText(plaintext, typeOfChiper); string ciphertext = PerformAction(plaintext, key, typeOfChiper, Model.Ciphers.Action.Encrypt); File.WriteAllText(pathToDestFile, ciphertext); }