Merge remote-tracking branch 'origin/master'

This commit is contained in:
Alexander 2019-11-06 12:09:14 +02:00
commit 702fa3aee6
4 changed files with 318 additions and 174 deletions

View file

@ -4,6 +4,7 @@
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
namespace Crypto_Notepad namespace Crypto_Notepad
{ {
@ -40,7 +41,8 @@ private bool ReadData(byte[] rawData, int offset, ref byte[] dataOut)
bool foundData = false; bool foundData = false;
// Push data to buffer // Push data to buffer
for (int i = offset; i < rawData.Length; i++) { for (int i = offset; i < rawData.Length; i++)
{
if (rawData[i] == nullTerminator) { foundData = true; break; } if (rawData[i] == nullTerminator) { foundData = true; break; }
else { buffer.Add(rawData[i]); } else { buffer.Add(rawData[i]); }
} }
@ -112,138 +114,145 @@ private static byte[] GenerateSecureNonZeroByteArray(int length)
return result; return result;
} }
public static string Encrypt(string plainText, string password, public static async Task<string> Encrypt(string plainText, string password,
string salt = null, string hashAlgorithm = "SHA1", string salt = null, string hashAlgorithm = "SHA1",
int passwordIterations = 2, int keySize = 256) int passwordIterations = 2, int keySize = 256)
{ {
if (string.IsNullOrEmpty(plainText)) return await Task.Run(() =>
return null; {
if (string.IsNullOrEmpty(password)) if (string.IsNullOrEmpty(plainText))
return null; return null;
if (string.IsNullOrEmpty(password))
return null;
byte[] plainTextBytes; byte[] plainTextBytes;
byte[] saltValueBytes; byte[] saltValueBytes;
// In case user wants a random salt or salt is null/empty for some other reason // In case user wants a random salt or salt is null/empty for some other reason
if (string.IsNullOrEmpty(salt)) if (string.IsNullOrEmpty(salt))
{ {
saltValueBytes = new byte[64]; // Nice and long saltValueBytes = new byte[64]; // Nice and long
saltValueBytes = GenerateSecureNonZeroByteArray(saltValueBytes.Length); saltValueBytes = GenerateSecureNonZeroByteArray(saltValueBytes.Length);
} }
else else
{ {
saltValueBytes = Encoding.ASCII.GetBytes(salt); saltValueBytes = Encoding.ASCII.GetBytes(salt);
} }
plainTextBytes = Encoding.UTF8.GetBytes(plainText); plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes derivedPassword = new PasswordDeriveBytes PasswordDeriveBytes derivedPassword = new PasswordDeriveBytes
(password, saltValueBytes, hashAlgorithm, passwordIterations); (password, saltValueBytes, hashAlgorithm, passwordIterations);
// Null password; adds *some* memory dump protection // Null password; adds *some* memory dump protection
password = null; password = null;
byte[] keyBytes = derivedPassword.GetBytes(keySize / 8); byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged(); RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC; symmetricKey.Mode = CipherMode.CBC;
// Generate IV // Generate IV
symmetricKey.IV = GenerateSecureNonZeroByteArray(symmetricKey.IV.Length); symmetricKey.IV = GenerateSecureNonZeroByteArray(symmetricKey.IV.Length);
byte[] cipherTextBytes = null; byte[] cipherTextBytes = null;
using (MemoryStream memStream = new MemoryStream()) using (MemoryStream memStream = new MemoryStream())
{ {
AESMetadata.WriteMetadata(memStream, symmetricKey.IV, saltValueBytes); AESMetadata.WriteMetadata(memStream, symmetricKey.IV, saltValueBytes);
using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor
(keyBytes, symmetricKey.IV)) (keyBytes, symmetricKey.IV))
{ {
using (CryptoStream cryptoStream = new CryptoStream using (CryptoStream cryptoStream = new CryptoStream
(memStream, encryptor, CryptoStreamMode.Write)) (memStream, encryptor, CryptoStreamMode.Write))
{ {
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock(); cryptoStream.FlushFinalBlock();
cipherTextBytes = memStream.ToArray(); cipherTextBytes = memStream.ToArray();
memStream.Close(); memStream.Close();
cryptoStream.Close(); cryptoStream.Close();
} }
} }
} }
symmetricKey.Dispose(); symmetricKey.Dispose();
derivedPassword.Dispose(); derivedPassword.Dispose();
return Convert.ToBase64String(cipherTextBytes); return Convert.ToBase64String(cipherTextBytes);
});
} }
public static string Decrypt(string cipherText, string password, string salt = "Kosher", public static async Task<string> Decrypt(string cipherText, string password, string salt = "Kosher",
string hashAlgorithm = "SHA1", string hashAlgorithm = "SHA1",
int passwordIterations = 2, int passwordIterations = 2,
int keySize = 256) int keySize = 256)
{ {
if (string.IsNullOrEmpty(cipherText)) return await Task.Run(() =>
return null;
if (string.IsNullOrEmpty(password))
return null;
byte[] initialVectorBytes;
byte[] saltValueBytes;
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
// Extract metadata from file
AESMetadata metadata = new AESMetadata();
if (!metadata.GetMetadata(cipherTextBytes))
{ {
// Metadata parsing error if (string.IsNullOrEmpty(cipherText))
DialogResult result = MessageBox.Show("Unable to parse file metadata.\nAttempt to open anyway?\n(May result in a \'Incorrect Key\' error if the salt is wrong.)", return null;
"Missing or Corrupted Metadata", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk); if (string.IsNullOrEmpty(password))
if (result == DialogResult.Yes) return null;
byte[] initialVectorBytes;
byte[] saltValueBytes;
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
// Extract metadata from file
AESMetadata metadata = new AESMetadata();
if (!metadata.GetMetadata(cipherTextBytes))
{ {
// Default initialization vector from builds v1.1.2 and older // Metadata parsing error
const string default_IV = "16CHARSLONG12345"; DialogResult result = MessageBox.Show("Unable to parse file metadata.\nAttempt to open anyway?\n(May result in a \'Incorrect Key\' error if the salt is wrong.)",
"Missing or Corrupted Metadata", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk);
initialVectorBytes = Encoding.ASCII.GetBytes(default_IV); if (result == DialogResult.Yes)
saltValueBytes = Encoding.ASCII.GetBytes(salt);
}
else { return null; }
}
else
{
saltValueBytes = metadata.Salt;
initialVectorBytes = metadata.InitialVector;
metadata.DeleteMetadataFromBuffer(ref cipherTextBytes);
}
PasswordDeriveBytes derivedPassword = new PasswordDeriveBytes
(password, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int byteCount = 0;
using (MemoryStream memStream = new MemoryStream(cipherTextBytes))
{
using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor
(keyBytes, initialVectorBytes))
{
using (CryptoStream cryptoStream
= new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
{ {
byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); // Default initialization vector from builds v1.1.2 and older
memStream.Close(); const string default_IV = "16CHARSLONG12345";
cryptoStream.Close();
initialVectorBytes = Encoding.ASCII.GetBytes(default_IV);
saltValueBytes = Encoding.ASCII.GetBytes(salt);
} }
else { return null; }
}
else
{
saltValueBytes = metadata.Salt;
initialVectorBytes = metadata.InitialVector;
metadata.DeleteMetadataFromBuffer(ref cipherTextBytes);
} }
symmetricKey.Dispose(); PasswordDeriveBytes derivedPassword = new PasswordDeriveBytes
} (password, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
derivedPassword.Dispose(); RijndaelManaged symmetricKey = new RijndaelManaged();
return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount); symmetricKey.Mode = CipherMode.CBC;
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int byteCount = 0;
using (MemoryStream memStream = new MemoryStream(cipherTextBytes))
{
using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor
(keyBytes, initialVectorBytes))
{
using (CryptoStream cryptoStream
= new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
{
byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memStream.Close();
cryptoStream.Close();
}
}
symmetricKey.Dispose();
}
derivedPassword.Dispose();
return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount);
});
} }
} }
} }

