Beispiel #1
0
        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();
        }
Beispiel #2
0
        private void SaveKeyAndIVDataOnUserPathButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (Symmetry is null)
                {
                    throw new NullReferenceException("Current Key and IV are not installed in the program!");
                }
                if ((AES.Checked && Symmetry is RijndaelEncoder) || (Rijndael.Checked && Symmetry is AesEncoder))
                {
                    throw new FormatException("Key and IV cannot be saved because they belong to another encryption algorithm!");
                }

                SaveFileDialog.Title = "Save Key and IV";
                if (SaveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    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();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message,
                                Ex.GetType().Name,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                File.WriteAllText(Path.Combine(Directory.GetCurrentDirectory(), "log.txt"),
                                  Ex.ToString());
            }
        }
Beispiel #3
0
        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());
            }
        }