Crypto-Notepad/Crypto Notepad/Form1.cs

1168 lines
38 KiB
C#
Raw Normal View History

2016-01-06 14:47:38 +00:00
using System;
using System.ComponentModel;
using System.Diagnostics;
2016-01-06 14:47:38 +00:00
using System.Drawing;
using System.Globalization;
2016-01-06 14:47:38 +00:00
using System.IO;
using System.Linq;
using System.Net;
2016-01-06 14:47:38 +00:00
using System.Net.NetworkInformation;
using System.Reflection;
2016-01-18 18:39:35 +00:00
using System.Security.Cryptography;
using System.Threading;
2016-01-06 14:47:38 +00:00
using System.Windows.Forms;
2016-01-09 20:46:25 +00:00
namespace Crypto_Notepad
2016-01-06 14:47:38 +00:00
{
public partial class MainWindow : Form
{
Properties.Settings ps = Properties.Settings.Default;
2016-01-25 18:26:55 +00:00
string filename = "Unnamed.cnp";
2016-01-24 21:05:35 +00:00
string[] args = Environment.GetCommandLineArgs();
2016-01-24 21:33:58 +00:00
int caretPos = 0;
2017-02-05 20:47:31 +00:00
string appName = Assembly.GetExecutingAssembly().GetName().Name + " ";
2017-02-09 21:42:08 +00:00
string currentFilename = "Unnamed.cnp";
2017-02-05 20:38:26 +00:00
bool shiftPresed;
2017-02-09 21:42:08 +00:00
bool cancelPressed = false;
bool noExit = false;
public MainWindow()
2016-01-06 14:47:38 +00:00
{
InitializeComponent();
customRTB.DragDrop += new DragEventHandler(customRTB_DragDrop);
customRTB.AllowDrop = true;
2016-01-06 14:47:38 +00:00
}
#region SaltMac
2016-01-06 14:47:38 +00:00
void SaltMAC()
{
var address = "";
2016-01-06 14:47:38 +00:00
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
2016-01-06 14:47:38 +00:00
foreach (var networkInterface in networkInterfaces)
{
if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
address = networkInterface.GetPhysicalAddress().ToString();
}
}
2016-01-06 14:47:38 +00:00
if (ps.FirstLaunch == false)
{
DialogResult res = new DialogResult();
using (new CenterWinDialog(this))
{
res = MessageBox.Show("Get The Salt from mac address? (You can change it yourself in Settings)", "Crypto Notepad",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
}
2016-01-06 14:47:38 +00:00
if (res == DialogResult.Yes)
{
ps.TheSalt = address;
ps.FirstLaunch = true;
}
2016-01-06 14:47:38 +00:00
if (res == DialogResult.No)
{
ps.FirstLaunch = true;
2016-01-06 14:47:38 +00:00
}
ps.Save();
2016-01-06 14:47:38 +00:00
}
}
#endregion
2016-01-06 14:47:38 +00:00
2016-01-18 18:39:35 +00:00
void DecryptAES()
{
2017-02-05 20:47:31 +00:00
publicVar.openFileName = Path.GetFileName(OpenFile.FileName);
2016-01-18 18:39:35 +00:00
Form2 f2 = new Form2();
f2.ShowDialog();
if (publicVar.okPressed == false)
2016-01-18 18:39:35 +00:00
{
OpenFile.FileName = "";
2016-01-18 18:39:35 +00:00
return;
2016-01-06 14:47:38 +00:00
}
if (panel1.Visible == true)
{
findToolStripMenuItem2_Click(this, new EventArgs());
}
2016-01-18 18:39:35 +00:00
try
2016-01-06 14:47:38 +00:00
{
2016-01-18 18:39:35 +00:00
string opnfile = File.ReadAllText(OpenFile.FileName);
2016-01-06 14:47:38 +00:00
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
string de = AES.Decrypt(opnfile, publicVar.encryptionKey.Get(), ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
2016-01-18 18:39:35 +00:00
customRTB.Text = de;
2016-01-27 16:26:19 +00:00
this.Text = appName + NameWithotPath;
2016-01-18 18:39:35 +00:00
filename = OpenFile.FileName;
2016-01-18 18:39:35 +00:00
string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
customRTB.Select(Convert.ToInt32(cc2), 0);
}
catch (CryptographicException)
{
using (new CenterWinDialog(this))
2016-01-06 14:47:38 +00:00
{
2016-01-22 17:42:13 +00:00
DialogResult dialogResult = MessageBox.Show("Invalid key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
2016-01-18 18:39:35 +00:00
if (dialogResult == DialogResult.Retry)
2016-01-06 14:47:38 +00:00
{
2016-01-18 18:39:35 +00:00
DecryptAES();
2016-01-06 14:47:38 +00:00
}
2016-01-18 18:39:35 +00:00
}
}
}
2016-01-06 14:47:38 +00:00
private void openToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-18 18:39:35 +00:00
{
2017-02-09 21:42:08 +00:00
saveConfirm(false);
if (cancelPressed == true)
{
cancelPressed = false;
return;
}
2017-02-05 20:47:31 +00:00
2016-01-18 18:39:35 +00:00
if (OpenFile.ShowDialog() != DialogResult.OK) return;
{
2016-01-24 20:48:34 +00:00
if (!OpenFile.FileName.Contains(".cnp"))
2016-01-18 18:39:35 +00:00
{
string opnfile = File.ReadAllText(OpenFile.FileName);
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
customRTB.Text = opnfile;
2016-01-27 16:26:19 +00:00
this.Text = appName + NameWithotPath;
2016-01-18 18:39:35 +00:00
string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
customRTB.Select(Convert.ToInt32(cc2), 0);
return;
}
2017-02-09 21:42:08 +00:00
DecryptAES();
/*workaround, strange behavior with the cursor in customRTB fix*/
customRTB.DetectUrls = false;
customRTB.DetectUrls = true;
customRTB.Modified = false;
/*workaround, strange behavior with the cursor in customRTB fix*/
2017-02-09 21:42:08 +00:00
if (publicVar.okPressed == true)
{
publicVar.okPressed = false;
currentFilename = Path.GetFileName(OpenFile.FileName);
}
2016-01-18 18:39:35 +00:00
}
2016-01-06 14:47:38 +00:00
}
private void openAsotiations()
{
Form2 Form2 = new Form2();
Form2.StartPosition = FormStartPosition.CenterScreen;
try
{
string NameWithotPath = Path.GetFileName(args[1]);
string opnfile = File.ReadAllText(args[1]);
2016-01-18 18:39:35 +00:00
2016-01-06 14:47:38 +00:00
Form2.ShowDialog();
if (publicVar.okPressed == false)
2016-01-06 14:47:38 +00:00
{
OpenFile.FileName = "";
2016-01-06 14:47:38 +00:00
return;
}
publicVar.okPressed = false;
2016-01-06 14:47:38 +00:00
string de = AES.Decrypt(opnfile, publicVar.encryptionKey.Get(), ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
2016-01-06 14:47:38 +00:00
customRTB.Text = de;
2016-01-27 16:26:19 +00:00
this.Text = appName + NameWithotPath;
filename = args[1];
string cc = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
customRTB.Select(Convert.ToInt32(cc), 0);
2016-01-06 14:47:38 +00:00
}
catch
{
2016-01-22 17:42:13 +00:00
DialogResult dialogResult = MessageBox.Show("Invalid key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
2016-01-06 14:47:38 +00:00
if (dialogResult == DialogResult.Retry)
{
2016-01-18 18:39:35 +00:00
openAsotiations();
2016-01-06 14:47:38 +00:00
}
}
2017-02-09 21:42:08 +00:00
currentFilename = Path.GetFileName(OpenFile.FileName);
2016-01-06 14:47:38 +00:00
}
private void newToolStripMenuItem_Click_1(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
2016-01-09 19:29:13 +00:00
Form2 f2 = new Form2();
f2.ShowDialog();
if (publicVar.okPressed == false)
2016-01-09 19:29:13 +00:00
{
return;
}
if (publicVar.okPressed == true)
2016-01-09 19:29:13 +00:00
{
publicVar.okPressed = false;
2016-01-09 19:29:13 +00:00
if (SaveFile.ShowDialog() != DialogResult.OK)
{
return;
}
customRTB.Clear();
StreamWriter sw = new StreamWriter(SaveFile.FileName);
string NameWithotPath = Path.GetFileName(SaveFile.FileName);
2016-01-27 16:26:19 +00:00
this.Text = appName + NameWithotPath;
2016-01-09 19:29:13 +00:00
filename = SaveFile.FileName;
sw.Close();
}
2016-01-06 14:47:38 +00:00
}
2016-01-25 10:34:53 +00:00
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
2018-02-17 12:48:03 +00:00
int saveCaret = customRTB.SelectionStart;
2016-01-06 14:47:38 +00:00
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
if (string.IsNullOrEmpty(publicVar.encryptionKey.Get()))
2016-01-06 14:47:38 +00:00
{
Form2 Form2 = new Form2();
Form2.ShowDialog();
if (publicVar.okPressed == false)
{
return;
}
publicVar.okPressed = false;
}
if (SaveFile.ShowDialog() != DialogResult.OK)
{
return;
2016-01-06 14:47:38 +00:00
}
filename = SaveFile.FileName;
string noenc = customRTB.Text;
string en;
if (publicVar.randomizeSalts)
{
en = AES.Encrypt(customRTB.Text, publicVar.encryptionKey.Get(), null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
}
else
{
en = AES.Encrypt(customRTB.Text, publicVar.encryptionKey.Get(), ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
}
2016-01-06 14:47:38 +00:00
customRTB.Text = en;
StreamWriter sw = new StreamWriter(filename);
int i = customRTB.Lines.Count();
int j = 0;
i = i - 1;
while (j <= i)
{
sw.WriteLine(customRTB.Lines.GetValue(j).ToString());
j = j + 1;
}
sw.Close();
customRTB.Text = noenc;
2017-02-05 20:47:31 +00:00
this.Text = appName + Path.GetFileName(filename);
2018-02-17 12:48:03 +00:00
customRTB.Select(Convert.ToInt32(saveCaret), 0);
2016-01-06 14:47:38 +00:00
}
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
ps.WindowSize = this.Size;
2016-01-24 21:11:23 +00:00
ps.WindowLocation = this.Location;
ps.WindowState = this.WindowState;
}
if (this.WindowState == FormWindowState.Maximized)
{
ps.WindowState = this.WindowState;
}
2016-01-06 14:47:38 +00:00
ps.Save();
2017-02-09 21:42:08 +00:00
saveConfirm(true);
2016-01-06 14:47:38 +00:00
2017-02-09 21:42:08 +00:00
if (noExit == true)
{
e.Cancel = true;
2016-01-06 14:47:38 +00:00
}
}
private void MainWindow_Load(object sender, EventArgs e)
{
string pos = ps.WindowLocation.ToString();
2016-01-06 14:47:38 +00:00
customRTB.Font = new Font(ps.RichTextFont, ps.RichTextSize);
customRTB.ForeColor = ps.RichForeColor;
customRTB.BackColor = ps.RichBackColor;
this.BackColor = ps.RichBackColor;
2016-01-24 20:54:45 +00:00
wordWrapToolStripMenuItem.Checked = ps.MenuWrap;
2016-01-06 14:47:38 +00:00
customRTB.WordWrap = ps.RichWrap;
panel2.Visible = ps.ShowToolbar;
if (ps.ShowToolbar == false)
{
panel2.Visible = false;
int h = customRTB.Height;
h += 23;
customRTB.Height = h;
customRTB.Location = new Point(6, 29);
}
if (ps.ShowToolbar == true)
{
panel2.Visible = true;
}
2016-01-23 19:27:25 +00:00
SaltMAC();
if (ps.AutoCheckUpdate == true)
{
Thread up = new Thread(() => сheckForUpdates(false));
up.Start();
}
2016-01-06 14:47:38 +00:00
DeleteUpdateFiles();
2016-01-06 14:47:38 +00:00
if (args.Length > 1)
{
openAsotiations();
}
2016-01-24 21:11:12 +00:00
if (pos != "{X=0,Y=0}")
{
this.Location = ps.WindowLocation;
}
this.Size = ps.WindowSize;
this.WindowState = ps.WindowState;
}
public void DeleteUpdateFiles()
{
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);
2016-01-06 14:47:38 +00:00
}
if (File.Exists(UpdateZip))
{
File.Delete(UpdateZip);
}
if (File.Exists(ZipDll))
{
File.Delete(ZipDll);
}
2016-01-06 14:47:38 +00:00
}
2016-01-24 21:33:58 +00:00
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
customRTB.Clear();
}
2016-01-25 10:34:53 +00:00
private void saveToolStripMenuItem1_Click_1(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
2016-01-25 10:38:07 +00:00
string noname = "Unnamed.cnp";
2016-01-06 14:47:38 +00:00
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
2018-02-17 12:48:03 +00:00
int saveCaret = customRTB.SelectionStart;
2016-01-06 14:47:38 +00:00
2016-01-25 10:38:07 +00:00
if (filename == noname)
2016-01-06 14:47:38 +00:00
{
2016-01-25 10:38:07 +00:00
SaveFile.FileName = noname;
saveAsToolStripMenuItem_Click(this, new EventArgs());
if (publicVar.okPressed == false)
{
return;
}
publicVar.okPressed = false;
2016-01-06 14:47:38 +00:00
}
string noenc = customRTB.Text;
string en;
if (publicVar.randomizeSalts)
{
en = AES.Encrypt(customRTB.Text, publicVar.encryptionKey.Get(), null, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
}
else
{
en = AES.Encrypt(customRTB.Text, publicVar.encryptionKey.Get(), ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
}
2016-01-06 14:47:38 +00:00
customRTB.Text = en;
StreamWriter sw = new StreamWriter(filename);
int i = customRTB.Lines.Count();
int j = 0;
i = i - 1;
while (j <= i)
{
sw.WriteLine(customRTB.Lines.GetValue(j).ToString());
j = j + 1;
}
sw.Close();
customRTB.Text = noenc;
2018-02-17 12:48:03 +00:00
customRTB.Select(Convert.ToInt32(saveCaret), 0);
2016-01-06 14:47:38 +00:00
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
Application.Exit();
}
2016-01-24 21:33:58 +00:00
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
if (customRTB.Focused == true)
{
customRTB.SelectAll();
}
else
{
searchTextBox.SelectAll();
}
2016-01-06 14:47:38 +00:00
}
2016-01-24 21:33:58 +00:00
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
if (customRTB.Focused == true)
{
2016-01-06 14:47:38 +00:00
customRTB.Cut();
}
2016-01-06 14:47:38 +00:00
else
{
searchTextBox.Cut();
}
2016-01-06 14:47:38 +00:00
}
2016-01-24 21:33:58 +00:00
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
if (customRTB.Focused == true)
{
2016-01-06 14:47:38 +00:00
customRTB.Copy();
}
2016-01-06 14:47:38 +00:00
else
{
2016-01-06 14:47:38 +00:00
searchTextBox.Copy();
}
2016-01-06 14:47:38 +00:00
}
2016-01-24 21:33:58 +00:00
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
if (customRTB.Focused == true)
{
2016-01-06 14:47:38 +00:00
customRTB.Paste(DataFormats.GetFormat(DataFormats.Text));
}
2016-01-06 14:47:38 +00:00
else
{
2016-01-06 14:47:38 +00:00
searchTextBox.Paste();
}
2016-01-06 14:47:38 +00:00
}
2016-01-25 10:34:53 +00:00
private void deleteFileToolStripMenuItem_Click_1(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
2016-01-18 18:39:35 +00:00
if (filename != "Unnamed.cnp")
2016-01-06 14:47:38 +00:00
{
2016-01-09 19:29:13 +00:00
using (new CenterWinDialog(this))
2016-01-06 14:47:38 +00:00
{
2016-01-25 18:26:39 +00:00
if (MessageBox.Show("Delete file: " + "\"" + filename + "\"" + " ?", "Crypto Notepad", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
2016-01-09 19:29:13 +00:00
{
File.Delete(filename);
customRTB.Clear();
publicVar.encryptionKey.Set(null);
2016-01-27 16:26:19 +00:00
pictureBox6.Enabled = false;
pictureBox7.Enabled = false;
pictureBox11.Enabled = false;
2016-01-27 16:26:19 +00:00
pictureBox13.Enabled = false;
2016-01-18 18:39:35 +00:00
filename = "Unnamed.cnp";
2016-01-27 16:26:19 +00:00
this.Text = appName.Remove(14);
return;
2016-01-09 19:29:13 +00:00
}
2016-01-06 14:47:38 +00:00
}
}
}
private void openFileLocationToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
2016-01-18 18:39:35 +00:00
if (filename != "Unnamed.cnp")
2016-01-06 14:47:38 +00:00
{
Process.Start("explorer.exe", @"/select, " + filename);
2016-01-06 14:47:38 +00:00
}
}
2016-01-24 21:33:58 +00:00
private void wordWrapToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
2016-01-24 20:54:45 +00:00
if (wordWrapToolStripMenuItem.Checked == true)
2016-01-06 14:47:38 +00:00
{
customRTB.WordWrap = true;
}
2016-01-24 20:54:45 +00:00
if (wordWrapToolStripMenuItem.Checked == false)
2016-01-06 14:47:38 +00:00
{
customRTB.WordWrap = false;
}
ps.MenuWrap = wordWrapToolStripMenuItem.Checked;
ps.RichWrap = customRTB.WordWrap;
ps.Save();
2016-01-06 14:47:38 +00:00
}
2016-01-24 21:33:58 +00:00
private void undoToolStripMenuItem1_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
2016-01-24 21:33:58 +00:00
undoToolStripMenuItem_Click(this, new EventArgs());
2016-01-06 14:47:38 +00:00
}
2016-01-24 21:33:58 +00:00
public void undoToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
if (customRTB.CanUndo == true)
{
customRTB.Undo();
}
else customRTB.Redo();
}
2016-01-24 21:33:58 +00:00
private void cutToolStripMenuItem1_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
customRTB.Cut();
}
2016-01-24 21:33:58 +00:00
private void copyToolStripMenuItem1_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
customRTB.Copy();
}
2016-01-24 21:33:58 +00:00
private void pasteToolStripMenuItem1_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
customRTB.Paste(DataFormats.GetFormat(DataFormats.Text));
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
if (customRTB.SelectionLength != 0)
{
2016-01-24 21:33:58 +00:00
cutToolStripMenuItem1.Enabled = true;
copyToolStripMenuItem1.Enabled = true;
deleteToolStripMenuItem1.Enabled = true;
2016-01-06 14:47:38 +00:00
}
if (customRTB.SelectionLength == 0)
{
2016-01-24 21:33:58 +00:00
cutToolStripMenuItem1.Enabled = false;
copyToolStripMenuItem1.Enabled = false;
deleteToolStripMenuItem1.Enabled = false;
2016-01-06 14:47:38 +00:00
}
}
2016-01-24 21:33:58 +00:00
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
customRTB.SelectedText = "";
}
2016-01-24 21:33:58 +00:00
private void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
customRTB.SelectedText = "";
}
2016-01-24 21:33:58 +00:00
private void rightToLeftToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
2016-01-24 21:33:58 +00:00
if (rightToLeftToolStripMenuItem.Checked == true)
2016-01-06 14:47:38 +00:00
{
customRTB.RightToLeft = RightToLeft.Yes;
}
2016-01-24 21:33:58 +00:00
if (rightToLeftToolStripMenuItem.Checked == false)
2016-01-06 14:47:38 +00:00
{
customRTB.RightToLeft = RightToLeft.No;
}
2016-01-06 14:47:38 +00:00
}
2016-01-24 21:33:58 +00:00
private void clearToolStripMenuItem1_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
customRTB.Clear();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
2016-01-07 15:00:49 +00:00
AboutFrom a = new AboutFrom();
a.ShowDialog(this);
2016-01-06 14:47:38 +00:00
}
2016-01-24 21:33:58 +00:00
private void selectAllToolStripMenuItem1_Click(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
customRTB.SelectAll();
}
2016-01-06 14:47:38 +00:00
private void changeKeyToolStripMenuItem_Click(object sender, EventArgs e)
{
2016-01-07 16:45:32 +00:00
ChangeKeyForm c = new ChangeKeyForm();
c.ShowDialog(this);
2016-01-06 14:47:38 +00:00
}
public int FindMyTextNext(string text, int start)
{
int returnValue = -1;
if (text.Length > 0 && start >= 0)
{
int indexToText = customRTB.Find(text, start, RichTextBoxFinds.MatchCase);
if (indexToText >= 0)
{
returnValue = indexToText;
}
}
return returnValue;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
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)
{
searchTextBox.Text = "";
panel1.Visible = false;
customRTB.Height = customRTB.Height += 27;
customRTB.Focus();
customRTB.DeselectAll();
2016-01-23 19:24:56 +00:00
customRTB.SelectionStart = caretPos;
2016-01-06 14:47:38 +00:00
e.Handled = e.SuppressKeyPress = true;
}
}
private void customRTB_SelectionChanged_1(object sender, EventArgs e)
{
if (customRTB.SelectionLength != 0)
{
pictureBox8.Enabled = true;
pictureBox9.Enabled = true;
}
if (customRTB.SelectionLength == 0)
{
pictureBox8.Enabled = false;
pictureBox9.Enabled = false;
}
2016-01-06 14:47:38 +00:00
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
SettingsForm sf = new SettingsForm();
2016-01-24 21:07:44 +00:00
sf.ShowDialog();
2016-01-06 14:47:38 +00:00
}
2016-01-25 10:34:53 +00:00
private void MainWindow_Activated(object sender, EventArgs e)
2016-01-06 14:47:38 +00:00
{
if (publicVar.settingsChanged == true)
2016-01-09 19:29:13 +00:00
{
2017-02-05 20:47:31 +00:00
publicVar.settingsChanged = false;
2016-01-09 19:29:13 +00:00
customRTB.Font = new Font(ps.RichTextFont, ps.RichTextSize);
customRTB.ForeColor = ps.RichForeColor;
customRTB.BackColor = ps.RichBackColor;
2017-02-05 20:48:16 +00:00
/*workaround, unhighlight URLs fix*/
2017-02-09 21:42:08 +00:00
customRTB.DetectUrls = false;
customRTB.DetectUrls = true;
2017-02-05 20:48:16 +00:00
/*workaround, unhighlight URLs fix*/
2016-01-12 16:03:17 +00:00
if (ps.ShowToolbar == false && panel2.Visible == true)
{
panel2.Visible = false;
int h = customRTB.Height;
h += 23;
customRTB.Height = h;
customRTB.Location = new Point(6, 29);
}
2016-01-12 16:03:17 +00:00
if (ps.ShowToolbar == true && panel2.Visible == false)
{
panel2.Visible = true;
int h = customRTB.Height;
h -= 23;
customRTB.Height = h;
customRTB.Location = new Point(6, 52);
}
2017-02-05 20:47:31 +00:00
2016-01-09 19:29:13 +00:00
}
if (publicVar.keyChanged == true)
2016-01-09 19:29:13 +00:00
{
customRTB.Modified = true;
2016-01-09 19:29:13 +00:00
}
if (publicVar.encryptionKey.Get() == null)
{
2016-01-26 16:40:18 +00:00
pictureBox6.Enabled = false;
pictureBox7.Enabled = false;
2016-01-27 16:26:19 +00:00
pictureBox11.Enabled = false;
pictureBox13.Enabled = false;
}
if (publicVar.encryptionKey.Get() != null)
{
2016-01-26 16:40:18 +00:00
pictureBox6.Enabled = true;
pictureBox7.Enabled = true;
2016-01-27 16:26:19 +00:00
pictureBox11.Enabled = true;
pictureBox13.Enabled = true;
}
2016-01-06 14:47:38 +00:00
}
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);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
findToolStripMenuItem2_Click(this, new EventArgs());
2016-01-06 14:47:38 +00:00
}
private void findToolStripMenuItem2_Click(object sender, EventArgs e)
{
if (panel1.Visible == false)
{
panel1.Visible = true;
searchTextBox.Focus();
customRTB.Height = customRTB.Height - 27;
return;
}
if (panel1.Visible == true)
{
searchTextBox.Text = "";
panel1.Visible = false;
customRTB.Height = customRTB.Height += 27;
customRTB.Focus();
customRTB.DeselectAll();
2016-01-23 19:24:56 +00:00
customRTB.SelectionStart = caretPos;
return;
2016-01-06 14:47:38 +00:00
}
}
private void documentationToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("https://github.com/Sigmanor/Crypto-Notepad/wiki/Documentation-%28ENG%29");
2016-01-06 14:47:38 +00:00
}
2016-01-07 16:45:32 +00:00
private void сервисToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
if (publicVar.encryptionKey.Get() == null)
2016-01-07 16:45:32 +00:00
{
changeKeyToolStripMenuItem.Enabled = false;
lockToolStripMenuItem.Enabled = false;
2016-01-07 16:45:32 +00:00
}
if (publicVar.encryptionKey.Get() != null)
2016-01-07 16:45:32 +00:00
{
changeKeyToolStripMenuItem.Enabled = true;
lockToolStripMenuItem.Enabled = true;
2016-01-07 16:45:32 +00:00
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
newToolStripMenuItem_Click_1(this, new EventArgs());
}
private void pictureBox3_Click(object sender, EventArgs e)
{
openToolStripMenuItem_Click(this, new EventArgs());
}
private void pictureBox4_Click(object sender, EventArgs e)
{
saveToolStripMenuItem1_Click_1(this, new EventArgs());
}
private void pictureBox6_Click(object sender, EventArgs e)
{
openFileLocationToolStripMenuItem_Click(this, new EventArgs());
}
private void pictureBox7_Click(object sender, EventArgs e)
{
deleteFileToolStripMenuItem_Click_1(this, new EventArgs());
}
private void pictureBox8_Click(object sender, EventArgs e)
{
2016-01-24 21:33:58 +00:00
cutToolStripMenuItem_Click(this, new EventArgs());
}
private void pictureBox9_Click(object sender, EventArgs e)
{
2016-01-24 21:33:58 +00:00
copyToolStripMenuItem_Click(this, new EventArgs());
}
private void pictureBox10_Click(object sender, EventArgs e)
{
2016-01-24 21:33:58 +00:00
pasteToolStripMenuItem_Click(this, new EventArgs());
}
private void pictureBox11_Click(object sender, EventArgs e)
{
changeKeyToolStripMenuItem_Click(this, new EventArgs());
}
private void pictureBox5_Click(object sender, EventArgs e)
{
settingsToolStripMenuItem_Click(this, new EventArgs());
}
private void pictureBox12_Click(object sender, EventArgs e)
{
panel2.Visible = false;
int h = customRTB.Height;
h += 23;
customRTB.Height = h;
customRTB.Location = new Point(6, 29);
ps.ShowToolbar = false;
ps.Save();
}
private void pictureBox12_MouseEnter(object sender, EventArgs e)
{
pictureBox12.Image = Properties.Resources.close_b;
}
private void pictureBox12_MouseLeave(object sender, EventArgs e)
{
pictureBox12.Image = Properties.Resources.close_g;
}
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.close_b;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Image = Properties.Resources.close_g;
}
private void правкаToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
if (customRTB.SelectionLength != 0)
{
2016-01-24 21:33:58 +00:00
cutToolStripMenuItem.Enabled = true;
copyToolStripMenuItem.Enabled = true;
deleteToolStripMenuItem.Enabled = true;
}
if (customRTB.SelectionLength == 0)
{
2016-01-24 21:33:58 +00:00
cutToolStripMenuItem.Enabled = false;
copyToolStripMenuItem.Enabled = false;
deleteToolStripMenuItem.Enabled = false;
}
}
private void сheckForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{
Thread up = new Thread(() => сheckForUpdates(true));
up.Start();
}
public void сheckForUpdates(bool autoCheck)
{
try
{
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) + @"\";
int ver = Convert.ToInt32(version.Replace(".", "")), con = Convert.ToInt32(content.Replace(".", ""));
if (con != ver)
{
MainMenu.Invoke((Action)delegate
{
using (new CenterWinDialog(this))
{
DialogResult res = new 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();
}
}
});
}
if (con == ver && autoCheck == true)
{
MainMenu.Invoke((Action)delegate
{
using (new CenterWinDialog(this))
{
MessageBox.Show("Crypto Notepad is up to date.", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
});
}
}
catch
{
return;
}
}
2017-02-05 20:47:31 +00:00
void AutoLock(bool minimize)
{
2017-02-05 20:47:31 +00:00
saveToolStripMenuItem1_Click_1(this, new EventArgs());
Form2 f2 = new Form2();
publicVar.encryptionKey.Set(null);
2016-01-23 19:24:56 +00:00
caretPos = customRTB.SelectionStart;
2016-01-26 17:08:17 +00:00
f2.MinimizeBox = true;
this.Hide();
2016-01-26 17:08:17 +00:00
if (minimize == true)
{
f2.WindowState = FormWindowState.Minimized;
}
f2.ShowDialog();
if (publicVar.okPressed == false)
{
publicVar.encryptionKey.Set(null);
customRTB.Clear();
2016-01-27 16:26:19 +00:00
this.Text = appName.Remove(14);
OpenFile.FileName = "";
this.Show();
return;
}
publicVar.okPressed = false;
try
{
2016-01-25 10:40:31 +00:00
OpenFile.FileName = filename;
string opnfile = File.ReadAllText(OpenFile.FileName);
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
string de = AES.Decrypt(opnfile, publicVar.encryptionKey.Get(), ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, ps.KeySize);
2016-01-27 16:26:19 +00:00
this.Text = appName + NameWithotPath;
filename = OpenFile.FileName;
string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
2016-01-23 19:24:56 +00:00
customRTB.SelectionStart = caretPos;
this.Show();
}
catch (CryptographicException)
{
2016-01-22 17:42:13 +00:00
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();
2016-01-27 16:26:19 +00:00
this.Text = appName.Remove(14);
OpenFile.FileName = "";
this.Show();
return;
}
}
}
protected override void WndProc(ref Message m)
{
2016-01-24 20:56:49 +00:00
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.Get() != null)
{
AutoLock(true);
return;
}
base.WndProc(ref m);
}
private void pictureBox13_Click(object sender, EventArgs e)
{
AutoLock(false);
}
private void lockToolStripMenuItem_Click(object sender, EventArgs e)
{
AutoLock(false);
}
private void файлToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
{
if (OpenFile.FileName == "")
{
openFileLocationToolStripMenuItem.Enabled = false;
deleteFileToolStripMenuItem.Enabled = false;
}
if (OpenFile.FileName != "")
{
openFileLocationToolStripMenuItem.Enabled = true;
deleteFileToolStripMenuItem.Enabled = true;
}
}
2016-01-23 19:24:56 +00:00
private void customRTB_Click(object sender, EventArgs e)
{
2017-02-05 20:47:31 +00:00
caretPos = customRTB.SelectionStart;
2016-01-23 19:24:56 +00:00
}
private void customRTB_KeyDown(object sender, KeyEventArgs e)
{
caretPos = customRTB.SelectionStart;
2017-02-05 20:38:26 +00:00
if (e.KeyCode == Keys.ShiftKey)
{
shiftPresed = true;
}
2016-01-23 19:24:56 +00:00
}
2016-01-25 18:22:01 +00:00
private void customRTB_LinkClicked(object sender, LinkClickedEventArgs e)
{
2017-02-05 20:38:26 +00:00
if (shiftPresed)
{
shiftPresed = false;
Process.Start(e.LinkText);
}
}
2017-02-05 20:47:31 +00:00
private void customRTB_DragDrop(object sender, DragEventArgs e)
{
2017-02-09 21:42:08 +00:00
saveConfirm(false);
if (cancelPressed == true)
{
cancelPressed = false;
return;
}
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"))
{
2017-02-10 21:22:50 +00:00
string opnfile = File.ReadAllText(OpenFile.FileName);
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
customRTB.Text = opnfile;
this.Text = appName + NameWithotPath;
string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
customRTB.Select(Convert.ToInt32(cc2), 0);
return;
}
2017-02-09 21:42:08 +00:00
DecryptAES();
if (publicVar.okPressed == true)
{
publicVar.okPressed = false;
currentFilename = Path.GetFileName(OpenFile.FileName);
}
2017-02-05 20:47:31 +00:00
}
}
}
2017-02-09 21:42:08 +00:00
public void saveConfirm(bool exit)
2017-02-05 20:47:31 +00:00
{
2017-02-09 21:42:08 +00:00
if (customRTB.Modified == false)
2017-02-05 20:47:31 +00:00
{
2017-02-09 21:42:08 +00:00
if (exit == true)
2017-02-05 20:47:31 +00:00
{
2017-02-09 21:42:08 +00:00
Environment.Exit(0);
2017-02-05 20:47:31 +00:00
}
2017-02-09 21:42:08 +00:00
}
2017-02-05 20:47:31 +00:00
2017-02-09 21:42:08 +00:00
if (customRTB.Modified == true)
{
string noname = "Unnamed.cnp";
2017-02-05 20:47:31 +00:00
if (customRTB.Text != "")
{
DialogResult res = new DialogResult();
string messageBoxText = "";
if (publicVar.keyChanged == false)
{
2017-02-09 21:42:08 +00:00
messageBoxText = "Save file: " + "\"" + currentFilename + "\"" + " ? ";
2017-02-05 20:47:31 +00:00
}
if (publicVar.keyChanged == true)
{
2017-02-09 21:42:08 +00:00
messageBoxText = "Save file: " + "\"" + currentFilename + "\"" + " with a new key? ";
2017-02-05 20:47:31 +00:00
}
using (new CenterWinDialog(this))
{
res = MessageBox.Show(messageBoxText, "Crypto Notepad",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question);
if (res == DialogResult.Yes)
{
if (filename == noname)
{
SaveFile.FileName = noname;
saveAsToolStripMenuItem_Click(this, new EventArgs());
return;
}
if (filename != noname)
{
saveToolStripMenuItem1_Click_1(this, new EventArgs());
2017-02-09 21:42:08 +00:00
if (exit == true)
{
Environment.Exit(0);
}
2017-02-05 20:47:31 +00:00
}
}
if (res == DialogResult.No)
{
2017-02-09 21:42:08 +00:00
if (exit == true)
{
Environment.Exit(0);
}
2017-02-05 20:47:31 +00:00
}
if (res == DialogResult.Cancel)
{
2017-02-09 21:42:08 +00:00
noExit = true;
cancelPressed = true;
2017-02-05 20:47:31 +00:00
return;
}
}
}
}
}
2017-02-09 21:42:08 +00:00
2017-02-05 20:38:26 +00:00
private void customRTB_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
{
shiftPresed = false;
}
}
2016-01-06 14:47:38 +00:00
}
}