View file

@ -99,5 +99,24 @@ private static Rectangle GetFormattingRect(this TextBoxBase textbox)
return rect; return rect;
} }
} }
public static class RichTextBoxExtensions
{
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
private const int WM_SETREDRAW = 0x0b;
public static void SuspendDrawing(this RichTextBox richTextBox)
{
SendMessage(richTextBox.Handle, WM_SETREDRAW, (IntPtr)0, IntPtr.Zero);
}
public static void ResumeDrawing(this RichTextBox richTextBox)
{
SendMessage(richTextBox.Handle, WM_SETREDRAW, (IntPtr)1, IntPtr.Zero);
richTextBox.Invalidate();
}
}
} }

View file

@ -66,17 +66,21 @@ static string SizeSuffix(long value)
return string.Format("{0:n1} {1}", dValue, SizeSuffixes[i]); return string.Format("{0:n1} {1}", dValue, SizeSuffixes[i]);
} }
private void DecryptAES() private async Task DecryptAES()
{ {
EnterKeyForm enterKeyForm = new EnterKeyForm EnterKeyForm enterKeyForm = new EnterKeyForm();
{ enterKeyForm.Owner = this;
Owner = this
};
enterKeyForm.ShowDialog(); enterKeyForm.ShowDialog();
richTextBox.SuspendDrawing();
UseWaitCursor = true;
if (!PublicVar.okPressed) if (!PublicVar.okPressed)
{ {
PublicVar.openFileName = Path.GetFileName(filePath); PublicVar.openFileName = Path.GetFileName(filePath);
mainMenu.Enabled = true;
toolbarPanel.Enabled = true;
richTextBox.ReadOnly = false;
UseWaitCursor = false;
richTextBox.ResumeDrawing();
return; return;
} }
if (searchPanel.Visible) if (searchPanel.Visible)
@ -85,38 +89,65 @@ private void DecryptAES()
} }
try try
{ {
string opnfile = File.ReadAllText(openFileDialog.FileName); using (StreamReader reader = File.OpenText(openFileDialog.FileName))
{
mainMenu.Enabled = false;
toolbarPanel.Enabled = false;
richTextBox.ReadOnly = true;
string openedFileText = await reader.ReadToEndAsync();
richTextBox.Text = await AES.Decrypt(openedFileText, TypedPassword.Value, null, settings.HashAlgorithm,
Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); ;
}
string NameWithotPath = Path.GetFileName(openFileDialog.FileName); string NameWithotPath = Path.GetFileName(openFileDialog.FileName);
string de;
de = AES.Decrypt(opnfile, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize));
richTextBox.Text = de;
Text = PublicVar.appName + " " + NameWithotPath; Text = PublicVar.appName + " " + NameWithotPath;
filePath = openFileDialog.FileName; filePath = openFileDialog.FileName;
PublicVar.openFileName = Path.GetFileName(openFileDialog.FileName); PublicVar.openFileName = Path.GetFileName(openFileDialog.FileName);
PublicVar.encryptionKey.Set(TypedPassword.Value); PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null; TypedPassword.Value = null;
StatusPanelFileInfo(); StatusPanelFileInfo();
mainMenu.Enabled = true;
toolbarPanel.Enabled = true;
richTextBox.ReadOnly = false;
UseWaitCursor = false;
richTextBox.ResumeDrawing();
} }
catch (CryptographicException) catch (Exception ex)
{ {
using (new CenterWinDialog(this)) if (ex is FormatException | ex is CryptographicException)
{ {
TypedPassword.Value = null; PublicVar.okPressed = false;
DialogResult dialogResult = MessageBox.Show(this, "Invalid key!", PublicVar.appName, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); if (Visible)
if (dialogResult == DialogResult.Retry)
{ {
DecryptAES();
PublicVar.messageBoxCenterParent = true; PublicVar.messageBoxCenterParent = true;
} }
if (dialogResult == DialogResult.Cancel) using (new CenterWinDialog(this))
{ {
PublicVar.openFileName = Path.GetFileName(filePath); TypedPassword.Value = null;
DialogResult dialogResult = MessageBox.Show(this, "Invalid key!", PublicVar.appName, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry)
{
await DecryptAES();
}
if (dialogResult == DialogResult.Cancel)
{
PublicVar.openFileName = Path.GetFileName(filePath);
mainMenu.Enabled = true;
toolbarPanel.Enabled = true;
richTextBox.ReadOnly = false;
UseWaitCursor = false;
richTextBox.ResumeDrawing();
if (!Visible)
{
Application.Exit();
}
}
} }
} }
} }
} }
private void OpenAsotiations() private async void OpenAsotiations()
{ {
EnterKeyForm enterKeyForm = new EnterKeyForm EnterKeyForm enterKeyForm = new EnterKeyForm
{ {
@ -128,7 +159,8 @@ private void OpenAsotiations()
openFileDialog.FileName = Path.GetFullPath(args[1]); openFileDialog.FileName = Path.GetFullPath(args[1]);
if (fileExtension != ".cnp") if (fileExtension != ".cnp")
{ {
DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName,
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.No) if (res == DialogResult.No)
{ {
string opnfile = File.ReadAllText(args[1]); string opnfile = File.ReadAllText(args[1]);
@ -140,10 +172,10 @@ private void OpenAsotiations()
return; return;
} }
} }
DecryptAES(); await DecryptAES();
} }
private void SendTo() private async void SendTo()
{ {
EnterKeyForm enterKeyForm = new EnterKeyForm EnterKeyForm enterKeyForm = new EnterKeyForm
{ {
@ -155,7 +187,8 @@ private void SendTo()
PublicVar.openFileName = Path.GetFileName(argsPath); PublicVar.openFileName = Path.GetFileName(argsPath);
if (fileExtension != ".cnp") if (fileExtension != ".cnp")
{ {
DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName,
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.No) if (res == DialogResult.No)
{ {
string opnfile = File.ReadAllText(argsPath); string opnfile = File.ReadAllText(argsPath);
@ -167,41 +200,40 @@ private void SendTo()
return; return;
} }
} }
DecryptAES(); await DecryptAES();
} }
private void ContextMenuEncryptReplace() private async void ContextMenuEncryptReplace()
{ {
DialogResult res = MessageBox.Show(this, "This action will delete the source file and replace it with encrypted version", PublicVar.appName, MessageBoxButtons.OKCancel, MessageBoxIcon.Question); 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) if (res == DialogResult.Cancel)
{ {
Environment.Exit(0); Environment.Exit(0);
} }
string opnfile = File.ReadAllText(args[1]); richTextBox.Text = File.ReadAllText(args[1]);
richTextBox.Text = opnfile;
PublicVar.openFileName = Path.GetFileName(args[1]); PublicVar.openFileName = Path.GetFileName(args[1]);
string newFile = Path.GetDirectoryName(args[1]) + @"\" + Path.GetFileNameWithoutExtension(args[1]) + ".cnp"; string newFileName = Path.GetDirectoryName(args[1]) + @"\" + Path.GetFileNameWithoutExtension(args[1]) + ".cnp";
EnterKeyForm enterKeyForm = new EnterKeyForm EnterKeyForm enterKeyForm = new EnterKeyForm
{ {
Owner = this Owner = this
}; };
enterKeyForm.ShowDialog(); enterKeyForm.ShowDialog();
File.Delete(args[1]); File.Delete(args[1]);
string noenc = richTextBox.Text; string unencryptedText = richTextBox.Text;
string en; string encryptedText = await AES.Encrypt(richTextBox.Text, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations),
en = AES.Encrypt(richTextBox.Text, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); Convert.ToInt32(settings.KeySize));
richTextBox.Text = en; using (StreamWriter writer = new StreamWriter(newFileName))
using (StreamWriter writer = new StreamWriter(newFile))
{ {
writer.Write(richTextBox.Text); writer.Write(encryptedText);
writer.Close(); writer.Close();
} }
PublicVar.encryptionKey.Set(TypedPassword.Value); PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null; TypedPassword.Value = null;
filePath = newFile; filePath = newFileName;
PublicVar.openFileName = Path.GetFileName(newFile); PublicVar.openFileName = Path.GetFileName(newFileName);
Text = PublicVar.appName + " " + PublicVar.openFileName; Text = PublicVar.appName + " " + PublicVar.openFileName;
richTextBox.Text = noenc; richTextBox.Text = unencryptedText;
StatusPanelFileInfo(); StatusPanelFileInfo();
richTextBox.Modified = false; richTextBox.Modified = false;
} }
@ -243,6 +275,10 @@ private void SaveConfirm()
{ {
messageBoxText = "Save file: " + "\"" + PublicVar.openFileName + "\"" + " with a new key? "; messageBoxText = "Save file: " + "\"" + PublicVar.openFileName + "\"" + " with a new key? ";
} }
if (Visible)
{
PublicVar.messageBoxCenterParent = true;
}
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
{ {
DialogResult res = MessageBox.Show(this, messageBoxText, PublicVar.appName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); DialogResult res = MessageBox.Show(this, messageBoxText, PublicVar.appName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
@ -280,7 +316,8 @@ private void CheckForUpdates(bool autoCheck)
{ {
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
{ {
DialogResult res = MessageBox.Show(this, "New version is available. Install it now?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); DialogResult res = MessageBox.Show(this, "New version is available. Install it now?", PublicVar.appName,
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.Yes) if (res == DialogResult.Yes)
{ {
File.WriteAllBytes(exePath + "Ionic.Zip.dll", Resources.Ionic_Zip); File.WriteAllBytes(exePath + "Ionic.Zip.dll", Resources.Ionic_Zip);
@ -324,7 +361,8 @@ private void CheckForUpdates(bool autoCheck)
} }
else else
{ {
MessageBox.Show(this, "Checking for updates failed:\nConnection lost or the server is busy.", PublicVar.appName, MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show(this, "Checking for updates failed:\nConnection lost or the server is busy.", PublicVar.appName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
}); });
@ -411,20 +449,33 @@ private void StatusPanelTimer_Tick(object sender, EventArgs e)
statusPanelTimer.Stop(); statusPanelTimer.Stop();
} }
private void UnlockFile() private async Task UnlockFile()
{ {
try try
{ {
richTextBox.SuspendDrawing();
UseWaitCursor = true;
fileLockedPanel.Enabled = false;
TypedPassword.Value = fileLockedKeyTextBox.Text; TypedPassword.Value = fileLockedKeyTextBox.Text;
string opnfile = File.ReadAllText(filePath); mainMenu.Enabled = false;
string de = AES.Decrypt(opnfile, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); toolbarPanel.Enabled = false;
using (StreamReader reader = File.OpenText(openFileDialog.FileName))
{
string openedFileText = await reader.ReadToEndAsync();
richTextBox.Text = await AES.Decrypt(openedFileText, TypedPassword.Value, null, settings.HashAlgorithm,
Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); ;
}
fileLockedPanel.Visible = false; fileLockedPanel.Visible = false;
richTextBox.Text = de;
richTextBox.SelectionStart = caretPos; richTextBox.SelectionStart = caretPos;
PublicVar.encryptionKey.Set(TypedPassword.Value); PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null; TypedPassword.Value = null;
richTextBox.Focus(); richTextBox.Focus();
StatusPanelFileInfo(); StatusPanelFileInfo();
richTextBox.ResumeDrawing();
UseWaitCursor = false;
fileLockedPanel.Enabled = true;
mainMenu.Enabled = true;
toolbarPanel.Enabled = true;
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -437,6 +488,9 @@ private void UnlockFile()
DialogResult dialogResult = MessageBox.Show(this, "Invalid key!", PublicVar.appName, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); DialogResult dialogResult = MessageBox.Show(this, "Invalid key!", PublicVar.appName, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry) if (dialogResult == DialogResult.Retry)
{ {
UseWaitCursor = false;
richTextBox.ResumeDrawing();
fileLockedPanel.Enabled = true;
fileLockedKeyTextBox.Text = ""; fileLockedKeyTextBox.Text = "";
fileLockedKeyTextBox.Focus(); fileLockedKeyTextBox.Focus();
} }
@ -446,6 +500,9 @@ private void UnlockFile()
Text = PublicVar.appName; Text = PublicVar.appName;
filePath = ""; filePath = "";
PublicVar.openFileName = ""; PublicVar.openFileName = "";
UseWaitCursor = false;
richTextBox.ResumeDrawing();
fileLockedPanel.Enabled = true;
} }
} }
} }
@ -705,8 +762,15 @@ private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
DialogResult res = MessageBox.Show(this, messageBoxText, PublicVar.appName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); DialogResult res = MessageBox.Show(this, messageBoxText, PublicVar.appName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (res == DialogResult.Yes) if (res == DialogResult.Yes)
{ {
Hide();
trayIcon.Visible = false; trayIcon.Visible = false;
SaveMainMenu_Click(this, new EventArgs()); using (StreamWriter writer = new StreamWriter(filePath))
{
string enc = "";
Task.Run(async () => { enc = await AES.Encrypt(richTextBox.Text, PublicVar.encryptionKey.Get(), null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); }).Wait();
writer.Write(enc);
writer.Close();
}
} }
if (res == DialogResult.Cancel) if (res == DialogResult.Cancel)
{ {
@ -725,6 +789,10 @@ private void MainForm_Shown(object sender, EventArgs e)
if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Crypto Notepad.settings")) if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Crypto Notepad.settings"))
{ {
if (Visible)
{
PublicVar.messageBoxCenterParent = true;
}
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
{ {
DialogResult res = MessageBox.Show(this, "Enable automatic update check?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); DialogResult res = MessageBox.Show(this, "Enable automatic update check?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
@ -819,7 +887,7 @@ private void RichTextBox_LinkClicked(object sender, LinkClickedEventArgs e)
} }
} }
private void RichTextBox_DragDrop(object sender, DragEventArgs e) private async void RichTextBox_DragDrop(object sender, DragEventArgs e)
{ {
SaveConfirm(); SaveConfirm();
if (cancelPressed) if (cancelPressed)
@ -843,7 +911,8 @@ private void RichTextBox_DragDrop(object sender, DragEventArgs e)
} }
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
{ {
DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName,
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.No) if (res == DialogResult.No)
{ {
string opnfile = File.ReadAllText(openFileDialog.FileName); string opnfile = File.ReadAllText(openFileDialog.FileName);
@ -856,7 +925,7 @@ private void RichTextBox_DragDrop(object sender, DragEventArgs e)
} }
} }
} }
DecryptAES(); await DecryptAES();
} }
} }
} }
@ -908,7 +977,8 @@ private void StatusPanelLabel_Click(object sender, EventArgs e)
string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\"; string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\";
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
{ {
DialogResult res = MessageBox.Show(this, "New version is available. Install it now?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); DialogResult res = MessageBox.Show(this, "New version is available. Install it now?", PublicVar.appName,
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.Yes) if (res == DialogResult.Yes)
{ {
File.WriteAllBytes(exePath + "Ionic.Zip.dll", Resources.Ionic_Zip); File.WriteAllBytes(exePath + "Ionic.Zip.dll", Resources.Ionic_Zip);
@ -971,7 +1041,7 @@ private void NewMainMenu_Click(object sender, EventArgs e)
TypedPassword.Value = null; TypedPassword.Value = null;
} }
private void OpenMainMenu_Click(object sender, EventArgs e) private async void OpenMainMenu_Click(object sender, EventArgs e)
{ {
SaveConfirm(); SaveConfirm();
if (cancelPressed) if (cancelPressed)
@ -985,9 +1055,14 @@ private void OpenMainMenu_Click(object sender, EventArgs e)
PublicVar.openFileName = Path.GetFileName(openFileDialog.FileName); PublicVar.openFileName = Path.GetFileName(openFileDialog.FileName);
if (!openFileDialog.FileName.Contains(".cnp")) if (!openFileDialog.FileName.Contains(".cnp"))
{ {
if (Visible)
{
PublicVar.messageBoxCenterParent = true;
}
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
{ {
DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information); DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName,
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.No) if (res == DialogResult.No)
{ {
string opnfile = File.ReadAllText(openFileDialog.FileName); string opnfile = File.ReadAllText(openFileDialog.FileName);
@ -1000,31 +1075,41 @@ private void OpenMainMenu_Click(object sender, EventArgs e)
} }
} }
} }
DecryptAES(); await DecryptAES();
richTextBox.Modified = false; richTextBox.Modified = false;
} }
} }
private void SaveMainMenu_Click(object sender, EventArgs e) private async void SaveMainMenu_Click(object sender, EventArgs e)
{ {
if (string.IsNullOrEmpty(PublicVar.encryptionKey.Get())) if (string.IsNullOrEmpty(PublicVar.encryptionKey.Get()))
{ {
SaveAsMainMenu_Click(this, new EventArgs()); SaveAsMainMenu_Click(this, new EventArgs());
return; return;
} }
string enc = AES.Encrypt(richTextBox.Text, PublicVar.encryptionKey.Get(), null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); mainMenu.Enabled = false;
toolbarPanel.Enabled = false;
richTextBox.ReadOnly = true;
richTextBox.SuspendDrawing();
UseWaitCursor = true;
using (StreamWriter writer = new StreamWriter(filePath)) using (StreamWriter writer = new StreamWriter(filePath))
{ {
writer.Write(enc); writer.Write(await AES.Encrypt(richTextBox.Text, PublicVar.encryptionKey.Get(), null, settings.HashAlgorithm,
Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)));
writer.Close(); writer.Close();
} }
richTextBox.Modified = false; richTextBox.Modified = false;
PublicVar.keyChanged = false; PublicVar.keyChanged = false;
StatusPanelMessage("save"); StatusPanelMessage("save");
StatusPanelFileInfo(); StatusPanelFileInfo();
richTextBox.ResumeDrawing();
UseWaitCursor = false;
mainMenu.Enabled = true;
toolbarPanel.Enabled = true;
richTextBox.ReadOnly = false;
} }
private void SaveAsMainMenu_Click(object sender, EventArgs e) private async void SaveAsMainMenu_Click(object sender, EventArgs e)
{ {
if (!string.IsNullOrEmpty(filePath)) if (!string.IsNullOrEmpty(filePath))
{ {
@ -1058,7 +1143,8 @@ private void SaveAsMainMenu_Click(object sender, EventArgs e)
TypedPassword.Value = PublicVar.encryptionKey.Get(); TypedPassword.Value = PublicVar.encryptionKey.Get();
} }
filePath = saveFileDialog.FileName; filePath = saveFileDialog.FileName;
string enc = AES.Encrypt(richTextBox.Text, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)); string enc = await AES.Encrypt(richTextBox.Text, TypedPassword.Value, null, settings.HashAlgorithm,
Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize));
using (StreamWriter writer = new StreamWriter(filePath)) using (StreamWriter writer = new StreamWriter(filePath))
{ {
writer.Write(enc); writer.Write(enc);
@ -1075,11 +1161,28 @@ private void SaveAsMainMenu_Click(object sender, EventArgs e)
private async void SaveCloseFileMainMenu_Click(object sender, EventArgs e) private async void SaveCloseFileMainMenu_Click(object sender, EventArgs e)
{ {
//using (StreamWriter writer = new StreamWriter(filePath))
//{
// writer.Write(AES.Encrypt(richTextBox.Text, PublicVar.encryptionKey.Get(), null, settings.HashAlgorithm,
// Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)));
// writer.Close();
//}
mainMenu.Enabled = false;
toolbarPanel.Enabled = false;
richTextBox.SuspendDrawing();
UseWaitCursor = true;
using (StreamWriter writer = new StreamWriter(filePath)) using (StreamWriter writer = new StreamWriter(filePath))
{ {
writer.Write(AES.Encrypt(richTextBox.Text, PublicVar.encryptionKey.Get(), null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize))); writer.Write(await AES.Encrypt(richTextBox.Text, PublicVar.encryptionKey.Get(), null, settings.HashAlgorithm,
Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)));
writer.Close(); writer.Close();
} }
richTextBox.ResumeDrawing();
UseWaitCursor = false;
mainMenu.Enabled = true;
toolbarPanel.Enabled = true;
richTextBox.ReadOnly = false;
PublicVar.encryptionKey.Set(null); PublicVar.encryptionKey.Set(null);
richTextBox.Clear(); richTextBox.Clear();
PublicVar.openFileName = ""; PublicVar.openFileName = "";
@ -1101,7 +1204,8 @@ private void DeleteFileToolStripMenuItem_Click(object sender, EventArgs e)
{ {
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
{ {
if (MessageBox.Show(this, "Delete file: " + "\"" + filePath + "\"" + " ?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) if (MessageBox.Show(this, "Delete file: " + "\"" + filePath + "\"" + " ?", PublicVar.appName,
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ {
File.Delete(filePath); File.Delete(filePath);
richTextBox.Clear(); richTextBox.Clear();
@ -1289,9 +1393,21 @@ private void ChangeKeyMainMenu_Click(object sender, EventArgs e)
changeKeyForm.ShowDialog(this); changeKeyForm.ShowDialog(this);
} }
private void LockMainMenu_Click(object sender, EventArgs e) private async void LockMainMenu_Click(object sender, EventArgs e)
{ {
SaveMainMenu_Click(this, new EventArgs()); //SaveMainMenu_Click(this, new EventArgs());
mainMenu.Enabled = false;
toolbarPanel.Enabled = false;
richTextBox.SuspendDrawing();
UseWaitCursor = true;
using (StreamWriter writer = new StreamWriter(filePath))
{
writer.Write(await AES.Encrypt(richTextBox.Text, PublicVar.encryptionKey.Get(), null, settings.HashAlgorithm,
Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize)));
writer.Close();
}
richTextBox.ResumeDrawing();
UseWaitCursor = false;
fileLockedPanel.Visible = true; fileLockedPanel.Visible = true;
} }
@ -1697,9 +1813,9 @@ private void FileLockedPanel_VisibleChanged(object sender, EventArgs e)
} }
} }
private void FileLockedOkButton_Click(object sender, EventArgs e) private async void FileLockedOkButton_Click(object sender, EventArgs e)
{ {
UnlockFile(); await UnlockFile();
} }
private void FileLockedCloseButton_MouseClick(object sender, MouseEventArgs e) private void FileLockedCloseButton_MouseClick(object sender, MouseEventArgs e)

View file

@ -121,7 +121,7 @@
<value>17, 17</value> <value>17, 17</value>
</metadata> </metadata>
<metadata name="contextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="contextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>676, 17</value> <value>533, 17</value>
</metadata> </metadata>
<metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="openFileDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>127, 17</value> <value>127, 17</value>
@ -146,7 +146,7 @@
</value> </value>
</data> </data>
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>1112, 17</value> <value>969, 17</value>
</metadata> </metadata>
<data name="newToolbarButton.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="newToolbarButton.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>
@ -290,13 +290,13 @@
</value> </value>
</data> </data>
<metadata name="statusPanel.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="statusPanel.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>904, 17</value> <value>761, 17</value>
</metadata> </metadata>
<metadata name="trayIcon.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="trayIcon.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>1017, 17</value> <value>874, 17</value>
</metadata> </metadata>
<metadata name="trayMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="trayMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>800, 17</value> <value>657, 17</value>
</metadata> </metadata>
<data name="trayIcon.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="trayIcon.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>