From 71b13ca6fb0a22ed15fdda84d36b7bfd6c33d160 Mon Sep 17 00:00:00 2001 From: Sigmanor Date: Tue, 26 Jan 2016 19:07:17 +0200 Subject: [PATCH] Public variables now stored in program.cs --- Crypto Notepad/ChangeKeyForm.cs | 8 ++-- Crypto Notepad/Form1.cs | 69 ++++++++++++++++----------------- Crypto Notepad/Form2.cs | 5 +-- Crypto Notepad/Program.cs | 8 ++++ Crypto Notepad/SettingsForm.cs | 2 +- 5 files changed, 48 insertions(+), 44 deletions(-) diff --git a/Crypto Notepad/ChangeKeyForm.cs b/Crypto Notepad/ChangeKeyForm.cs index d77d5a7..06eab98 100644 --- a/Crypto Notepad/ChangeKeyForm.cs +++ b/Crypto Notepad/ChangeKeyForm.cs @@ -14,10 +14,10 @@ public ChangeKeyForm() private async void button1_Click(object sender, EventArgs e) { - if (textBox1.Text == MainWindow.encryptionKey & textBox1.Text != textBox2.Text) + if (textBox1.Text == publicVar.encryptionKey & textBox1.Text != textBox2.Text) { - MainWindow.encryptionKey = textBox2.Text; - MainWindow.keyChanged = true; + publicVar.encryptionKey = textBox2.Text; + publicVar.keyChanged = true; textBox1.Text = ""; textBox2.Text = ""; statusLabel.Text = "Key was successfully changed!"; @@ -28,7 +28,7 @@ private async void button1_Click(object sender, EventArgs e) return; } - if (textBox1.Text != MainWindow.encryptionKey) + if (textBox1.Text != publicVar.encryptionKey) { SystemSounds.Beep.Play(); statusLabel.Text = "Invalid old key!"; diff --git a/Crypto Notepad/Form1.cs b/Crypto Notepad/Form1.cs index 3ebce65..6329e7f 100644 --- a/Crypto Notepad/Form1.cs +++ b/Crypto Notepad/Form1.cs @@ -16,12 +16,9 @@ namespace Crypto_Notepad { public partial class MainWindow : Form { - public static string encryptionKey = ""; - public static bool keyChanged = false; - public static bool settingsChanged = false; + Properties.Settings ps = Properties.Settings.Default; string filename = "Unnamed.cnp"; string[] args = Environment.GetCommandLineArgs(); - Properties.Settings ps = Properties.Settings.Default; int caretPos = 0; string appName = Assembly.GetExecutingAssembly().GetName().Name; @@ -72,18 +69,18 @@ void DecryptAES() { Form2 f2 = new Form2(); f2.ShowDialog(); - if (Form2.OkPressed == false) + if (publicVar.okPressed == false) { OpenFile.FileName = ""; return; } - Form2.OkPressed = false; + publicVar.okPressed = false; try { string opnfile = File.ReadAllText(OpenFile.FileName); string NameWithotPath = Path.GetFileName(OpenFile.FileName); - string de = AES.Decrypt(opnfile, encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize); + string de = AES.Decrypt(opnfile, publicVar.encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize); customRTB.Text = de; this.Text = appName + " - " + NameWithotPath; @@ -135,14 +132,14 @@ private void openAsotiations() string opnfile = File.ReadAllText(args[1]); Form2.ShowDialog(); - if (Form2.OkPressed == false) + if (publicVar.okPressed == false) { OpenFile.FileName = ""; return; } - Form2.OkPressed = false; + publicVar.okPressed = false; - string de = AES.Decrypt(opnfile, encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize); + string de = AES.Decrypt(opnfile, publicVar.encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize); customRTB.Text = de; this.Text = appName + " - " + NameWithotPath; @@ -168,14 +165,14 @@ private void newToolStripMenuItem_Click_1(object sender, EventArgs e) Form2 f2 = new Form2(); f2.ShowDialog(); - if (Form2.OkPressed == false) + if (publicVar.okPressed == false) { return; } - if (Form2.OkPressed == true) + if (publicVar.okPressed == true) { - Form2.OkPressed = false; + publicVar.okPressed = false; if (SaveFile.ShowDialog() != DialogResult.OK) { return; @@ -193,15 +190,15 @@ private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { string NameWithotPath = Path.GetFileName(OpenFile.FileName); - if (string.IsNullOrEmpty(encryptionKey)) + if (string.IsNullOrEmpty(publicVar.encryptionKey)) { Form2 Form2 = new Form2(); Form2.ShowDialog(); - if (Form2.OkPressed == false) + if (publicVar.okPressed == false) { return; } - Form2.OkPressed = false; + publicVar.okPressed = false; } if (SaveFile.ShowDialog() != DialogResult.OK) @@ -212,7 +209,7 @@ private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) filename = SaveFile.FileName; string noenc = customRTB.Text; - string en = AES.Encrypt(customRTB.Text, encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize); + string en = AES.Encrypt(customRTB.Text, publicVar.encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize); customRTB.Text = en; StreamWriter sw = new StreamWriter(filename); int i = customRTB.Lines.Count(); @@ -269,11 +266,11 @@ private void MainWindow_FormClosing(object sender, FormClosingEventArgs e) DialogResult res = new DialogResult(); string messageBoxText = ""; - if (keyChanged == false) + if (publicVar.keyChanged == false) { messageBoxText = "Save file: " + "\"" + NameWithotPath + "\"" + " ? "; } - if (keyChanged == true) + if (publicVar.keyChanged == true) { messageBoxText = "Save file: " + "\"" + NameWithotPath + "\"" + " with a new key? "; } @@ -402,16 +399,16 @@ private void saveToolStripMenuItem1_Click_1(object sender, EventArgs e) SaveFile.FileName = noname; saveAsToolStripMenuItem_Click(this, new EventArgs()); - if (Form2.OkPressed == false) + if (publicVar.okPressed == false) { return; } - Form2.OkPressed = false; + publicVar.okPressed = false; } string noenc = customRTB.Text; - string en = AES.Encrypt(customRTB.Text, encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize); + string en = AES.Encrypt(customRTB.Text, publicVar.encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize); customRTB.Text = en; StreamWriter sw = new StreamWriter(filename); int i = customRTB.Lines.Count(); @@ -493,7 +490,7 @@ private void deleteFileToolStripMenuItem_Click_1(object sender, EventArgs e) { File.Delete(filename); customRTB.Clear(); - encryptionKey = ""; + publicVar.encryptionKey = ""; pictureBox11.Enabled = false; filename = "Unnamed.cnp"; this.Text = appName; @@ -670,7 +667,7 @@ private void settingsToolStripMenuItem_Click(object sender, EventArgs e) private void MainWindow_Activated(object sender, EventArgs e) { - if (settingsChanged == true) + if (publicVar.settingsChanged == true) { customRTB.Font = new Font(ps.RichTextFont, ps.RichTextSize); customRTB.ForeColor = ps.RichForeColor; @@ -695,12 +692,12 @@ private void MainWindow_Activated(object sender, EventArgs e) } } - if (keyChanged == true) + if (publicVar.keyChanged == true) { customRTB.Modified = true; } - if (encryptionKey == "") + if (publicVar.encryptionKey == "") { pictureBox11.Enabled = false; pictureBox13.Enabled = false; @@ -708,7 +705,7 @@ private void MainWindow_Activated(object sender, EventArgs e) pictureBox7.Enabled = false; } - if (encryptionKey != "") + if (publicVar.encryptionKey != "") { pictureBox11.Enabled = true; pictureBox13.Enabled = true; @@ -761,13 +758,13 @@ private void documentationToolStripMenuItem_Click(object sender, EventArgs e) private void сервисToolStripMenuItem_DropDownOpened(object sender, EventArgs e) { - if (encryptionKey == "") + if (publicVar.encryptionKey == "") { changeKeyToolStripMenuItem.Enabled = false; lockToolStripMenuItem.Enabled = false; } - if (encryptionKey != "") + if (publicVar.encryptionKey != "") { changeKeyToolStripMenuItem.Enabled = true; lockToolStripMenuItem.Enabled = true; @@ -928,7 +925,7 @@ private void pictureBox1_MouseLeave(object sender, EventArgs e) void AutoLock(bool minimize) { Form2 f2 = new Form2(); - encryptionKey = ""; + publicVar.encryptionKey = ""; caretPos = customRTB.SelectionStart; this.Hide(); if (minimize == true) @@ -937,23 +934,23 @@ void AutoLock(bool minimize) } f2.ShowDialog(); - if (Form2.OkPressed == false) + if (publicVar.okPressed == false) { - encryptionKey = ""; + publicVar.encryptionKey = ""; customRTB.Clear(); this.Text = appName; OpenFile.FileName = ""; this.Show(); return; } - Form2.OkPressed = false; + publicVar.okPressed = false; try { OpenFile.FileName = filename; string opnfile = File.ReadAllText(OpenFile.FileName); string NameWithotPath = Path.GetFileName(OpenFile.FileName); - string de = AES.Decrypt(opnfile, encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize); + string de = AES.Decrypt(opnfile, publicVar.encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize); this.Text = appName + " - " + NameWithotPath; filename = OpenFile.FileName; @@ -971,7 +968,7 @@ void AutoLock(bool minimize) } if (dialogResult == DialogResult.Cancel) { - encryptionKey = ""; + publicVar.encryptionKey = ""; customRTB.Clear(); this.Text = appName; OpenFile.FileName = ""; @@ -987,7 +984,7 @@ protected override void WndProc(ref Message m) const int WM_SYSCOMMAND = 0x112; const int SC_MINIMIZE = 0xF020; - if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE && ps.AutoLock == true && encryptionKey != "") + if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE && ps.AutoLock == true && publicVar.encryptionKey != "") { AutoLock(true); return; diff --git a/Crypto Notepad/Form2.cs b/Crypto Notepad/Form2.cs index 05ee5e8..f5bb003 100644 --- a/Crypto Notepad/Form2.cs +++ b/Crypto Notepad/Form2.cs @@ -5,7 +5,6 @@ namespace Crypto_Notepad { public partial class Form2 : Form { - public static bool OkPressed = false; public Form2() { InitializeComponent(); @@ -13,10 +12,10 @@ public Form2() private void button1_Click(object sender, EventArgs e) { - MainWindow.encryptionKey = textBox1.Text; + publicVar.encryptionKey = textBox1.Text; textBox1.Focus(); textBox1.Text = ""; - OkPressed = true; + publicVar.okPressed = true; this.Hide(); } diff --git a/Crypto Notepad/Program.cs b/Crypto Notepad/Program.cs index 344183d..40d4534 100644 --- a/Crypto Notepad/Program.cs +++ b/Crypto Notepad/Program.cs @@ -16,4 +16,12 @@ static void Main() Application.Run(new MainWindow()); } } + + static class publicVar + { + public static string encryptionKey { get; set; } + public static bool keyChanged { get; set; } + public static bool settingsChanged{ get; set; } + public static bool okPressed { get; set; } + } } diff --git a/Crypto Notepad/SettingsForm.cs b/Crypto Notepad/SettingsForm.cs index 49ae978..afb2cf5 100644 --- a/Crypto Notepad/SettingsForm.cs +++ b/Crypto Notepad/SettingsForm.cs @@ -77,7 +77,7 @@ private void SetSettings(string value) ps.AutoLock = checkBox4.Checked; ps.Save(); - MainWindow.settingsChanged = true; + publicVar.settingsChanged = true; this.Hide(); }