Encrypt/decrypt all file formats
This commit is contained in:
Alexander 2019-11-05 22:30:49 +02:00
parent a6d1e8caf2
commit 04117e65c7
3 changed files with 83 additions and 143 deletions

View file

@ -620,7 +620,7 @@ public void InitializeComponent()
//
// saveFileDialog
//
this.saveFileDialog.Filter = "Crypto Notepad (*.cnp)|*.cnp";
this.saveFileDialog.Filter = "Crypto Notepad (*.cnp)|*.cnp|Normal text file (*.txt)|*.txt|All Files (*.*)|*.*";
//
// searchPanel
//
@ -1118,13 +1118,13 @@ public void InitializeComponent()
//
this.fileLockedKeyTextBox.Location = new System.Drawing.Point(6, 34);
this.fileLockedKeyTextBox.Name = "fileLockedKeyTextBox";
this.fileLockedKeyTextBox.Size = new System.Drawing.Size(231, 22);
this.fileLockedKeyTextBox.TabIndex = 8;
this.fileLockedKeyTextBox.UseSystemPasswordChar = true;
this.fileLockedKeyTextBox.Placeholder = "Encryption key";
this.fileLockedKeyTextBox.PlaceholderActiveForeColor = System.Drawing.Color.DarkGray;
this.fileLockedKeyTextBox.PlaceholderFont = new System.Drawing.Font("Segoe UI", 8.25F);
this.fileLockedKeyTextBox.PlaceholderForeColor = System.Drawing.Color.DarkGray;
this.fileLockedKeyTextBox.Size = new System.Drawing.Size(231, 22);
this.fileLockedKeyTextBox.TabIndex = 8;
this.fileLockedKeyTextBox.UseSystemPasswordChar = true;
this.fileLockedKeyTextBox.TextChanged += new System.EventHandler(this.FileLockedKeyTextBox_TextChanged);
this.fileLockedKeyTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FileLockedKeyTextBox_KeyDown);
//
@ -1180,6 +1180,7 @@ public void InitializeComponent()
this.richTextBox.Text = "";
this.richTextBox.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.RichTextBox_LinkClicked);
this.richTextBox.SelectionChanged += new System.EventHandler(this.RichTextBox_SelectionChanged);
this.richTextBox.ModifiedChanged += new System.EventHandler(this.RichTextBox_ModifiedChanged);
this.richTextBox.TextChanged += new System.EventHandler(this.RichTextBox_TextChanged);
this.richTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.RichTextBox_KeyDown);
//

View file

