Added EncryptedString class to encrypt key in memory.

This commit is contained in:
h5p9sl 2018-11-30 19:29:19 -07:00
parent 834c91d5af
commit c9bd9f436d
6 changed files with 67 additions and 20 deletions

View file

@ -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!";

View file

@ -105,6 +105,7 @@
<Compile Include="CustomRichTextBox.Designer.cs">
<DependentUpon>CustomRichTextBox.cs</DependentUpon>
</Compile>
<Compile Include="EncryptedString.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>

View file

@ -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
{
/// <summary>
/// Stores a string encrypted in memory to defend against memory dumps
/// </summary>
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);
}
}
}

View file

@ -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;

View file

@ -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;

View file

@ -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;