KRY-0x04/LogicHandler.cs

346 lines
14 KiB
C#

using System;
using System.IO;
using System.IO.Compression;
using System.Numerics;
using System.Windows.Forms;
namespace KRY_0x04
{
class LogicHandler
{
Dsapls dsapls = new Dsapls();
internal void load_msg(RichTextBox msgbox, TextBox msgpathbox, TextBox msgdetailsbox)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "pick a msg";
/* freedom for all */
dlg.Filter = "message (*.msg)|*.msg|custom (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
msgbox.Text = File.ReadAllText(dlg.FileName);
msgpathbox.Text = dlg.FileName;
string f = dlg.FileName;
msgdetailsbox.Text = dsapls.get_file_metrics(f);
}
}
}
internal void save_msg(RichTextBox msgbox)
{
if (msgbox.Text == "")
{
MessageBox.Show("you've nothing to dump, friend ()");
return;
}
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
saveFileDialog1.Title = "export msg";
saveFileDialog1.CheckFileExists = false;
saveFileDialog1.CheckPathExists = false;
saveFileDialog1.DefaultExt = "";
saveFileDialog1.Filter = "msg (*.msg)|*.msg|any file you wish (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
File.WriteAllText(saveFileDialog1.FileName, msgbox.Text);
MessageBox.Show($"saved to {saveFileDialog1.FileName}", "gj, file saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("error: " + ex.ToString(), "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
internal void compute_sha256(TextBox msgpathbox, TextBox computedshabox)
{
if (msgpathbox.Text != "")
{
computedshabox.Text = dsapls.sha256sum(msgpathbox.Text);
computedshabox.BorderStyle = BorderStyle.Fixed3D;
}
else
{
MessageBox.Show("please first load a file, thanks", "why are you doing this to me");
return;
}
}
internal void load_privkey(RichTextBox privkeybox, TextBox privkeypathbox)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "pick a privkey";
/* freedom for all */
dlg.Filter = "private key (*.pri)|*.pri|custom (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
privkeybox.Text = File.ReadAllText(dlg.FileName);
privkeypathbox.Text = dlg.FileName;
}
}
}
internal void load_pubkey(RichTextBox pubkeybox, TextBox pubkeypathbox)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "pick a pubkey";
/* freedom for all */
dlg.Filter = "public key (*.pub)|*.pub|custom (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
pubkeybox.Text = File.ReadAllText(dlg.FileName);
pubkeypathbox.Text = dlg.FileName;
}
}
}
internal void save_sha256(TextBox computedshabox)
{
if (computedshabox.Text == "")
{
MessageBox.Show("you've nothing to dump, friend ()");
return;
}
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
saveFileDialog.Title = "export checksum";
saveFileDialog.CheckFileExists = false;
saveFileDialog.CheckPathExists = false;
saveFileDialog.DefaultExt = "";
saveFileDialog.Filter = "sha256sum (*.sha256sum)|*.sha256sum|any file you wish (*.*)|*.*";
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
File.WriteAllText(saveFileDialog.FileName, computedshabox.Text);
MessageBox.Show($"saved to {saveFileDialog.FileName}", "gj, file saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("error: " + ex.ToString(), "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
internal void load_sha256(TextBox loadedshabox)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "pick a sha256sum";
/* freedom for all */
dlg.Filter = "sha256sum (*.sha256sum)|*.sha256sum|custom (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
loadedshabox.Text = File.ReadAllText(dlg.FileName);
}
}
}
internal void do_zip(RichTextBox msgbox, TextBox msgpathbox)
{
if (msgpathbox.Text == "")
{
MessageBox.Show("load a msg file first, please", "oh hey");
return;
}
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
dlg.Description = "choose a folder to zip";
if (dlg.ShowDialog() == DialogResult.OK)
{
string pathTo_dirToZip = dlg.SelectedPath;
using (SaveFileDialog dlggg = new SaveFileDialog())
{
dlggg.Title = "where to save text of earlier specified msg";
dlggg.Filter = "msg in windows-recognizable text file ending with \"txt\" (*.txt)|*.txt|custom (*.*)|*.*";
if (dlggg.ShowDialog() == DialogResult.OK)
{
string path_to_msg = dlggg.FileName;
if (path_to_msg == "")
{
MessageBox.Show("please specify a path first", "not this again, come on");
return;
}
Directory.CreateDirectory(pathTo_dirToZip);
/* TODO - needs fixing */
File.WriteAllText(path_to_msg, msgbox.Text);
try
{
if (File.Exists(pathTo_dirToZip + "zipfile.zip"))
{
MessageBox.Show($"file exists: {pathTo_dirToZip + "zipfile.zip"}\ndeleting", "ayy");
File.Delete(pathTo_dirToZip + "zipfile.zip");
}
ZipFile.CreateFromDirectory(pathTo_dirToZip, pathTo_dirToZip + "zipfile.zip");
MessageBox.Show("great success!", "gj, zip created");
}
catch (IOException exception)
{
MessageBox.Show("error: " + exception.ToString(), "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
}
internal void do_unzip()
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "choose a zip file to unzip";
dlg.Filter = "zip (*.zip)|*.zip|custom (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
string zipfile = dlg.FileName;
using (FolderBrowserDialog dlgg = new FolderBrowserDialog())
{
dlgg.Description = "select folder to unzip the zip to yeeeaaah";
if (dlgg.ShowDialog() == DialogResult.OK)
{
string pathTo_UnzippedDir = dlgg.SelectedPath;
if (Directory.Exists(pathTo_UnzippedDir))
{
pathTo_UnzippedDir = pathTo_UnzippedDir + "-NEW-" + DateTime.Now.ToString("yyyyMMddTHHmmss");
MessageBox.Show("dir exists, created a new one with the same name + date", "just so you know");
Directory.CreateDirectory(pathTo_UnzippedDir);
}
try
{
ZipFile.ExtractToDirectory(zipfile, pathTo_UnzippedDir);
MessageBox.Show("great success!", "gj, archive unzipped");
}
catch (IOException exc)
{
MessageBox.Show("error: " + exc.ToString(), "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
}
internal void do_sign(RichTextBox msgbox, RichTextBox privkeybox, RichTextBox pubkeybox, TextBox computedshabox)
{
if (msgbox.Text == "" || privkeybox.Text == "" || pubkeybox.Text == "" || computedshabox.Text == "")
{
MessageBox.Show("please, fill in all of the required fields");
return;
}
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string[] keys = privkeybox.Text.Split(' ');
MessageBox.Show("we good?");
Console.WriteLine(BigInteger.Parse(keys[1]));
Console.WriteLine(BigInteger.Parse(keys[0]));
string signed = Convert.ToString(BigInteger.ModPow(dsapls.return_bigint_representation_of_message(computedshabox.Text), BigInteger.Parse(keys[1]), BigInteger.Parse(keys[0])));
saveFileDialog1.Title = ".sign";
saveFileDialog1.CheckFileExists = false;
saveFileDialog1.CheckPathExists = false;
saveFileDialog1.DefaultExt = "";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
File.WriteAllText(saveFileDialog1.FileName, signed);
MessageBox.Show($"saved to {saveFileDialog1.FileName}", "gj, file saved!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("error: " + ex.ToString(), "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
internal void do_verify(RichTextBox pubkeybox)
{
string[] keys = pubkeybox.Text.Split(' ');
string verified = "";
string orig_message = "";
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "pick a .sign file";
/* freedom for all */
dlg.Filter = "sign file (*.sign)|*.sign|custom (*.*)|*.*";
MessageBox.Show("we still good");
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
verified = dsapls.return_string_representation_of_bigint(BigInteger.ModPow(BigInteger.Parse(File.ReadAllText(dlg.FileName)), BigInteger.Parse(keys[1]), BigInteger.Parse(keys[0])));
}
catch (Exception excep)
{
MessageBox.Show("error: " + excep.ToString(), "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "pick a .msg file";
/* freedom for all */
dlg.Filter = "msg file (*.msg)|*.msg|custom (*.*)|*.*";
if (dlg.ShowDialog() == DialogResult.OK)
{
try
{
orig_message = dlg.FileName;
}
catch (Exception excep)
{
MessageBox.Show("error: " + excep.ToString(), "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
if (dsapls.sha256sum(orig_message) == verified)
{
MessageBox.Show($"{dsapls.sha256sum(orig_message)} == {verified}", "we good");
}
else
{
Console.WriteLine($"orig message: {orig_message}");
Console.WriteLine($"verif message: {verified}");
MessageBox.Show($"something is seriously wrong\n{dsapls.sha256sum(orig_message)} != {verified}", "checksums differ!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}