From c9bd9f436d38087023bd56dee465e2fa1e5a737a Mon Sep 17 00:00:00 2001 From: h5p9sl Date: Fri, 30 Nov 2018 19:29:19 -0700 Subject: [PATCH] Added EncryptedString class to encrypt key in memory. --- Crypto Notepad/ChangeKeyForm.cs | 6 ++-- Crypto Notepad/Crypto Notepad.csproj | 1 + Crypto Notepad/EncryptedString.cs | 46 ++++++++++++++++++++++++++++ Crypto Notepad/Form1.cs | 30 +++++++++--------- Crypto Notepad/Form2.cs | 2 +- Crypto Notepad/Program.cs | 2 +- 6 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 Crypto Notepad/EncryptedString.cs diff --git a/Crypto Notepad/ChangeKeyForm.cs b/Crypto Notepad/ChangeKeyForm.cs index 06eab98..0fa250a 100644 --- a/Crypto Notepad/ChangeKeyForm.cs +++ b/Crypto Notepad/ChangeKeyForm.cs @@ -14,9 +14,9 @@ public ChangeKeyForm() private async void button1_Click(object sender, EventArgs e) { - if (textBox1.Text == publicVar.encryptionKey & textBox1.Text != textBox2.Text) + if (textBox1.Text == publicVar.encryptionKey.Get() & textBox1.Text != textBox2.Text) { - publicVar.encryptionKey = textBox2.Text; + publicVar.encryptionKey.Set(textBox2.Text); publicVar.keyChanged = true; textBox1.Text = ""; textBox2.Text = ""; @@ -28,7 +28,7 @@ private async void button1_Click(object sender, EventArgs e) return; } - if (textBox1.Text != publicVar.encryptionKey) + if (textBox1.Text != publicVar.encryptionKey.Get()) { SystemSounds.Beep.Play(); statusLabel.Text = "Invalid old key!"; diff --git a/Crypto Notepad/Crypto Notepad.csproj b/Crypto Notepad/Crypto Notepad.csproj index cea71f0..873edd1 100644 --- a/Crypto Notepad/Crypto Notepad.csproj +++ b/Crypto Notepad/Crypto Notepad.csproj @@ -105,6 +105,7 @@ CustomRichTextBox.cs + Form diff --git a/Crypto Notepad/EncryptedString.cs b/Crypto Notepad/EncryptedString.cs new file mode 100644 index 0000000..3e304c2 --- /dev/null +++ b/Crypto Notepad/EncryptedString.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Security.Cryptography; + +namespace Crypto_Notepad +{ + /// + /// Stores a string encrypted in memory to defend against memory dumps + /// + class EncryptedString + { + private TripleDES des = TripleDESCryptoServiceProvider.Create(); + private byte[] encryptedString = null; + + public EncryptedString(string String) + { + this.des.GenerateIV(); + this.des.GenerateKey(); + this.Set(String); + } + + public EncryptedString() + { + this.des.GenerateIV(); + this.des.GenerateKey(); + } + + public string Get() + { + if (this.encryptedString == null) return null; + var decryptor = this.des.CreateDecryptor(); + byte[] output = decryptor.TransformFinalBlock(this.encryptedString, 0, this.encryptedString.Length); + return Encoding.Default.GetString(output); + } + + public void Set(string String) + { + var encryptor = this.des.CreateEncryptor(); + byte[] str = Encoding.Default.GetBytes(String); + this.encryptedString = encryptor.TransformFinalBlock(str, 0, str.Length); + } + } +} diff --git a/Crypto Notepad/Form1.cs b/Crypto Notepad/Form1.cs index a0a363f..304ad5d 100644 --- a/Crypto Notepad/Form1.cs +++ b/Crypto Notepad/Form1.cs @@ -88,7 +88,7 @@ void DecryptAES() { string opnfile = File.ReadAllText(OpenFile.FileName); string NameWithotPath = Path.GetFileName(OpenFile.FileName); - string de = AES.Decrypt(opnfile, publicVar.encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize); + string de = AES.Decrypt(opnfile, publicVar.encryptionKey.Get(), ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize); customRTB.Text = de; this.Text = appName + NameWithotPath; @@ -165,7 +165,7 @@ private void openAsotiations() } publicVar.okPressed = false; - string de = AES.Decrypt(opnfile, publicVar.encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize); + string de = AES.Decrypt(opnfile, publicVar.encryptionKey.Get(), ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize); customRTB.Text = de; this.Text = appName + NameWithotPath; @@ -216,7 +216,7 @@ private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { int saveCaret = customRTB.SelectionStart; string NameWithotPath = Path.GetFileName(OpenFile.FileName); - if (string.IsNullOrEmpty(publicVar.encryptionKey)) + if (string.IsNullOrEmpty(publicVar.encryptionKey.Get())) { Form2 Form2 = new Form2(); Form2.ShowDialog(); @@ -235,7 +235,7 @@ private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) filename = SaveFile.FileName; string noenc = customRTB.Text; - string en = AES.Encrypt(customRTB.Text, publicVar.encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize); + string en = AES.Encrypt(customRTB.Text, publicVar.encryptionKey.Get(), ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize); customRTB.Text = en; StreamWriter sw = new StreamWriter(filename); @@ -376,7 +376,7 @@ private void saveToolStripMenuItem1_Click_1(object sender, EventArgs e) } string noenc = customRTB.Text; - string en = AES.Encrypt(customRTB.Text, publicVar.encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize); + string en = AES.Encrypt(customRTB.Text, publicVar.encryptionKey.Get(), ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize); customRTB.Text = en; StreamWriter sw = new StreamWriter(filename); @@ -458,7 +458,7 @@ private void deleteFileToolStripMenuItem_Click_1(object sender, EventArgs e) { File.Delete(filename); customRTB.Clear(); - publicVar.encryptionKey = ""; + publicVar.encryptionKey.Set(null); pictureBox6.Enabled = false; pictureBox7.Enabled = false; pictureBox11.Enabled = false; @@ -675,7 +675,7 @@ private void MainWindow_Activated(object sender, EventArgs e) customRTB.Modified = true; } - if (publicVar.encryptionKey == "") + if (publicVar.encryptionKey.Get() == null) { pictureBox6.Enabled = false; pictureBox7.Enabled = false; @@ -683,7 +683,7 @@ private void MainWindow_Activated(object sender, EventArgs e) pictureBox13.Enabled = false; } - if (publicVar.encryptionKey != "") + if (publicVar.encryptionKey.Get() != null) { pictureBox6.Enabled = true; pictureBox7.Enabled = true; @@ -736,13 +736,13 @@ private void documentationToolStripMenuItem_Click(object sender, EventArgs e) private void сервисToolStripMenuItem_DropDownOpened(object sender, EventArgs e) { - if (publicVar.encryptionKey == "") + if (publicVar.encryptionKey.Get() == null) { changeKeyToolStripMenuItem.Enabled = false; lockToolStripMenuItem.Enabled = false; } - if (publicVar.encryptionKey != "") + if (publicVar.encryptionKey.Get() != null) { changeKeyToolStripMenuItem.Enabled = true; lockToolStripMenuItem.Enabled = true; @@ -911,7 +911,7 @@ void AutoLock(bool minimize) { saveToolStripMenuItem1_Click_1(this, new EventArgs()); Form2 f2 = new Form2(); - publicVar.encryptionKey = ""; + publicVar.encryptionKey.Set(null); caretPos = customRTB.SelectionStart; f2.MinimizeBox = true; this.Hide(); @@ -924,7 +924,7 @@ void AutoLock(bool minimize) if (publicVar.okPressed == false) { - publicVar.encryptionKey = ""; + publicVar.encryptionKey.Set(null); customRTB.Clear(); this.Text = appName.Remove(14); OpenFile.FileName = ""; @@ -938,7 +938,7 @@ void AutoLock(bool minimize) OpenFile.FileName = filename; string opnfile = File.ReadAllText(OpenFile.FileName); string NameWithotPath = Path.GetFileName(OpenFile.FileName); - string de = AES.Decrypt(opnfile, publicVar.encryptionKey, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize); + string de = AES.Decrypt(opnfile, publicVar.encryptionKey.Get(), ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize); this.Text = appName + NameWithotPath; filename = OpenFile.FileName; @@ -956,7 +956,7 @@ void AutoLock(bool minimize) } if (dialogResult == DialogResult.Cancel) { - publicVar.encryptionKey = ""; + publicVar.encryptionKey.Set(null); customRTB.Clear(); this.Text = appName.Remove(14); OpenFile.FileName = ""; @@ -972,7 +972,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 && publicVar.encryptionKey != "") + if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE && ps.AutoLock == true && publicVar.encryptionKey.Get() != null) { AutoLock(true); return; diff --git a/Crypto Notepad/Form2.cs b/Crypto Notepad/Form2.cs index 8b1bfaa..bd36d6a 100644 --- a/Crypto Notepad/Form2.cs +++ b/Crypto Notepad/Form2.cs @@ -14,7 +14,7 @@ public Form2() private void button1_Click(object sender, EventArgs e) { - publicVar.encryptionKey = textBox1.Text; + publicVar.encryptionKey.Set(textBox1.Text); textBox1.Focus(); textBox1.Text = ""; publicVar.okPressed = true; diff --git a/Crypto Notepad/Program.cs b/Crypto Notepad/Program.cs index ef59121..1563cd7 100644 --- a/Crypto Notepad/Program.cs +++ b/Crypto Notepad/Program.cs @@ -19,7 +19,7 @@ static void Main() static class publicVar { - public static string encryptionKey = ""; + public static EncryptedString encryptionKey = new EncryptedString(); public static bool keyChanged = false; public static bool settingsChanged = false; public static bool okPressed = false;