From 04117e65c76686387b4435d958c6b57bc76f222a Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 5 Nov 2019 22:30:49 +0200 Subject: [PATCH] Resolved #40 Encrypt/decrypt all file formats --- Crypto Notepad/MainForm.Designer.cs | 9 +- Crypto Notepad/MainForm.cs | 207 ++++++++++------------------ Crypto Notepad/MainForm.resx | 10 +- 3 files changed, 83 insertions(+), 143 deletions(-) diff --git a/Crypto Notepad/MainForm.Designer.cs b/Crypto Notepad/MainForm.Designer.cs index 848b9e5..18a3b6f 100644 --- a/Crypto Notepad/MainForm.Designer.cs +++ b/Crypto Notepad/MainForm.Designer.cs @@ -620,7 +620,7 @@ public void InitializeComponent() // // saveFileDialog // - this.saveFileDialog.Filter = "Crypto Notepad (*.cnp)|*.cnp"; + this.saveFileDialog.Filter = "Crypto Notepad (*.cnp)|*.cnp|Normal text file (*.txt)|*.txt|All Files (*.*)|*.*"; // // searchPanel // @@ -1118,13 +1118,13 @@ public void InitializeComponent() // this.fileLockedKeyTextBox.Location = new System.Drawing.Point(6, 34); this.fileLockedKeyTextBox.Name = "fileLockedKeyTextBox"; - this.fileLockedKeyTextBox.Size = new System.Drawing.Size(231, 22); - this.fileLockedKeyTextBox.TabIndex = 8; - this.fileLockedKeyTextBox.UseSystemPasswordChar = true; this.fileLockedKeyTextBox.Placeholder = "Encryption key"; this.fileLockedKeyTextBox.PlaceholderActiveForeColor = System.Drawing.Color.DarkGray; this.fileLockedKeyTextBox.PlaceholderFont = new System.Drawing.Font("Segoe UI", 8.25F); this.fileLockedKeyTextBox.PlaceholderForeColor = System.Drawing.Color.DarkGray; + this.fileLockedKeyTextBox.Size = new System.Drawing.Size(231, 22); + this.fileLockedKeyTextBox.TabIndex = 8; + this.fileLockedKeyTextBox.UseSystemPasswordChar = true; this.fileLockedKeyTextBox.TextChanged += new System.EventHandler(this.FileLockedKeyTextBox_TextChanged); this.fileLockedKeyTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FileLockedKeyTextBox_KeyDown); // @@ -1180,6 +1180,7 @@ public void InitializeComponent() this.richTextBox.Text = ""; this.richTextBox.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.RichTextBox_LinkClicked); this.richTextBox.SelectionChanged += new System.EventHandler(this.RichTextBox_SelectionChanged); + this.richTextBox.ModifiedChanged += new System.EventHandler(this.RichTextBox_ModifiedChanged); this.richTextBox.TextChanged += new System.EventHandler(this.RichTextBox_TextChanged); this.richTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.RichTextBox_KeyDown); // diff --git a/Crypto Notepad/MainForm.cs b/Crypto Notepad/MainForm.cs index b76e11e..93c1012 100644 --- a/Crypto Notepad/MainForm.cs +++ b/Crypto Notepad/MainForm.cs @@ -125,47 +125,22 @@ private void OpenAsotiations() }; string fileExtension = Path.GetExtension(args[1]); PublicVar.openFileName = Path.GetFileName(args[1]); - if (fileExtension == ".cnp") + openFileDialog.FileName = Path.GetFullPath(args[1]); + if (fileExtension != ".cnp") { - try - { - string NameWithotPath = Path.GetFileName(args[1]); - string opnfile = File.ReadAllText(args[1]); - enterKeyForm.ShowDialog(); - string de = AES.Decrypt(opnfile, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); - richTextBox.Text = de; - Text = PublicVar.appName + " – " + NameWithotPath; - filePath = args[1]; - PublicVar.encryptionKey.Set(TypedPassword.Value); - TypedPassword.Value = null; - StatusPanelFileInfo(); - } - catch (CryptographicException) - { - TypedPassword.Value = null; - DialogResult dialogResult = MessageBox.Show(this, "Invalid key!", PublicVar.appName, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); - if (dialogResult == DialogResult.Retry) + DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); + if (res == DialogResult.No) { - OpenAsotiations(); + string opnfile = File.ReadAllText(args[1]); + string NameWithotPath = Path.GetFileName(args[1]); + richTextBox.Text = opnfile; + filePath = args[1]; + Text = PublicVar.appName + " – " + NameWithotPath; + StatusPanelFileInfo(); + return; } - if (dialogResult == DialogResult.Cancel) - { - if (!Visible) - { - Application.Exit(); - } - } - } - } - else - { - string opnfile = File.ReadAllText(args[1]); - string NameWithotPath = Path.GetFileName(args[1]); - richTextBox.Text = opnfile; - filePath = args[1]; - Text = PublicVar.appName + " – " + NameWithotPath; - StatusPanelFileInfo(); } + DecryptAES(); } private void SendTo() @@ -176,108 +151,58 @@ private void SendTo() StartPosition = FormStartPosition.CenterScreen }; string fileExtension = Path.GetExtension(argsPath); - if (fileExtension == ".cnp") + openFileDialog.FileName = Path.GetFullPath(argsPath); + PublicVar.openFileName = Path.GetFileName(argsPath); + if (fileExtension != ".cnp") { - try + DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); + if (res == DialogResult.No) { - string NameWithotPath = Path.GetFileName(argsPath); string opnfile = File.ReadAllText(argsPath); - PublicVar.openFileName = Path.GetFileName(argsPath); - enterKeyForm.ShowDialog(); - string de = AES.Decrypt(opnfile, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); - richTextBox.Text = de; - Text = PublicVar.appName + " – " + NameWithotPath; + string NameWithotPath = Path.GetFileName(argsPath); + richTextBox.Text = opnfile; filePath = argsPath; - PublicVar.encryptionKey.Set(TypedPassword.Value); - TypedPassword.Value = null; + Text = PublicVar.appName + " – " + NameWithotPath; StatusPanelFileInfo(); - } - catch (CryptographicException) - { - TypedPassword.Value = null; - DialogResult dialogResult = MessageBox.Show(this, "Invalid key!", PublicVar.appName, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); - if (dialogResult == DialogResult.Retry) - { - SendTo(); - } - if (dialogResult == DialogResult.Cancel) - { - if (!Visible) - { - Application.Exit(); - } - } + return; } } - else - { - string opnfile = File.ReadAllText(argsPath); - string NameWithotPath = Path.GetFileName(argsPath); - richTextBox.Text = opnfile; - Text = PublicVar.appName + " – " + NameWithotPath; - } + DecryptAES(); } private void ContextMenuEncryptReplace() { - if (args[1].Contains(".cnp")) - { - MessageBox.Show(this, "Looks like this file is already encrypted", PublicVar.appName, MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } DialogResult res = MessageBox.Show(this, "This action will delete the source file and replace it with encrypted version", PublicVar.appName, MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (res == DialogResult.Cancel) { Environment.Exit(0); } - if (!args[1].Contains(".cnp")) + string opnfile = File.ReadAllText(args[1]); + richTextBox.Text = opnfile; + PublicVar.openFileName = Path.GetFileName(args[1]); + string newFile = Path.GetDirectoryName(args[1]) + @"\" + Path.GetFileNameWithoutExtension(args[1]) + ".cnp"; + EnterKeyForm enterKeyForm = new EnterKeyForm { - string opnfile = File.ReadAllText(args[1]); - richTextBox.Text = opnfile; - PublicVar.openFileName = Path.GetFileName(args[1]); - string newFile = Path.GetDirectoryName(args[1]) + @"\" + Path.GetFileNameWithoutExtension(args[1]) + ".cnp"; - EnterKeyForm enterKeyForm = new EnterKeyForm - { - Owner = this - }; - enterKeyForm.ShowDialog(); - File.Delete(args[1]); - string noenc = richTextBox.Text; - string en; - en = AES.Encrypt(richTextBox.Text, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); - richTextBox.Text = en; - StreamWriter sw = new StreamWriter(newFile); - int i = richTextBox.Lines.Count(); - int j = 0; - i = i - 1; - while (j <= i) - { - sw.WriteLine(richTextBox.Lines.GetValue(j).ToString()); - j = j + 1; - } - sw.Close(); - PublicVar.encryptionKey.Set(TypedPassword.Value); - TypedPassword.Value = null; - filePath = newFile; - PublicVar.openFileName = Path.GetFileName(newFile); - Text = PublicVar.appName + " – " + PublicVar.openFileName; - richTextBox.Text = noenc; - StatusPanelFileInfo(); - } - richTextBox.Modified = false; - } - - private void ContextMenuEncrypt() - { - if (!args[1].Contains(".cnp")) + Owner = this + }; + enterKeyForm.ShowDialog(); + File.Delete(args[1]); + string noenc = richTextBox.Text; + string en; + en = AES.Encrypt(richTextBox.Text, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); + richTextBox.Text = en; + using (StreamWriter writer = new StreamWriter(newFile)) { - string opnfile = File.ReadAllText(args[1]); - string NameWithotPath = Path.GetFileName(args[1]); - richTextBox.Text = opnfile; - Text = PublicVar.appName + NameWithotPath; - PublicVar.openFileName = Path.GetFileName(args[1]); - filePath = openFileDialog.FileName; + writer.Write(richTextBox.Text); + writer.Close(); } + PublicVar.encryptionKey.Set(TypedPassword.Value); + TypedPassword.Value = null; + filePath = newFile; + PublicVar.openFileName = Path.GetFileName(newFile); + Text = PublicVar.appName + " – " + PublicVar.openFileName; + richTextBox.Text = noenc; + StatusPanelFileInfo(); richTextBox.Modified = false; } @@ -906,17 +831,24 @@ private void RichTextBox_DragDrop(object sender, DragEventArgs e) { if (!openFileDialog.FileName.Contains(".cnp")) { - string opnfile = File.ReadAllText(openFileDialog.FileName); - string NameWithotPath = Path.GetFileName(openFileDialog.FileName); - richTextBox.Text = opnfile; - Text = PublicVar.appName + " – " + NameWithotPath; - filePath = openFileDialog.FileName; - StatusPanelFileInfo(); - return; if (Visible) { PublicVar.messageBoxCenterParent = true; } + using (new CenterWinDialog(this)) + { + DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); + if (res == DialogResult.No) + { + string opnfile = File.ReadAllText(openFileDialog.FileName); + string NameWithotPath = Path.GetFileName(openFileDialog.FileName); + richTextBox.Text = opnfile; + Text = PublicVar.appName + " – " + NameWithotPath; + filePath = openFileDialog.FileName; + StatusPanelFileInfo(); + return; + } + } } DecryptAES(); } @@ -1024,13 +956,20 @@ private void OpenMainMenu_Click(object sender, EventArgs e) PublicVar.openFileName = Path.GetFileName(openFileDialog.FileName); if (!openFileDialog.FileName.Contains(".cnp")) { - string opnfile = File.ReadAllText(openFileDialog.FileName); - string NameWithotPath = Path.GetFileName(openFileDialog.FileName); - richTextBox.Text = opnfile; - Text = PublicVar.appName + " – " + NameWithotPath; - filePath = openFileDialog.FileName; - StatusPanelFileInfo(); - return; + using (new CenterWinDialog(this)) + { + DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); + if (res == DialogResult.No) + { + string opnfile = File.ReadAllText(openFileDialog.FileName); + string NameWithotPath = Path.GetFileName(openFileDialog.FileName); + richTextBox.Text = opnfile; + Text = PublicVar.appName + " – " + NameWithotPath; + filePath = openFileDialog.FileName; + StatusPanelFileInfo(); + return; + } + } } DecryptAES(); richTextBox.Modified = false; diff --git a/Crypto Notepad/MainForm.resx b/Crypto Notepad/MainForm.resx index 65ccef3..63b73e3 100644 --- a/Crypto Notepad/MainForm.resx +++ b/Crypto Notepad/MainForm.resx @@ -121,7 +121,7 @@ 17, 17 - 17, 56 + 676, 17 127, 17 @@ -146,7 +146,7 @@ - 453, 56 + 1112, 17 @@ -290,13 +290,13 @@ - 245, 56 + 904, 17 - 358, 56 + 1017, 17 - 141, 56 + 800, 17