Public variables now stored in program.cs

This commit is contained in:
Sigmanor 2016-01-26 19:07:17 +02:00
parent 57326babd9
commit 71b13ca6fb
5 changed files with 48 additions and 44 deletions

View file

@ -14,10 +14,10 @@ public ChangeKeyForm()
private async void button1_Click(object sender, EventArgs e) 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; publicVar.encryptionKey = textBox2.Text;
MainWindow.keyChanged = true; publicVar.keyChanged = true;
textBox1.Text = ""; textBox1.Text = "";
textBox2.Text = ""; textBox2.Text = "";
statusLabel.Text = "Key was successfully changed!"; statusLabel.Text = "Key was successfully changed!";
@ -28,7 +28,7 @@ private async void button1_Click(object sender, EventArgs e)
return; return;
} }
if (textBox1.Text != MainWindow.encryptionKey) if (textBox1.Text != publicVar.encryptionKey)
{ {
SystemSounds.Beep.Play(); SystemSounds.Beep.Play();
statusLabel.Text = "Invalid old key!"; statusLabel.Text = "Invalid old key!";

View file

@ -16,12 +16,9 @@ namespace Crypto_Notepad
{ {
public partial class MainWindow : Form public partial class MainWindow : Form
{ {
public static string encryptionKey = ""; Properties.Settings ps = Properties.Settings.Default;
public static bool keyChanged = false;
public static bool settingsChanged = false;
string filename = "Unnamed.cnp"; string filename = "Unnamed.cnp";
string[] args = Environment.GetCommandLineArgs(); string[] args = Environment.GetCommandLineArgs();
Properties.Settings ps = Properties.Settings.Default;
int caretPos = 0; int caretPos = 0;
string appName = Assembly.GetExecutingAssembly().GetName().Name; string appName = Assembly.GetExecutingAssembly().GetName().Name;
@ -72,18 +69,18 @@ void DecryptAES()
{ {
Form2 f2 = new Form2(); Form2 f2 = new Form2();
f2.ShowDialog(); f2.ShowDialog();
if (Form2.OkPressed == false) if (publicVar.okPressed == false)
{ {
OpenFile.FileName = ""; OpenFile.FileName = "";
return; return;
} }
Form2.OkPressed = false; publicVar.okPressed = false;
try try
{ {
string opnfile = File.ReadAllText(OpenFile.FileName); string opnfile = File.ReadAllText(OpenFile.FileName);
string NameWithotPath = Path.GetFileName(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; customRTB.Text = de;
this.Text = appName + " - " + NameWithotPath; this.Text = appName + " - " + NameWithotPath;
@ -135,14 +132,14 @@ private void openAsotiations()
string opnfile = File.ReadAllText(args[1]); string opnfile = File.ReadAllText(args[1]);
Form2.ShowDialog(); Form2.ShowDialog();
if (Form2.OkPressed == false) if (publicVar.okPressed == false)
{ {
OpenFile.FileName = ""; OpenFile.FileName = "";
return; 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; customRTB.Text = de;
this.Text = appName + " - " + NameWithotPath; this.Text = appName + " - " + NameWithotPath;
@ -168,14 +165,14 @@ private void newToolStripMenuItem_Click_1(object sender, EventArgs e)
Form2 f2 = new Form2(); Form2 f2 = new Form2();
f2.ShowDialog(); f2.ShowDialog();
if (Form2.OkPressed == false) if (publicVar.okPressed == false)
{ {
return; return;
} }
if (Form2.OkPressed == true) if (publicVar.okPressed == true)
{ {
Form2.OkPressed = false; publicVar.okPressed = false;
if (SaveFile.ShowDialog() != DialogResult.OK) if (SaveFile.ShowDialog() != DialogResult.OK)
{ {
return; return;
@ -193,15 +190,15 @@ private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{ {
string NameWithotPath = Path.GetFileName(OpenFile.FileName); string NameWithotPath = Path.GetFileName(OpenFile.FileName);
if (string.IsNullOrEmpty(encryptionKey)) if (string.IsNullOrEmpty(publicVar.encryptionKey))
{ {
Form2 Form2 = new Form2(); Form2 Form2 = new Form2();
Form2.ShowDialog(); Form2.ShowDialog();
if (Form2.OkPressed == false) if (publicVar.okPressed == false)
{ {
return; return;
} }
Form2.OkPressed = false; publicVar.okPressed = false;
} }
if (SaveFile.ShowDialog() != DialogResult.OK) if (SaveFile.ShowDialog() != DialogResult.OK)
@ -212,7 +209,7 @@ private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
filename = SaveFile.FileName; filename = SaveFile.FileName;
string noenc = customRTB.Text; 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; customRTB.Text = en;
StreamWriter sw = new StreamWriter(filename); StreamWriter sw = new StreamWriter(filename);
int i = customRTB.Lines.Count(); int i = customRTB.Lines.Count();
@ -269,11 +266,11 @@ private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
DialogResult res = new DialogResult(); DialogResult res = new DialogResult();
string messageBoxText = ""; string messageBoxText = "";
if (keyChanged == false) if (publicVar.keyChanged == false)
{ {
messageBoxText = "Save file: " + "\"" + NameWithotPath + "\"" + " ? "; messageBoxText = "Save file: " + "\"" + NameWithotPath + "\"" + " ? ";
} }
if (keyChanged == true) if (publicVar.keyChanged == true)
{ {
messageBoxText = "Save file: " + "\"" + NameWithotPath + "\"" + " with a new key? "; messageBoxText = "Save file: " + "\"" + NameWithotPath + "\"" + " with a new key? ";
} }
@ -402,16 +399,16 @@ private void saveToolStripMenuItem1_Click_1(object sender, EventArgs e)
SaveFile.FileName = noname; SaveFile.FileName = noname;
saveAsToolStripMenuItem_Click(this, new EventArgs()); saveAsToolStripMenuItem_Click(this, new EventArgs());
if (Form2.OkPressed == false) if (publicVar.okPressed == false)
{ {
return; return;
} }
Form2.OkPressed = false; publicVar.okPressed = false;
} }
string noenc = customRTB.Text; 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; customRTB.Text = en;
StreamWriter sw = new StreamWriter(filename); StreamWriter sw = new StreamWriter(filename);
int i = customRTB.Lines.Count(); int i = customRTB.Lines.Count();
@ -493,7 +490,7 @@ private void deleteFileToolStripMenuItem_Click_1(object sender, EventArgs e)
{ {
File.Delete(filename); File.Delete(filename);
customRTB.Clear(); customRTB.Clear();
encryptionKey = ""; publicVar.encryptionKey = "";
pictureBox11.Enabled = false; pictureBox11.Enabled = false;
filename = "Unnamed.cnp"; filename = "Unnamed.cnp";
this.Text = appName; this.Text = appName;
@ -670,7 +667,7 @@ private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
private void MainWindow_Activated(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.Font = new Font(ps.RichTextFont, ps.RichTextSize);
customRTB.ForeColor = ps.RichForeColor; 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; customRTB.Modified = true;
} }
if (encryptionKey == "") if (publicVar.encryptionKey == "")
{ {
pictureBox11.Enabled = false; pictureBox11.Enabled = false;
pictureBox13.Enabled = false; pictureBox13.Enabled = false;
@ -708,7 +705,7 @@ private void MainWindow_Activated(object sender, EventArgs e)
pictureBox7.Enabled = false; pictureBox7.Enabled = false;
} }
if (encryptionKey != "") if (publicVar.encryptionKey != "")
{ {
pictureBox11.Enabled = true; pictureBox11.Enabled = true;
pictureBox13.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) private void сервисToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{ {
if (encryptionKey == "") if (publicVar.encryptionKey == "")
{ {
changeKeyToolStripMenuItem.Enabled = false; changeKeyToolStripMenuItem.Enabled = false;
lockToolStripMenuItem.Enabled = false; lockToolStripMenuItem.Enabled = false;
} }
if (encryptionKey != "") if (publicVar.encryptionKey != "")
{ {
changeKeyToolStripMenuItem.Enabled = true; changeKeyToolStripMenuItem.Enabled = true;
lockToolStripMenuItem.Enabled = true; lockToolStripMenuItem.Enabled = true;
@ -928,7 +925,7 @@ private void pictureBox1_MouseLeave(object sender, EventArgs e)
void AutoLock(bool minimize) void AutoLock(bool minimize)
{ {
Form2 f2 = new Form2(); Form2 f2 = new Form2();
encryptionKey = ""; publicVar.encryptionKey = "";
caretPos = customRTB.SelectionStart; caretPos = customRTB.SelectionStart;
this.Hide(); this.Hide();
if (minimize == true) if (minimize == true)
@ -937,23 +934,23 @@ void AutoLock(bool minimize)
} }
f2.ShowDialog(); f2.ShowDialog();
if (Form2.OkPressed == false) if (publicVar.okPressed == false)
{ {
encryptionKey = ""; publicVar.encryptionKey = "";
customRTB.Clear(); customRTB.Clear();
this.Text = appName; this.Text = appName;
OpenFile.FileName = ""; OpenFile.FileName = "";
this.Show(); this.Show();
return; return;
} }
Form2.OkPressed = false; publicVar.okPressed = false;
try try
{ {
OpenFile.FileName = filename; OpenFile.FileName = filename;
string opnfile = File.ReadAllText(OpenFile.FileName); string opnfile = File.ReadAllText(OpenFile.FileName);
string NameWithotPath = Path.GetFileName(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; this.Text = appName + " - " + NameWithotPath;
filename = OpenFile.FileName; filename = OpenFile.FileName;
@ -971,7 +968,7 @@ void AutoLock(bool minimize)
} }
if (dialogResult == DialogResult.Cancel) if (dialogResult == DialogResult.Cancel)
{ {
encryptionKey = ""; publicVar.encryptionKey = "";
customRTB.Clear(); customRTB.Clear();
this.Text = appName; this.Text = appName;
OpenFile.FileName = ""; OpenFile.FileName = "";
@ -987,7 +984,7 @@ protected override void WndProc(ref Message m)
const int WM_SYSCOMMAND = 0x112; const int WM_SYSCOMMAND = 0x112;
const int SC_MINIMIZE = 0xF020; 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); AutoLock(true);
return; return;

View file

@ -5,7 +5,6 @@ namespace Crypto_Notepad
{ {
public partial class Form2 : Form public partial class Form2 : Form
{ {
public static bool OkPressed = false;
public Form2() public Form2()
{ {
InitializeComponent(); InitializeComponent();
@ -13,10 +12,10 @@ public Form2()
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
{ {
MainWindow.encryptionKey = textBox1.Text; publicVar.encryptionKey = textBox1.Text;
textBox1.Focus(); textBox1.Focus();
textBox1.Text = ""; textBox1.Text = "";
OkPressed = true; publicVar.okPressed = true;
this.Hide(); this.Hide();
} }

View file

@ -16,4 +16,12 @@ static void Main()
Application.Run(new MainWindow()); 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; }
}
} }

View file

@ -77,7 +77,7 @@ private void SetSettings(string value)
ps.AutoLock = checkBox4.Checked; ps.AutoLock = checkBox4.Checked;
ps.Save(); ps.Save();
MainWindow.settingsChanged = true; publicVar.settingsChanged = true;
this.Hide(); this.Hide();
} }