Rewritten "open file" function

This commit is contained in:
Sigmanor 2016-01-18 20:39:35 +02:00
parent fb03afd9e9
commit b0287da632
2 changed files with 91 additions and 152 deletions

View file

@ -159,7 +159,7 @@ public void InitializeComponent()
this.создатьToolStripMenuItem.Name = "создатьToolStripMenuItem"; this.создатьToolStripMenuItem.Name = "создатьToolStripMenuItem";
this.создатьToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N))); this.создатьToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N)));
this.создатьToolStripMenuItem.Size = new System.Drawing.Size(202, 22); this.создатьToolStripMenuItem.Size = new System.Drawing.Size(202, 22);
this.создатьToolStripMenuItem.Text = "New File"; this.создатьToolStripMenuItem.Text = "New";
this.создатьToolStripMenuItem.Click += new System.EventHandler(this.новыйToolStripMenuItem_Click_1); this.создатьToolStripMenuItem.Click += new System.EventHandler(this.новыйToolStripMenuItem_Click_1);
// //
// открытьToolStripMenuItem // открытьToolStripMenuItem
@ -167,7 +167,7 @@ public void InitializeComponent()
this.открытьToolStripMenuItem.Name = "открытьToolStripMenuItem"; this.открытьToolStripMenuItem.Name = "открытьToolStripMenuItem";
this.открытьToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O))); this.открытьToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O)));
this.открытьToolStripMenuItem.Size = new System.Drawing.Size(202, 22); this.открытьToolStripMenuItem.Size = new System.Drawing.Size(202, 22);
this.открытьToolStripMenuItem.Text = "Open File"; this.открытьToolStripMenuItem.Text = "Open...";
this.открытьToolStripMenuItem.Click += new System.EventHandler(this.открытьToolStripMenuItem_Click); this.открытьToolStripMenuItem.Click += new System.EventHandler(this.открытьToolStripMenuItem_Click);
// //
// toolStripSeparator11 // toolStripSeparator11
@ -357,14 +357,14 @@ public void InitializeComponent()
// changeKeyToolStripMenuItem // changeKeyToolStripMenuItem
// //
this.changeKeyToolStripMenuItem.Name = "changeKeyToolStripMenuItem"; this.changeKeyToolStripMenuItem.Name = "changeKeyToolStripMenuItem";
this.changeKeyToolStripMenuItem.Size = new System.Drawing.Size(139, 22); this.changeKeyToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.changeKeyToolStripMenuItem.Text = "Change Key"; this.changeKeyToolStripMenuItem.Text = "Change Key";
this.changeKeyToolStripMenuItem.Click += new System.EventHandler(this.changeKeyToolStripMenuItem_Click); this.changeKeyToolStripMenuItem.Click += new System.EventHandler(this.changeKeyToolStripMenuItem_Click);
// //
// settingsToolStripMenuItem // settingsToolStripMenuItem
// //
this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem"; this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem";
this.settingsToolStripMenuItem.Size = new System.Drawing.Size(139, 22); this.settingsToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.settingsToolStripMenuItem.Text = "Settings"; this.settingsToolStripMenuItem.Text = "Settings";
this.settingsToolStripMenuItem.Click += new System.EventHandler(this.settingsToolStripMenuItem_Click); this.settingsToolStripMenuItem.Click += new System.EventHandler(this.settingsToolStripMenuItem_Click);
// //
@ -493,11 +493,11 @@ public void InitializeComponent()
// //
// OpenFile // OpenFile
// //
this.OpenFile.Filter = "Crypto Notepad (*.enp)|*.enp|All files (*.*)|*.*"; this.OpenFile.Filter = "Crypto Notepad (*.cnp)|*.cnp|All files (*.*)|*.*";
// //
// SaveFile // SaveFile
// //
this.SaveFile.Filter = "Crypto Notepad (*.enp)|*.enp"; this.SaveFile.Filter = "Crypto Notepad (*.cnp)|*.cnp";
// //
// button1 // button1
// //

View file

