private void CreateNewEncryptDataOnUserPathButton_Click(object sender, EventArgs e) { SaveFileDialog.Title = "Save New Key and IV"; if (SaveFileDialog.ShowDialog() == DialogResult.OK) { EncryptedTextBox.Clear(); if (AES.Checked) { Symmetry = new AesEncoder(); } else { Symmetry = new RijndaelEncoder(); } string filePath = EncodersIOManager.WriteKeyAndIVInFile(Symmetry, SaveFileDialog.FileName); MessageBox.Show($"Your Key and IV were saved successfully at {filePath}", "Data Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); } SaveFileDialog.Dispose(); }
/// <summary> /// Reads IV and Kеy from the specified file /// </summary> /// <param name="symmetry"></param> /// <param name="filePath"></param> /// <returns>ISymmetry instance with new IV and Key</returns> public static ISymmetry ReadKeyAndIVFromFile(ISymmetry symmetry, string filePath) { using (var reader = new BinaryReader(File.OpenRead(filePath), System.Text.Encoding.UTF8)) { int length = reader.ReadInt32(); symmetry.Key = reader.ReadBytes(length); length = reader.ReadInt32(); symmetry.IV = reader.ReadBytes(length); } return(symmetry); }
/// <summary> /// Writes data for decryption to the specified path /// </summary> /// <param name="symmetry"></param> /// <param name="pathFolder"></param> /// <returns>Path to a new file containing data to decrypt</returns> public static string WriteKeyAndIVInFile(ISymmetry symmetry, string filePath) { using (var writer = new BinaryWriter(File.Create(filePath), System.Text.Encoding.UTF8)) { writer.Write(symmetry.Key.Length); writer.Write(symmetry.Key); writer.Write(symmetry.IV.Length); writer.Write(symmetry.IV); } return(filePath); }
private void DownloadKeyAndIVDataFromUserPathButton_Click(object sender, EventArgs e) { try { OpenFileDialog.Filter = "Text file|*.txt"; OpenFileDialog.Title = "Select a file with Key and IV"; if (OpenFileDialog.ShowDialog() == DialogResult.OK) { if (AES.Checked) { Symmetry = new AesEncoder(); } else { Symmetry = new RijndaelEncoder(); } EncodersIOManager.ReadKeyAndIVFromFile(Symmetry, OpenFileDialog.FileName); MessageBox.Show("Your Key and IV were successfully downloaded", "Data Loaded", MessageBoxButtons.OK, MessageBoxIcon.Information); } OpenFileDialog.Dispose(); } catch (Exception Ex) { MessageBox.Show(Ex.Message, Ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error); File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "log.txt"), Ex.ToString()); } }
static RotationalTwoFold() { Symmetry = new RotationalTwoFold(); }
static Asymmetric() { Symmetry = new Asymmetric(); }
static RotationalFourFold() { Symmetry = new RotationalFourFold(); }
static Vertical() { Symmetry = new Vertical(); }
static Horizontal() { Symmetry = new Horizontal(); }
private void EncryptOrDecryptTextFromFileButton_Click(object sender, EventArgs e) { try { if (!File.Exists(PathInputTextBox.Text)) { throw new FileNotFoundException("The file on the path you specified does not exist!"); } if (AES.Checked && !(Symmetry is AesEncoder)) { Symmetry = new AesEncoder(); } else if (Rijndael.Checked && !(Symmetry is RijndaelEncoder)) { Symmetry = new RijndaelEncoder(); } string fileName = Symmetry is AesEncoder ? "AESEncryptionData.txt" : "RijndaelEncryptionData.txt"; string filePath = null; if (Encrypt.Checked) //encrypt text { SaveFileDialog.Title = "Select a file to save encrypted text"; if (SaveFileDialog.ShowDialog() == DialogResult.OK) { //Encryption: var encryptedBytes = Symmetry.EncryptStringIntoBytes(File.ReadAllText(PathInputTextBox.Text)); File.WriteAllBytes(SaveFileDialog.FileName, encryptedBytes); string dataFilePath = string.Concat(SaveFileDialog.FileName, '.', fileName); EncodersIOManager.WriteKeyAndIVInFile(Symmetry, dataFilePath); MessageBox.Show($"Encrypted text was saved to file at {SaveFileDialog.FileName};" + $"\ndata for decrypt was saved to file at {dataFilePath}", "Encryption Complete", MessageBoxButtons.OK, MessageBoxIcon.Information); EncryptedTextBox.Text = System.Text.Encoding.Default.GetString(encryptedBytes); } SaveFileDialog.Dispose(); } else //decrypt text { SaveFileDialog.Title = "Select a file to save decrypted text"; if (SaveFileDialog.ShowDialog() == DialogResult.OK) { //Decryption: EncryptedTextBox.Text = Symmetry.DecryptStringFromBytes(File.ReadAllBytes(PathInputTextBox.Text)); File.WriteAllText(SaveFileDialog.FileName, EncryptedTextBox.Text); MessageBox.Show($"Decrypted text was saved to file at {filePath}", "Decryption completed", MessageBoxButtons.OK, MessageBoxIcon.Information); } SaveFileDialog.Dispose(); } } catch (Exception Ex) { MessageBox.Show(Ex.Message, Ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error); File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "log.txt"), Ex.ToString()); } }
static DiagonalDown() { Symmetry = new DiagonalDown(); }
static DiagonalUp() { Symmetry = new DiagonalUp(); }