@ -125,47 +125,22 @@ private void OpenAsotiations()
};
string fileExtension = Path.GetExtension(args[1]);
PublicVar.openFileName = Path.GetFileName(args[1]);
if (fileExtension == ".cnp")
openFileDialog.FileName = Path.GetFullPath(args[1]);
if (fileExtension != ".cnp")
{
try
{
string NameWithotPath = Path.GetFileName(args[1]);
string opnfile = File.ReadAllText(args[1]);
enterKeyForm.ShowDialog();
string de = AES.Decrypt(opnfile, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize));
richTextBox.Text = de;
Text = PublicVar.appName + " " + NameWithotPath;
filePath = args[1];
PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null;
StatusPanelFileInfo();
}
catch (CryptographicException)
{
TypedPassword.Value = null;
DialogResult dialogResult = MessageBox.Show(this, "Invalid key!", PublicVar.appName, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry)
DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.No)
{
OpenAsotiations();
string opnfile = File.ReadAllText(args[1]);
string NameWithotPath = Path.GetFileName(args[1]);
richTextBox.Text = opnfile;
filePath = args[1];
Text = PublicVar.appName + " " + NameWithotPath;
StatusPanelFileInfo();
return;
}
if (dialogResult == DialogResult.Cancel)
{
if (!Visible)
{
Application.Exit();
}
}
}
}
else
{
string opnfile = File.ReadAllText(args[1]);
string NameWithotPath = Path.GetFileName(args[1]);
richTextBox.Text = opnfile;
filePath = args[1];
Text = PublicVar.appName + " " + NameWithotPath;
StatusPanelFileInfo();
}
DecryptAES();
}
private void SendTo()
@ -176,108 +151,58 @@ private void SendTo()
StartPosition = FormStartPosition.CenterScreen
};
string fileExtension = Path.GetExtension(argsPath);
if (fileExtension == ".cnp")
openFileDialog.FileName = Path.GetFullPath(argsPath);
PublicVar.openFileName = Path.GetFileName(argsPath);
if (fileExtension != ".cnp")
{
try
DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.No)
{
string NameWithotPath = Path.GetFileName(argsPath);
string opnfile = File.ReadAllText(argsPath);
PublicVar.openFileName = Path.GetFileName(argsPath);
enterKeyForm.ShowDialog();
string de = AES.Decrypt(opnfile, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize));
richTextBox.Text = de;
Text = PublicVar.appName + " " + NameWithotPath;
string NameWithotPath = Path.GetFileName(argsPath);
richTextBox.Text = opnfile;
filePath = argsPath;
PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null;
Text = PublicVar.appName + " " + NameWithotPath;
StatusPanelFileInfo();
}
catch (CryptographicException)
{
TypedPassword.Value = null;
DialogResult dialogResult = MessageBox.Show(this, "Invalid key!", PublicVar.appName, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
if (dialogResult == DialogResult.Retry)
{
SendTo();
}
if (dialogResult == DialogResult.Cancel)
{
if (!Visible)
{
Application.Exit();
}
}
return;
}
}
else
{
string opnfile = File.ReadAllText(argsPath);
string NameWithotPath = Path.GetFileName(argsPath);
richTextBox.Text = opnfile;
Text = PublicVar.appName + " " + NameWithotPath;
}
DecryptAES();
}
private void ContextMenuEncryptReplace()
{
if (args[1].Contains(".cnp"))
{
MessageBox.Show(this, "Looks like this file is already encrypted", PublicVar.appName, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
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)
{
Environment.Exit(0);
}
if (!args[1].Contains(".cnp"))
string opnfile = File.ReadAllText(args[1]);
richTextBox.Text = opnfile;
PublicVar.openFileName = Path.GetFileName(args[1]);
string newFile = Path.GetDirectoryName(args[1]) + @"\" + Path.GetFileNameWithoutExtension(args[1]) + ".cnp";
EnterKeyForm enterKeyForm = new EnterKeyForm
{
string opnfile = File.ReadAllText(args[1]);
richTextBox.Text = opnfile;
PublicVar.openFileName = Path.GetFileName(args[1]);
string newFile = Path.GetDirectoryName(args[1]) + @"\" + Path.GetFileNameWithoutExtension(args[1]) + ".cnp";
EnterKeyForm enterKeyForm = new EnterKeyForm
{
Owner = this
};
enterKeyForm.ShowDialog();
File.Delete(args[1]);
string noenc = richTextBox.Text;
string en;
en = AES.Encrypt(richTextBox.Text, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize));
richTextBox.Text = en;
StreamWriter sw = new StreamWriter(newFile);
int i = richTextBox.Lines.Count();
int j = 0;
i = i - 1;
while (j <= i)
{
sw.WriteLine(richTextBox.Lines.GetValue(j).ToString());
j = j + 1;
}
sw.Close();
PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null;
filePath = newFile;
PublicVar.openFileName = Path.GetFileName(newFile);
Text = PublicVar.appName + " " + PublicVar.openFileName;
richTextBox.Text = noenc;
StatusPanelFileInfo();
}
richTextBox.Modified = false;
}
private void ContextMenuEncrypt()
{
if (!args[1].Contains(".cnp"))
Owner = this
};
enterKeyForm.ShowDialog();
File.Delete(args[1]);
string noenc = richTextBox.Text;
string en;
en = AES.Encrypt(richTextBox.Text, TypedPassword.Value, null, settings.HashAlgorithm, Convert.ToInt32(settings.PasswordIterations), Convert.ToInt32(settings.KeySize));
richTextBox.Text = en;
using (StreamWriter writer = new StreamWriter(newFile))
{
string opnfile = File.ReadAllText(args[1]);
string NameWithotPath = Path.GetFileName(args[1]);
richTextBox.Text = opnfile;
Text = PublicVar.appName + NameWithotPath;
PublicVar.openFileName = Path.GetFileName(args[1]);
filePath = openFileDialog.FileName;
writer.Write(richTextBox.Text);
writer.Close();
}
PublicVar.encryptionKey.Set(TypedPassword.Value);
TypedPassword.Value = null;
filePath = newFile;
PublicVar.openFileName = Path.GetFileName(newFile);
Text = PublicVar.appName + " " + PublicVar.openFileName;
richTextBox.Text = noenc;
StatusPanelFileInfo();
richTextBox.Modified = false;
}
@ -906,17 +831,24 @@ private void RichTextBox_DragDrop(object sender, DragEventArgs e)
{
if (!openFileDialog.FileName.Contains(".cnp"))
{
string opnfile = File.ReadAllText(openFileDialog.FileName);
string NameWithotPath = Path.GetFileName(openFileDialog.FileName);
richTextBox.Text = opnfile;
Text = PublicVar.appName + " " + NameWithotPath;
filePath = openFileDialog.FileName;
StatusPanelFileInfo();
return;
if (Visible)
{
PublicVar.messageBoxCenterParent = true;
}
using (new CenterWinDialog(this))
{
DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.No)
{
string opnfile = File.ReadAllText(openFileDialog.FileName);
string NameWithotPath = Path.GetFileName(openFileDialog.FileName);
richTextBox.Text = opnfile;
Text = PublicVar.appName + " " + NameWithotPath;
filePath = openFileDialog.FileName;
StatusPanelFileInfo();
return;
}
}
}
DecryptAES();
}
@ -1024,13 +956,20 @@ private void OpenMainMenu_Click(object sender, EventArgs e)
PublicVar.openFileName = Path.GetFileName(openFileDialog.FileName);
if (!openFileDialog.FileName.Contains(".cnp"))
{
string opnfile = File.ReadAllText(openFileDialog.FileName);
string NameWithotPath = Path.GetFileName(openFileDialog.FileName);
richTextBox.Text = opnfile;
Text = PublicVar.appName + " " + NameWithotPath;
filePath = openFileDialog.FileName;
StatusPanelFileInfo();
return;
using (new CenterWinDialog(this))
{
DialogResult res = MessageBox.Show(this, "Try to decrypt \"" + PublicVar.openFileName + "\" file?", PublicVar.appName, MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (res == DialogResult.No)
{
string opnfile = File.ReadAllText(openFileDialog.FileName);
string NameWithotPath = Path.GetFileName(openFileDialog.FileName);
richTextBox.Text = opnfile;
Text = PublicVar.appName + " " + NameWithotPath;
filePath = openFileDialog.FileName;
StatusPanelFileInfo();
return;
}
}
}
DecryptAES();
richTextBox.Modified = false;

View file

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