@ -9,7 +9,7 @@
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Security.Cryptography;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -20,7 +20,7 @@ public partial class MainWindow : Form
{ {
[DllImport("user32.dll")] [DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, uint wMsg, UIntPtr wParam, IntPtr lParam); static extern int SendMessage(IntPtr hWnd, uint wMsg, UIntPtr wParam, IntPtr lParam);
public static string filename = "Unnamed.enp"; public static string filename = "Unnamed.cnp";
public static string key = ""; public static string key = "";
public static bool keyChanged = false; public static bool keyChanged = false;
public static bool settingsChanged = false; public static bool settingsChanged = false;
@ -65,96 +65,64 @@ void SaltMAC()
} }
#endregion #endregion
private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
{ void DecryptAES()
int Cikil = 0; {
Form2 f2 = new Form2(); Form2 f2 = new Form2();
f2.ShowDialog();
if (Form2.OkPressed == false)
{
return;
}
Form2.OkPressed = false;
try try
{ {
if (OpenFile.ShowDialog() != DialogResult.OK) return; string opnfile = File.ReadAllText(OpenFile.FileName);
{
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
string opnfile = File.ReadAllText(OpenFile.FileName);
if (OpenFile.FileName.Contains(".txt"))
{
customRTB.Text = opnfile;
toolStripStatusLabel1.Text = NameWithotPath;
toolStripStatusLabel1.ToolTipText = (OpenFile.FileName);
return;
}
f2.ShowDialog();
if (Form2.OkPressed == false)
{
return;
}
Form2.OkPressed = false;
string de = AES.Decrypt(opnfile, key, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize);
customRTB.Text = de;
toolStripStatusLabel1.Text = NameWithotPath;
toolStripStatusLabel1.ToolTipText = (OpenFile.FileName);
}
}
catch
{
string NameWithotPath = Path.GetFileName(OpenFile.FileName); string NameWithotPath = Path.GetFileName(OpenFile.FileName);
string opnfile = File.ReadAllText(OpenFile.FileName, Encoding.Default); string de = AES.Decrypt(opnfile, key, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize);
customRTB.Text = de;
toolStripStatusLabel1.Text = NameWithotPath; toolStripStatusLabel1.Text = NameWithotPath;
toolStripStatusLabel1.ToolTipText = (OpenFile.FileName); toolStripStatusLabel1.ToolTipText = (OpenFile.FileName);
do
{
Cikil = 0;
using (new CenterWinDialog(this))
{
DialogResult dialogResult = MessageBox.Show("Wrong key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry)
{
f2.ShowDialog();
if (Form2.OkPressed == false)
{
return;
}
Form2.OkPressed = false;
try
{
string de = AES.Decrypt(opnfile, key, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize);
customRTB.Text = de;
toolStripStatusLabel1.Text = NameWithotPath;
toolStripStatusLabel1.ToolTipText = (OpenFile.FileName);
}
catch
{
Cikil = 1;
}
}
if (dialogResult == DialogResult.Cancel)
{
toolStripStatusLabel1.Text = "Ready";
}
}
} while (Cikil == 1);
filename = OpenFile.FileName; filename = OpenFile.FileName;
textSave = false; textSave = false;
string cc = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture); string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
customRTB.Select(Convert.ToInt32(cc), 0); customRTB.Select(Convert.ToInt32(cc2), 0);
return; }
catch (CryptographicException)
{
using (new CenterWinDialog(this))
{
DialogResult dialogResult = MessageBox.Show("Wrong key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry)
{
DecryptAES();
}
}
} }
filename = OpenFile.FileName; }
textSave = false;
string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture); private void открытьToolStripMenuItem_Click(object sender, EventArgs e)
customRTB.Select(Convert.ToInt32(cc2), 0); {
if (OpenFile.ShowDialog() != DialogResult.OK) return;
{
if (OpenFile.FileName.Contains(".txt"))
{
string opnfile = File.ReadAllText(OpenFile.FileName);
string NameWithotPath = Path.GetFileName(OpenFile.FileName);
customRTB.Text = opnfile;
toolStripStatusLabel1.Text = NameWithotPath;
toolStripStatusLabel1.ToolTipText = (OpenFile.FileName);
string cc2 = customRTB.Text.Length.ToString(CultureInfo.InvariantCulture);
customRTB.Select(Convert.ToInt32(cc2), 0);
return;
}
DecryptAES();
}
} }
private void openAsotiations() private void openAsotiations()
@ -166,14 +134,15 @@ private void openAsotiations()
{ {
string NameWithotPath = Path.GetFileName(args[1]); string NameWithotPath = Path.GetFileName(args[1]);
string opnfile = File.ReadAllText(args[1]); string opnfile = File.ReadAllText(args[1]);
Form2.ShowDialog(); Form2.ShowDialog();
if (Form2.OkPressed == false) if (Form2.OkPressed == false)
{ {
return; return;
} }
Form2.OkPressed = false; Form2.OkPressed = false;
string de = AES.Decrypt(opnfile, key, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize);
string de = AES.Decrypt(opnfile, key, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize);
customRTB.Text = de; customRTB.Text = de;
toolStripStatusLabel1.Text = NameWithotPath; toolStripStatusLabel1.Text = NameWithotPath;
toolStripStatusLabel1.ToolTipText = (args[1]); toolStripStatusLabel1.ToolTipText = (args[1]);
@ -181,41 +150,11 @@ private void openAsotiations()
catch catch
{ {
string NameWithotPath = Path.GetFileName(args[1]);
string opnfile = File.ReadAllText(args[1]);
toolStripStatusLabel1.Text = NameWithotPath;
toolStripStatusLabel1.ToolTipText = (args[1]);
ComeHere:
DialogResult dialogResult = MessageBox.Show("Wrong key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error); DialogResult dialogResult = MessageBox.Show("Wrong key!", "Crypto Notepad", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry) if (dialogResult == DialogResult.Retry)
{ {
Form2.OkPressed = false; openAsotiations();
Form2.ShowDialog();
if (Form2.OkPressed == false)
{
return;
}
Form2.OkPressed = false;
try
{
string de = AES.Decrypt(opnfile, key, ps.TheSalt, ps.HashAlgorithm, ps.PasswordIterations, "16CHARSLONG12345", ps.KeySize);
customRTB.Text = de;
toolStripStatusLabel1.Text = NameWithotPath;
toolStripStatusLabel1.ToolTipText = (args[1]);
}
catch
{
goto ComeHere;
}
} }
if (dialogResult == DialogResult.Cancel)
{
Application.Exit();
}
} }
filename = args[1]; filename = args[1];
@ -309,7 +248,7 @@ private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
if (textSave == true) if (textSave == true)
{ {
string f = "Unnamed.enp"; string f = "Unnamed.cnp";
if (customRTB.Text != "") if (customRTB.Text != "")
{ {
DialogResult res = new DialogResult(); DialogResult res = new DialogResult();
@ -319,41 +258,41 @@ private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
"Crypto Notepad", "Crypto Notepad",
MessageBoxButtons.YesNoCancel, MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Question); MessageBoxIcon.Question);
if (res == DialogResult.Yes) if (res == DialogResult.Yes)
{
if (filename == f)
{
SaveFile.FileName = f;
сохранитьToolStripMenuItem_Click(this, new EventArgs());
}
if (filename != f)
{
сохранитьToolStripMenuItem1_Click_1(this, new EventArgs());
}
Environment.Exit(0);
}
try
{
if (res == DialogResult.No)
{ {
if (filename == f)
{
SaveFile.FileName = f;
сохранитьToolStripMenuItem_Click(this, new EventArgs());
}
if (filename != f)
{
сохранитьToolStripMenuItem1_Click_1(this, new EventArgs());
}
Environment.Exit(0); Environment.Exit(0);
} }
if (res == DialogResult.Cancel) try
{
if (res == DialogResult.No)
{
Environment.Exit(0);
}
if (res == DialogResult.Cancel)
{
e.Cancel = true;
}
}
catch
{ {
e.Cancel = true;
} }
} }
catch (Exception)
{
}
}
} }
} }
} }
@ -446,7 +385,7 @@ public void DeleteUpdateFiles()
private async void сохранитьToolStripMenuItem1_Click_1(object sender, EventArgs e) private async void сохранитьToolStripMenuItem1_Click_1(object sender, EventArgs e)
{ {
string f = "Unnamed.enp"; string f = "Unnamed.cnp";
string NameWithotPath = Path.GetFileName(OpenFile.FileName); string NameWithotPath = Path.GetFileName(OpenFile.FileName);
if (filename == f) if (filename == f)
@ -529,7 +468,7 @@ public void DeleteUpdateFiles()
private async void УдалитьФайлToolStripMenuItem_Click_1(object sender, EventArgs e) private async void УдалитьФайлToolStripMenuItem_Click_1(object sender, EventArgs e)
{ {
if (filename != "Unnamed.enp") if (filename != "Unnamed.cnp")
{ {
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
{ {
@ -540,14 +479,14 @@ public void DeleteUpdateFiles()
customRTB.Clear(); customRTB.Clear();
key = ""; key = "";
pictureBox11.Enabled = false; pictureBox11.Enabled = false;
filename = "Unnamed.enp"; filename = "Unnamed.cnp";
await Task.Delay(4000); await Task.Delay(4000);
toolStripStatusLabel1.Text = "Ready"; toolStripStatusLabel1.Text = "Ready";
return; return;
} }
} }
} }
if (filename == "Unnamed.enp") if (filename == "Unnamed.cnp")
{ {
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
{ {
@ -558,11 +497,11 @@ public void DeleteUpdateFiles()
private void открытьРасположениеФайлаToolStripMenuItem_Click(object sender, EventArgs e) private void открытьРасположениеФайлаToolStripMenuItem_Click(object sender, EventArgs e)
{ {
if (filename != "Unnamed.enp") if (filename != "Unnamed.cnp")
{ {
Process.Start("explorer.exe", @"/select, " + filename); Process.Start("explorer.exe", @"/select, " + filename);
} }
if (filename == "Unnamed.enp") if (filename == "Unnamed.cnp")
{ {
using (new CenterWinDialog(this)) using (new CenterWinDialog(this))
{ {