Crypto-Notepad/Crypto Notepad/MainForm.cs

1397 lines
47 KiB
C#
Raw Normal View History

using IWshRuntimeLibrary;
2018-12-17 11:16:17 +00:00
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Threading;
using System.Windows.Forms;
using File = System.IO.File;
namespace Crypto_Notepad
{
public partial class MainForm : Form
{
Properties.Settings ps = Properties.Settings.Default;
string filePath = "";
string[] args = Environment.GetCommandLineArgs();
int caretPos = 0;
string appName = Assembly.GetExecutingAssembly().GetName().Name + " ";
string currentFilename = "";
bool shiftPresed;
bool cancelPressed = false;
bool noExit = false;
string argsPath = "";
2018-12-17 11:16:17 +00:00
public MainForm()
{
InitializeComponent();
CustomRTB.DragDrop += new DragEventHandler(CustomRTB_DragDrop);
CustomRTB.AllowDrop = true;
2018-12-17 11:16:17 +00:00
}
/*Functions*/
private void DecryptAES()
2018-12-17 11:16:17 +00:00
{
EnterKeyForm f2 = new EnterKeyForm();
f2.ShowDialog();
if (!PublicVar.okPressed)
2018-12-17 11:16:17 +00:00
{
return;
}
if (SearchPanel.Visible)
2018-12-17 11:16:17 +00:00
{
FindToolStripMenuItem_Click(this, new EventArgs());
2018-12-17 11:16:17 +00:00
}
try
{
string opnfile = File.ReadAllText(OpenFile.FileName);
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
2018-12-19 22:56:59 +00:00
string de = AES.Decrypt(opnfile, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
CustomRTB.Text = de;
2018-12-17 11:16:17 +00:00
Text = appName + NameWithotPath;
2018-12-17 11:16:17 +00:00
filePath = OpenFile.FileName;
string cc2 = CustomRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
CustomRTB.Select(Convert.ToInt32(cc2), 0);
2018-12-17 11:16:17 +00:00
PublicVar.openFileName = Path.GetFileName(OpenFile.FileName);
currentFilename = Path.GetFileName(OpenFile.FileName);
PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null;
}
catch (CryptographicException)
{
using (new CenterWinDialog(this))
{
TypedPassword.Value = null;
DialogResult dialogResult = MessageBox.Show("Invalid key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry)
{
DecryptAES();
}
if (dialogResult == DialogResult.Cancel)
{
return;
}
}
}
}
private void OpenAsotiations()
2018-12-17 11:16:17 +00:00
{
EnterKeyForm Form2 = new EnterKeyForm();
Form2.StartPosition = FormStartPosition.CenterScreen;
string fileExtension = Path.GetExtension(args[1]);
if (fileExtension == ".cnp")
{
try
{
string NameWithotPath = Path.GetFileName(args[1]);
string opnfile = File.ReadAllText(args[1]);
Form2.ShowDialog();
if (!PublicVar.okPressed)
2018-12-17 11:16:17 +00:00
{
OpenFile.FileName = "";
return;
}
PublicVar.okPressed = false;
2018-12-19 22:56:59 +00:00
string de = AES.Decrypt(opnfile, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
CustomRTB.Text = de;
2018-12-17 11:16:17 +00:00
Text = appName + NameWithotPath;
2018-12-17 11:16:17 +00:00
filePath = args[1];
string cc = CustomRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
2018-12-17 11:16:17 +00:00
PublicVar.encryptionKey.Set(TypedPassword.Value);
CustomRTB.Select(Convert.ToInt32(cc), 0);
2018-12-17 11:16:17 +00:00
TypedPassword.Value = null;
}
catch (CryptographicException)
{
TypedPassword.Value = null;
DialogResult dialogResult = MessageBox.Show("Invalid key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry)
{
OpenAsotiations();
2018-12-17 11:16:17 +00:00
}
}
}
else
{
string opnfile = File.ReadAllText(args[1]);
string NameWithotPath = Path.GetFileName(args[1]);
CustomRTB.Text = opnfile;
Text = appName + NameWithotPath;
string cc2 = CustomRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
CustomRTB.Select(Convert.ToInt32(cc2), 0);
2018-12-17 11:16:17 +00:00
}
currentFilename = Path.GetFileName(args[1]);
}
private void SendTo()
2018-12-17 11:16:17 +00:00
{
EnterKeyForm f2 = new EnterKeyForm();
f2.StartPosition = FormStartPosition.CenterScreen;
string fileExtension = Path.GetExtension(argsPath);
if (fileExtension == ".cnp")
{
try
{
string NameWithotPath = Path.GetFileName(argsPath);
string opnfile = File.ReadAllText(argsPath);
f2.ShowDialog();
if (!PublicVar.okPressed)
2018-12-17 11:16:17 +00:00
{
OpenFile.FileName = "";
return;
}
PublicVar.okPressed = false;
2018-12-19 22:56:59 +00:00
string de = AES.Decrypt(opnfile, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
CustomRTB.Text = de;
2018-12-17 11:16:17 +00:00
Text = appName + NameWithotPath;
2018-12-17 11:16:17 +00:00
filePath = argsPath;
string cc = CustomRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
CustomRTB.Select(Convert.ToInt32(cc), 0);
2018-12-17 11:16:17 +00:00
PublicVar.encryptionKey.Set(TypedPassword.Value);
currentFilename = Path.GetFileName(argsPath);
TypedPassword.Value = null;
}
catch (CryptographicException)
{
TypedPassword.Value = null;
DialogResult dialogResult = MessageBox.Show("Invalid key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry)
{
SendTo();
2018-12-17 11:16:17 +00:00
}
}
}
else
{
string opnfile = File.ReadAllText(argsPath);
string NameWithotPath = Path.GetFileName(argsPath);
CustomRTB.Text = opnfile;
Text = appName + NameWithotPath;
string cc2 = CustomRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
CustomRTB.Select(Convert.ToInt32(cc2), 0);
2018-12-17 11:16:17 +00:00
}
}
private void SendToShortcut()
2018-12-17 11:16:17 +00:00
{
string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Windows\SendTo";
string shortcutName = "Crypto Notepad" + ".lnk";
string shortcutLocation = Path.Combine(shortcutPath, shortcutName);
string targetFileLocation = Assembly.GetEntryAssembly().Location;
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = "Crypto Notepad";
shortcut.IconLocation = targetFileLocation;
shortcut.TargetPath = targetFileLocation;
shortcut.Arguments = "/s";
shortcut.Save();
2018-12-17 11:16:17 +00:00
}
private void ContextMenuEncryptReplace()
2018-12-17 11:16:17 +00:00
{
PublicVar.openFileName = Path.GetFileName(args[1]);
if (args[1].Contains(".cnp"))
{
MessageBox.Show("Looks like this file is already encrypted", "Crypto Notepad", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
DialogResult res = MessageBox.Show("This action will delete the source file and replace it with encrypted version", "Crypto Notepad", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
2018-12-17 11:16:17 +00:00
if (res == DialogResult.Cancel)
{
2018-12-17 13:03:08 +00:00
Environment.Exit(0);
2018-12-17 11:16:17 +00:00
}
if (!args[1].Contains(".cnp"))
{
string opnfile = File.ReadAllText(args[1]);
CustomRTB.Text = opnfile;
string cc2 = CustomRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
CustomRTB.Select(Convert.ToInt32(cc2), 0);
2018-12-17 11:16:17 +00:00
currentFilename = Path.GetFileName(args[1]);
string newFile = Path.GetDirectoryName(args[1]) + @"\" + Path.GetFileNameWithoutExtension(args[1]) + ".cnp";
EnterKeyForm f2 = new EnterKeyForm();
f2.ShowDialog();
if (!PublicVar.okPressed)
2018-12-17 11:16:17 +00:00
{
Application.Exit();
}
PublicVar.okPressed = false;
File.Delete(args[1]);
string noenc = CustomRTB.Text;
2018-12-17 11:16:17 +00:00
string en;
en = AES.Encrypt(CustomRTB.Text, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
CustomRTB.Text = en;
2018-12-17 11:16:17 +00:00
StreamWriter sw = new StreamWriter(newFile);
int i = CustomRTB.Lines.Count();
2018-12-17 11:16:17 +00:00
int j = 0;
i = i - 1;
while (j <= i)
{
sw.WriteLine(CustomRTB.Lines.GetValue(j).ToString());
2018-12-17 11:16:17 +00:00
j = j + 1;
}
sw.Close();
PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null;
filePath = newFile;
currentFilename = Path.GetFileName(newFile);
Text = appName + currentFilename;
CustomRTB.Text = noenc;
2018-12-17 11:16:17 +00:00
}
#region workaround, strange behavior with the cursor in customRTB fix
CustomRTB.DetectUrls = false;
CustomRTB.DetectUrls = true;
CustomRTB.Modified = false;
2018-12-17 11:16:17 +00:00
#endregion
if (PublicVar.okPressed)
2018-12-17 11:16:17 +00:00
{
PublicVar.okPressed = false;
}
}
private void ContextMenuEncrypt()
2018-12-17 11:16:17 +00:00
{
PublicVar.openFileName = Path.GetFileName(args[1]);
if (!args[1].Contains(".cnp"))
{
string opnfile = File.ReadAllText(args[1]);
string NameWithotPath = Path.GetFileName(args[1]);
CustomRTB.Text = opnfile;
Text = appName + NameWithotPath;
string cc2 = CustomRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
CustomRTB.Select(Convert.ToInt32(cc2), 0);
2018-12-17 11:16:17 +00:00
currentFilename = Path.GetFileName(args[1]);
filePath = OpenFile.FileName;
}
#region workaround, strange behavior with the cursor in customRTB fix
CustomRTB.DetectUrls = false;
CustomRTB.DetectUrls = true;
2018-12-17 11:16:17 +00:00
#endregion
CustomRTB.Modified = false;
if (PublicVar.okPressed)
2018-12-17 11:16:17 +00:00
{
PublicVar.okPressed = false;
}
}
private void DeleteUpdateFiles()
2018-12-17 11:16:17 +00:00
{
string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\";
string UpdaterExe = exePath + "Updater.exe";
string UpdateZip = exePath + "Crypto-Notepad-Update.zip";
string ZipDll = exePath + "Ionic.Zip.dll";
if (File.Exists(UpdaterExe))
{
File.Delete(UpdaterExe);
}
if (File.Exists(UpdateZip))
{
File.Delete(UpdateZip);
}
if (File.Exists(ZipDll))
{
File.Delete(ZipDll);
}
}
private void SaveConfirm(bool exit)
2018-12-17 11:16:17 +00:00
{
if (!CustomRTB.Modified)
2018-12-17 11:16:17 +00:00
{
if (exit)
2018-12-17 11:16:17 +00:00
{
Environment.Exit(0);
2018-12-17 11:16:17 +00:00
}
}
else
{
if (currentFilename == "")
{
currentFilename = "Unnamed.cnp";
}
2018-12-17 11:16:17 +00:00
if (CustomRTB.Text != "")
{
string messageBoxText = "";
2018-12-17 11:16:17 +00:00
if (!PublicVar.keyChanged)
{
messageBoxText = "Save file: " + "\"" + currentFilename + "\"" + " ? ";
}
else
{
messageBoxText = "Save file: " + "\"" + currentFilename + "\"" + " with a new key? ";
}
2018-12-19 22:56:59 +00:00
using (new CenterWinDialog(this))
{
DialogResult res = MessageBox.Show(messageBoxText, "Crypto Notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (res == DialogResult.Yes)
{
SaveToolStripMenuItem_Click(this, new EventArgs());
if (exit)
{
Environment.Exit(0);
}
2018-12-17 11:16:17 +00:00
}
2018-12-17 11:16:17 +00:00
if (res == DialogResult.No)
{
if (exit)
{
Environment.Exit(0);
}
}
2018-12-17 11:16:17 +00:00
if (res == DialogResult.Cancel)
{
noExit = true;
cancelPressed = true;
return;
}
}
}
2018-12-17 11:16:17 +00:00
}
}
private void CheckForUpdates(bool autoCheck)
2018-12-17 11:16:17 +00:00
{
try
2018-12-17 11:16:17 +00:00
{
WebClient client = new WebClient();
Stream stream = client.OpenRead("https://raw.githubusercontent.com/Sigmanor/Crypto-Notepad/master/version.txt");
StreamReader reader = new StreamReader(stream);
string content = reader.ReadToEnd();
string version = Application.ProductVersion;
string exePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"\";
2018-12-17 11:16:17 +00:00
int appVersion = Convert.ToInt32(version.Replace(".", "")), serverVersion = Convert.ToInt32(content.Replace(".", ""));
2018-12-17 11:16:17 +00:00
if (serverVersion > appVersion)
2018-12-17 11:16:17 +00:00
{
MainMenu.Invoke((Action)delegate
2018-12-17 11:16:17 +00:00
{
using (new CenterWinDialog(this))
{
DialogResult res = MessageBox.Show("New version is available. Install it now?", "Update", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.Yes)
{
File.WriteAllBytes(exePath + "Ionic.Zip.dll", Properties.Resources.Ionic_Zip);
File.WriteAllBytes(exePath + "Updater.exe", Properties.Resources.Updater);
var pr = new Process();
pr.StartInfo.FileName = exePath + "Updater.exe";
pr.StartInfo.Arguments = "/u";
pr.Start();
Application.Exit();
}
}
});
2018-12-17 11:16:17 +00:00
}
if (serverVersion <= appVersion && autoCheck)
{
MainMenu.Invoke((Action)delegate
{
using (new CenterWinDialog(this))
{
MessageBox.Show("Crypto Notepad is up to date.", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
});
}
2018-12-17 11:16:17 +00:00
}
catch
2018-12-17 11:16:17 +00:00
{
return;
2018-12-17 11:16:17 +00:00
}
}
private void AutoLock(bool minimize)
2018-12-17 11:16:17 +00:00
{
EnterKeyForm f2 = new EnterKeyForm();
PublicVar.encryptionKey.Set(null);
caretPos = CustomRTB.SelectionStart;
f2.MinimizeBox = true;
Hide();
2018-12-17 11:16:17 +00:00
if (minimize)
2018-12-17 11:16:17 +00:00
{
f2.WindowState = FormWindowState.Minimized;
2018-12-17 11:16:17 +00:00
}
f2.ShowDialog();
2018-12-17 11:16:17 +00:00
if (!PublicVar.okPressed)
2018-12-17 11:16:17 +00:00
{
PublicVar.encryptionKey.Set(null);
CustomRTB.Clear();
Text = appName.Remove(14);
currentFilename = "";
filePath = "";
Show();
return;
2018-12-17 11:16:17 +00:00
}
PublicVar.okPressed = false;
2018-12-17 11:16:17 +00:00
try
2018-12-17 11:16:17 +00:00
{
CustomRTB.Clear();
string opnfile = File.ReadAllText(filePath);
string de = AES.Decrypt(opnfile, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
CustomRTB.Text = de;
Text = appName + currentFilename;
string cc2 = CustomRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
CustomRTB.Select(Convert.ToInt32(cc2), 0);
CustomRTB.SelectionStart = caretPos;
PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null;
Show();
2018-12-17 11:16:17 +00:00
}
catch (Exception ex)
2018-12-17 11:16:17 +00:00
{
if (ex is CryptographicException)
{
TypedPassword.Value = null;
DialogResult dialogResult = MessageBox.Show("Invalid key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry)
{
AutoLock(false);
}
if (dialogResult == DialogResult.Cancel)
{
PublicVar.encryptionKey.Set(null);
CustomRTB.Clear();
Text = appName.Remove(14);
filePath = "";
currentFilename = "";
Show();
return;
}
}
2018-12-17 11:16:17 +00:00
}
}
protected override void WndProc(ref Message m)
2018-12-17 11:16:17 +00:00
{
const int WM_SYSCOMMAND = 0x112;
const int SC_MINIMIZE = 0xF020;
2018-12-17 11:16:17 +00:00
if (m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE && ps.AutoLock && PublicVar.encryptionKey.Get() != null)
2018-12-17 11:16:17 +00:00
{
if (ps.AutoSave)
{
SaveToolStripMenuItem_Click(this, new EventArgs());
}
else
2018-12-17 11:16:17 +00:00
{
SaveConfirm(false);
2018-12-17 11:16:17 +00:00
}
AutoLock(true);
return;
2018-12-17 11:16:17 +00:00
}
base.WndProc(ref m);
2018-12-17 11:16:17 +00:00
}
/*Functions*/
2018-12-17 11:16:17 +00:00
/*Form Events*/
private void MainWindow_Activated(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
if (PublicVar.settingsChanged)
2018-12-17 11:16:17 +00:00
{
PublicVar.settingsChanged = false;
CustomRTB.Font = new Font(ps.RichTextFont, ps.RichTextSize);
CustomRTB.ForeColor = ps.RichForeColor;
CustomRTB.BackColor = ps.RichBackColor;
BackColor = ps.RichBackColor;
2018-12-17 11:16:17 +00:00
if (ps.SendTo)
{
SendToShortcut();
}
else
{
string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Microsoft\Windows\SendTo\Crypto Notepad.lnk";
if (File.Exists(shortcutPath))
{
File.Delete(shortcutPath);
}
}
#region workaround, unhighlight URLs fix
CustomRTB.DetectUrls = false;
CustomRTB.DetectUrls = true;
2018-12-17 11:16:17 +00:00
#endregion
if (!ps.ShowToolbar && ToolbarPanel.Visible)
2018-12-17 11:16:17 +00:00
{
ToolbarPanel.Visible = false;
CustomRTB.Height += 23;
CustomRTB.Location = new Point(0, 24);
2018-12-17 11:16:17 +00:00
}
if (ps.ShowToolbar && !ToolbarPanel.Visible)
2018-12-17 11:16:17 +00:00
{
ToolbarPanel.Visible = true;
CustomRTB.Height -= 23;
CustomRTB.Location = new Point(0, 47);
2018-12-17 11:16:17 +00:00
}
}
if (PublicVar.keyChanged)
2018-12-17 11:16:17 +00:00
{
CustomRTB.Modified = true;
2018-12-17 11:16:17 +00:00
}
if (PublicVar.encryptionKey.Get() == null)
{
FileLocationToolbarButton.Enabled = false;
DeleteFileToolbarButton.Enabled = false;
ChangeKeyToolbarButton.Enabled = false;
AutoLockToolbarButton.Enabled = false;
2018-12-17 11:16:17 +00:00
}
else
2018-12-17 11:16:17 +00:00
{
FileLocationToolbarButton.Enabled = true;
DeleteFileToolbarButton.Enabled = true;
ChangeKeyToolbarButton.Enabled = true;
AutoLockToolbarButton.Enabled = true;
2018-12-17 11:16:17 +00:00
}
}
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
2018-12-17 11:16:17 +00:00
{
if (WindowState == FormWindowState.Normal)
{
ps.WindowSize = Size;
ps.WindowLocation = Location;
ps.WindowState = WindowState;
}
2018-12-17 11:16:17 +00:00
if (WindowState == FormWindowState.Maximized)
{
ps.WindowState = WindowState;
}
ps.Save();
SaveConfirm(true);
if (noExit)
{
e.Cancel = true;
}
2018-12-17 11:16:17 +00:00
}
private void MainWindow_Load(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
string pos = ps.WindowLocation.ToString();
CustomRTB.Font = new Font(ps.RichTextFont, ps.RichTextSize);
CustomRTB.ForeColor = ps.RichForeColor;
CustomRTB.BackColor = ps.RichBackColor;
BackColor = ps.RichBackColor;
WordWrapToolStripMenuItem.Checked = ps.MenuWrap;
CustomRTB.WordWrap = ps.RichWrap;
ToolbarPanel.Visible = ps.ShowToolbar;
if (!ps.ShowToolbar)
2018-12-17 11:16:17 +00:00
{
ToolbarPanel.Visible = false;
CustomRTB.Height += 23;
CustomRTB.Location = new Point(0, 24);
}
else
{
ToolbarPanel.Visible = true;
}
if (ps.AutoCheckUpdate)
{
Thread up = new Thread(() => CheckForUpdates(false));
up.Start();
2018-12-17 11:16:17 +00:00
}
DeleteUpdateFiles();
if (args.Contains("/s")) /*send to*/
2018-12-17 11:16:17 +00:00
{
foreach (var arg in args)
{
argsPath = arg;
}
SendTo();
2018-12-17 11:16:17 +00:00
}
if (args.Contains("/o")) /*decrypt & open cnp*/
{
OpenAsotiations();
}
if (args.Contains("/e")) /*encrypt*/
{
ContextMenuEncrypt();
}
if (args.Contains("/er")) /*encrypt and replace*/
{
ContextMenuEncryptReplace();
}
if (pos != "{X=0,Y=0}")
{
Location = ps.WindowLocation;
}
Size = ps.WindowSize;
WindowState = ps.WindowState;
#if DEBUG
DebugToolStripMenuItem.Visible = true;
#endif
2018-12-17 11:16:17 +00:00
}
/*Form Events*/
2018-12-17 11:16:17 +00:00
/*CustomRTB Events*/
private void CustomRTB_SelectionChanged_1(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
if (CustomRTB.SelectionLength != 0)
2018-12-17 11:16:17 +00:00
{
CutToolbarButton.Enabled = true;
CopyToolbarButton.Enabled = true;
2018-12-17 11:16:17 +00:00
}
else
{
CutToolbarButton.Enabled = false;
CopyToolbarButton.Enabled = false;
2018-12-17 11:16:17 +00:00
}
}
private void CustomRTB_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
caretPos = CustomRTB.SelectionStart;
2018-12-17 11:16:17 +00:00
}
private void CustomRTB_KeyDown(object sender, KeyEventArgs e)
2018-12-17 11:16:17 +00:00
{
caretPos = CustomRTB.SelectionStart;
if (e.KeyCode == Keys.ShiftKey)
{
shiftPresed = true;
}
2018-12-17 11:16:17 +00:00
}
private void СustomRTB_LinkClicked(object sender, LinkClickedEventArgs e)
2018-12-17 11:16:17 +00:00
{
if (shiftPresed)
{
shiftPresed = false;
Process.Start(e.LinkText);
}
2018-12-17 11:16:17 +00:00
}
private void CustomRTB_DragDrop(object sender, DragEventArgs e)
2018-12-17 11:16:17 +00:00
{
SaveConfirm(false);
if (cancelPressed)
{
cancelPressed = false;
return;
}
2018-12-17 11:16:17 +00:00
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string file in FileList) OpenFile.FileName = file;
object fname = e.Data.GetData("FileDrop");
if (fname != null)
{
var list = fname as string[];
if (list != null && !string.IsNullOrWhiteSpace(list[0]))
{
if (!OpenFile.FileName.Contains(".cnp"))
{
string opnfile = File.ReadAllText(OpenFile.FileName);
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
CustomRTB.Text = opnfile;
Text = appName + NameWithotPath;
string cc2 = CustomRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
CustomRTB.Select(Convert.ToInt32(cc2), 0);
currentFilename = Path.GetFileName(OpenFile.FileName);
filePath = OpenFile.FileName;
return;
}
PublicVar.openFileName = Path.GetFileName(OpenFile.FileName);
DecryptAES();
if (PublicVar.okPressed)
{
PublicVar.okPressed = false;
}
}
}
2018-12-17 11:16:17 +00:00
}
private void CustomRTB_KeyUp(object sender, KeyEventArgs e)
2018-12-17 11:16:17 +00:00
{
if (e.KeyCode == Keys.ShiftKey)
{
shiftPresed = false;
}
2018-12-17 11:16:17 +00:00
}
private void CustomRTB_KeyPress(object sender, KeyPressEventArgs e)
2018-12-17 11:16:17 +00:00
{
}
private void CustomRTB_TextChanged(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
}
/*CustomRTB Events*/
2018-12-17 11:16:17 +00:00
/* Main Menu */
/*File*/
private void NewToolStripMenuItem_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
SaveConfirm(false);
SaveFile.FileName = "Unnamed.cnp";
PublicVar.openFileName = "Crypto Notepad";
EnterKeyForm f2 = new EnterKeyForm();
f2.ShowDialog();
PublicVar.encryptionKey.Set(TypedPassword.Value);
2018-12-17 11:16:17 +00:00
if (!PublicVar.okPressed)
{
TypedPassword.Value = null;
return;
}
else
{
PublicVar.okPressed = false;
if (SaveFile.ShowDialog() != DialogResult.OK)
{
TypedPassword.Value = null;
return;
}
2018-12-17 11:16:17 +00:00
CustomRTB.Clear();
StreamWriter sw = new StreamWriter(SaveFile.FileName);
string NameWithotPath = Path.GetFileName(SaveFile.FileName);
Text = appName + NameWithotPath;
filePath = SaveFile.FileName;
currentFilename = Path.GetFileName(SaveFile.FileName);
sw.Close();
}
TypedPassword.Value = null;
2018-12-17 11:16:17 +00:00
}
private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
OpenFile.FileName = "";
SaveConfirm(false);
if (cancelPressed)
2018-12-17 11:16:17 +00:00
{
cancelPressed = false;
return;
2018-12-17 11:16:17 +00:00
}
if (OpenFile.ShowDialog() != DialogResult.OK) return;
2018-12-17 11:16:17 +00:00
{
PublicVar.openFileName = Path.GetFileName(OpenFile.FileName);
if (!OpenFile.FileName.Contains(".cnp"))
{
string opnfile = File.ReadAllText(OpenFile.FileName);
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
CustomRTB.Text = opnfile;
Text = appName + NameWithotPath;
string cc2 = CustomRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
CustomRTB.Select(Convert.ToInt32(cc2), 0);
return;
}
DecryptAES();
#region workaround, strange behavior with the cursor in customRTB fix
CustomRTB.DetectUrls = false;
CustomRTB.DetectUrls = true;
#endregion
CustomRTB.Modified = false;
if (PublicVar.okPressed)
{
PublicVar.okPressed = false;
}
2018-12-17 11:16:17 +00:00
}
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
int saveCaret = CustomRTB.SelectionStart;
2018-12-17 11:16:17 +00:00
if (PublicVar.encryptionKey.Get() == null)
2018-12-17 11:16:17 +00:00
{
SaveFile.FileName = "Unnamed.cnp";
SaveAsToolStripMenuItem_Click(this, new EventArgs());
if (!PublicVar.okPressed)
2018-12-17 11:16:17 +00:00
{
return;
}
PublicVar.okPressed = false;
}
2018-12-17 11:16:17 +00:00
string noenc = CustomRTB.Text;
string en;
2018-12-17 11:16:17 +00:00
en = AES.Encrypt(CustomRTB.Text, PublicVar.encryptionKey.Get(), null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
CustomRTB.Text = en;
2018-12-17 11:16:17 +00:00
StreamWriter sw = new StreamWriter(filePath);
int i = CustomRTB.Lines.Count();
int j = 0;
i = i - 1;
2018-12-17 11:16:17 +00:00
while (j <= i)
2018-12-17 11:16:17 +00:00
{
sw.WriteLine(CustomRTB.Lines.GetValue(j).ToString());
j = j + 1;
2018-12-17 11:16:17 +00:00
}
sw.Close();
CustomRTB.Text = noenc;
CustomRTB.Select(Convert.ToInt32(saveCaret), 0);
PublicVar.keyChanged = false;
2018-12-17 11:16:17 +00:00
}
private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
int saveCaret = CustomRTB.SelectionStart;
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
SaveFile.FileName = currentFilename;
2018-12-17 11:16:17 +00:00
EnterKeyForm f2 = new EnterKeyForm();
if (string.IsNullOrEmpty(PublicVar.encryptionKey.Get()))
2018-12-17 11:16:17 +00:00
{
f2.ShowDialog();
if (!PublicVar.okPressed)
{
return;
}
PublicVar.okPressed = false;
2018-12-17 11:16:17 +00:00
}
if (SaveFile.ShowDialog() != DialogResult.OK)
2018-12-17 11:16:17 +00:00
{
return;
}
if (TypedPassword.Value == null)
2018-12-17 11:16:17 +00:00
{
TypedPassword.Value = PublicVar.encryptionKey.Get();
2018-12-17 11:16:17 +00:00
}
filePath = SaveFile.FileName;
string noenc = CustomRTB.Text;
string en;
en = AES.Encrypt(CustomRTB.Text, TypedPassword.Value, null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
CustomRTB.Text = en;
StreamWriter sw = new StreamWriter(filePath);
int i = CustomRTB.Lines.Count();
int j = 0;
i = i - 1;
while (j <= i)
2018-12-17 11:16:17 +00:00
{
sw.WriteLine(CustomRTB.Lines.GetValue(j).ToString());
j = j + 1;
}
sw.Close();
CustomRTB.Text = noenc;
Text = appName + Path.GetFileName(filePath);
CustomRTB.Select(Convert.ToInt32(saveCaret), 0);
PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null;
currentFilename = Path.GetFileName(SaveFile.FileName);
}
private void OpenFileLocationToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("explorer.exe", @"/select, " + filePath);
}
private void DeleteFileToolStripMenuItem_Click(object sender, EventArgs e)
{
if (filePath != "")
{
using (new CenterWinDialog(this))
2018-12-17 11:16:17 +00:00
{
if (MessageBox.Show("Delete file: " + "\"" + filePath + "\"" + " ?", "Crypto Notepad", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
2018-12-17 11:16:17 +00:00
{
File.Delete(filePath);
CustomRTB.Clear();
2018-12-17 11:16:17 +00:00
PublicVar.encryptionKey.Set(null);
FileLocationToolbarButton.Enabled = false;
DeleteFileToolbarButton.Enabled = false;
ChangeKeyToolbarButton.Enabled = false;
AutoLockToolbarButton.Enabled = false;
2018-12-19 22:56:59 +00:00
filePath = "";
currentFilename = "Unnamed.cnp";
Text = appName.Remove(14);
2018-12-17 11:16:17 +00:00
return;
}
}
}
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
Application.Exit();
}
2018-12-17 11:16:17 +00:00
private void FileToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
if (currentFilename == "")
2018-12-17 11:16:17 +00:00
{
OpenFileLocationToolStripMenuItem.Enabled = false;
DeleteFileToolStripMenuItem.Enabled = false;
}
else
{
OpenFileLocationToolStripMenuItem.Enabled = true;
DeleteFileToolStripMenuItem.Enabled = true;
2018-12-17 11:16:17 +00:00
}
}
/*File*/
2018-12-17 11:16:17 +00:00
/*Edit*/
private void UndoToolStripMenuItem_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
CustomRTB.Undo();
}
public void RedoToolStripMenuItem_Click(object sender, EventArgs e)
{
CustomRTB.Redo();
}
private void CutToolStripMenuItem_Click(object sender, EventArgs e)
{
CustomRTB.Cut();
}
private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
{
CustomRTB.Copy();
}
private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
{
if (CustomRTB.Focused)
{
CustomRTB.Paste(DataFormats.GetFormat(DataFormats.Text));
}
if (SearchTextBox.Focused)
{
SearchTextBox.Paste();
}
}
private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
{
CustomRTB.SelectedText = "";
}
private void FindToolStripMenuItem_Click(object sender, EventArgs e)
{
if (SearchPanel.Visible)
2018-12-17 11:16:17 +00:00
{
SearchTextBox.Text = "";
SearchPanel.Visible = false;
CustomRTB.Height += 27;
CustomRTB.Focus();
CustomRTB.DeselectAll();
CustomRTB.SelectionStart = caretPos;
2018-12-17 11:16:17 +00:00
}
else
{
SearchPanel.Visible = true;
SearchTextBox.Focus();
CustomRTB.Height -= 27;
2018-12-17 11:16:17 +00:00
}
}
private void SelectAllToolStripMenuItem_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
if (CustomRTB.Focused)
{
CustomRTB.SelectAll();
}
if (SearchTextBox.Focused)
{
SearchTextBox.SelectAll();
}
}
private void WordWrapToolStripMenuItem_Click(object sender, EventArgs e)
{
if (WordWrapToolStripMenuItem.Checked)
2018-12-17 11:16:17 +00:00
{
CustomRTB.WordWrap = true;
2018-12-17 11:16:17 +00:00
}
else
{
CustomRTB.WordWrap = false;
2018-12-17 11:16:17 +00:00
}
ps.MenuWrap = WordWrapToolStripMenuItem.Checked;
ps.RichWrap = CustomRTB.WordWrap;
ps.Save();
2018-12-17 11:16:17 +00:00
}
private void ClearToolStripMenuItem_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
CustomRTB.Clear();
2018-12-17 11:16:17 +00:00
}
private void EditToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
if (CustomRTB.SelectionLength != 0)
2018-12-17 11:16:17 +00:00
{
CutToolStripMenuItem.Enabled = true;
CopyToolStripMenuItem.Enabled = true;
DeleteToolStripMenuItem.Enabled = true;
}
else
{
CutToolStripMenuItem.Enabled = false;
CopyToolStripMenuItem.Enabled = false;
DeleteToolStripMenuItem.Enabled = false;
2018-12-17 11:16:17 +00:00
}
}
/*Edit*/
2018-12-17 11:16:17 +00:00
/*Tools*/
private void ChangeKeyToolStripMenuItem_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
ChangeKeyForm c = new ChangeKeyForm();
c.ShowDialog(this);
}
private void LockToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ps.AutoSave)
2018-12-17 11:16:17 +00:00
{
SaveToolStripMenuItem_Click(this, new EventArgs());
}
else
{
SaveConfirm(false);
2018-12-17 11:16:17 +00:00
}
AutoLock(false);
2018-12-17 11:16:17 +00:00
}
private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
SettingsForm sf = new SettingsForm();
sf.ShowDialog();
}
private void ToolsToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
if (PublicVar.encryptionKey.Get() == null)
2018-12-17 11:16:17 +00:00
{
ChangeKeyToolStripMenuItem.Enabled = false;
LockToolStripMenuItem.Enabled = false;
}
else
{
ChangeKeyToolStripMenuItem.Enabled = true;
LockToolStripMenuItem.Enabled = true;
2018-12-17 11:16:17 +00:00
}
}
/*Tools*/
/*Help*/
private void DocumentationToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("https://github.com/Sigmanor/Crypto-Notepad/wiki/Documentation");
}
private void CheckForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{
Thread up = new Thread(() => CheckForUpdates(true));
up.Start();
}
private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutFrom a = new AboutFrom();
a.ShowDialog(this);
}
/*Help*/
/* Main Menu */
/* Editor Menu */
private void UndoEditorMenuStrip_Click(object sender, EventArgs e)
{
UndoToolStripMenuItem_Click(this, new EventArgs());
}
private void RedoEditorMenuStrip_Click(object sender, EventArgs e)
{
RedoToolStripMenuItem_Click(this, new EventArgs());
}
private void CutEditorMenuStrip_Click(object sender, EventArgs e)
{
CutToolStripMenuItem_Click(this, new EventArgs());
}
private void CopyEditorMenuStrip_Click(object sender, EventArgs e)
{
CopyToolStripMenuItem_Click(this, new EventArgs());
}
private void PasteEditorMenuStrip_Click(object sender, EventArgs e)
{
PasteToolStripMenuItem_Click(this, new EventArgs());
}
private void DeleteEditorMenuStrip_Click(object sender, EventArgs e)
{
DeleteToolStripMenuItem_Click(this, new EventArgs());
}
private void SelectAllEditorMenuStrip_Click(object sender, EventArgs e)
{
SelectAllToolStripMenuItem_Click(this, new EventArgs());
}
2018-12-17 11:16:17 +00:00
2018-12-25 15:22:59 +00:00
private void RightToLeftEditorMenuStrip_Click(object sender, EventArgs e)
{
if (RightToLeftEditorMenuStrip.Checked)
{
if (!WordWrapToolStripMenuItem.Checked)
2018-12-17 11:16:17 +00:00
{
2018-12-25 15:22:59 +00:00
string rtbTxt = CustomRTB.Text;
CustomRTB.Clear();
CustomRTB.RightToLeft = RightToLeft.Yes;
Application.DoEvents();
CustomRTB.Text = rtbTxt;
}
2018-12-25 15:22:59 +00:00
else
{
CustomRTB.RightToLeft = RightToLeft.Yes;
2018-12-17 11:16:17 +00:00
}
}
2018-12-25 15:22:59 +00:00
else
{
CustomRTB.RightToLeft = RightToLeft.No;
}
2018-12-17 11:16:17 +00:00
}
private void ClearEditorMenuStrip_Click(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
ClearToolStripMenuItem_Click(this, new EventArgs());
}
private void EditorMenuStrip_Opening(object sender, CancelEventArgs e)
{
if (CustomRTB.SelectionLength != 0)
2018-12-17 11:16:17 +00:00
{
CutEditorMenuStrip.Enabled = true;
CopyEditorMenuStrip.Enabled = true;
DeleteEditorMenuStrip.Enabled = true;
2018-12-17 11:16:17 +00:00
}
else
2018-12-17 11:16:17 +00:00
{
CutEditorMenuStrip.Enabled = false;
CopyEditorMenuStrip.Enabled = false;
DeleteEditorMenuStrip.Enabled = false;
}
}
/* Editor Menu */
2018-12-17 11:16:17 +00:00
/*Toolbar*/
private void NewToolbarButton_Click(object sender, EventArgs e)
{
NewToolStripMenuItem_Click(this, new EventArgs());
}
2018-12-17 11:16:17 +00:00
private void OpenToolbarButton_Click(object sender, EventArgs e)
{
OpenToolStripMenuItem_Click(this, new EventArgs());
}
private void SaveToolbarButton_Click(object sender, EventArgs e)
{
SaveToolStripMenuItem_Click(this, new EventArgs());
}
private void FileLocationToolbarButton_Click(object sender, EventArgs e)
{
OpenFileLocationToolStripMenuItem_Click(this, new EventArgs());
}
private void DeleteFileToolbarButton_Click(object sender, EventArgs e)
{
DeleteFileToolStripMenuItem_Click(this, new EventArgs());
}
2018-12-17 11:16:17 +00:00
private void CutToolbarButton_Click(object sender, EventArgs e)
{
CutToolStripMenuItem_Click(this, new EventArgs());
}
private void CopyToolbarButton_Click(object sender, EventArgs e)
{
CopyToolStripMenuItem_Click(this, new EventArgs());
}
private void PasteToolbarButton_Click(object sender, EventArgs e)
{
PasteToolStripMenuItem_Click(this, new EventArgs());
}
private void ChangeKeyToolbarButton_Click(object sender, EventArgs e)
{
ChangeKeyToolStripMenuItem_Click(this, new EventArgs());
}
private void SettingsToolbarButton_Click(object sender, EventArgs e)
{
SettingsToolStripMenuItem_Click(this, new EventArgs());
}
private void AutoLockToolbarButton_Click(object sender, EventArgs e)
{
AutoLock(false);
}
private void CloseToolbar_Click(object sender, EventArgs e)
{
ToolbarPanel.Visible = false;
2018-12-25 15:15:29 +00:00
CustomRTB.Height += 23;
CustomRTB.Location = new Point(0, 24);
ps.ShowToolbar = false;
ps.Save();
}
2018-12-17 11:16:17 +00:00
private void CloseToolbar_MouseEnter(object sender, EventArgs e)
{
CloseToolbar.Image = Properties.Resources.close_b;
}
2018-12-17 11:16:17 +00:00
private void CloseToolbar_MouseLeave(object sender, EventArgs e)
{
CloseToolbar.Image = Properties.Resources.close_g;
2018-12-17 11:16:17 +00:00
}
/*Toolbar*/
/*Search Panel*/
private void SearchTextBox_TextChanged(object sender, EventArgs e)
2018-12-17 11:16:17 +00:00
{
bool isexist = CustomRTB.Highlight(SearchTextBox.Text, ps.HighlightsColor, chkMatchCase.Checked, chkMatchWholeWord.Checked);
}
private void SearchTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
2018-12-17 11:16:17 +00:00
{
SearchTextBox.Text = "";
SearchPanel.Visible = false;
CustomRTB.Height += 27;
CustomRTB.Focus();
CustomRTB.DeselectAll();
CustomRTB.SelectionStart = caretPos;
e.Handled = e.SuppressKeyPress = true;
2018-12-17 11:16:17 +00:00
}
}
private void CloseSearchPanel_Click(object sender, EventArgs e)
{
FindToolStripMenuItem_Click(this, new EventArgs());
}
private void CloseSearchPanel_MouseHover(object sender, EventArgs e)
{
CloseSearchPanel.Image = Properties.Resources.close_b;
}
private void CloseSearchPanel_MouseLeave(object sender, EventArgs e)
{
CloseSearchPanel.Image = Properties.Resources.close_g;
}
private void ChkMatchCase_CheckedChanged(object sender, EventArgs e)
{
bool isexist = CustomRTB.Highlight(SearchTextBox.Text, ps.HighlightsColor, chkMatchCase.Checked, chkMatchWholeWord.Checked);
}
private void ChkMatchWholeWord_CheckedChanged(object sender, EventArgs e)
{
bool isexist = CustomRTB.Highlight(SearchTextBox.Text, ps.HighlightsColor, chkMatchCase.Checked, chkMatchWholeWord.Checked);
}
/*Search Panel*/
/*Debug Menu*/
private void MainVariablesToolStripMenuItem_Click(object sender, EventArgs e)
{
Debug.WriteLine("\ncurrentFilename: " + currentFilename);
Debug.WriteLine("encryptionKey: " + PublicVar.encryptionKey.Get());
Debug.WriteLine("TypedPassword: " + TypedPassword.Value);
Debug.WriteLine("filePath: " + filePath);
Debug.WriteLine("cancelPressed: " + cancelPressed);
Debug.WriteLine("noExit: " + noExit);
Debug.WriteLine("keyChanged: " + PublicVar.keyChanged);
Debug.WriteLine("settingsChanged: " + PublicVar.settingsChanged);
Debug.WriteLine("okPressed: " + PublicVar.okPressed);
Debug.WriteLine("CustomRTB.Modified: " + CustomRTB.Modified);
}
/*Debug Menu*/
2018-12-17 11:16:17 +00:00
}
}