merge: finally bring in 'feature-ng' greatness
* this merge concludes the works on a next-gen (ng) version of this program (kry-0x04 - dsa). * added * keygen capabilities (from kry-0x03) * sha256sum autocompute on msg text changed * fixed * signature creation * signature verification * zip/unzip functionality * clear_all function * further included * design and ux enhancements * code cleanup for improved legibility * design-logic decoupling * feature-ng: (25 commits) chore: add "keygen running" indicator label feat: add the option to save keys to a folder chore: add Rsa.cs for keygen capabilities feat: unzip functioning correctly refactor: get rid of the using chore: clear msgbox first, sha256sumbox after that chore: zip creation parameter tweaks chore: smarten do_zip(); use disposable workirs feat: legibility++; bugginess-- in do_zip() chore: delete the folder after the fact chore: creating a zip with at least the msg inside chore: refactor do_zip() chore: rm unused method chore: show sha256sum even for an empty string fix: handle the label when loading a new message chore: refactor LogicHandler feat: refactor do_verify() feat: autocompute sha on text changed chore: rm border style handling chore: rm checksum loading capability + renames ...
This commit is contained in:
commit
a6b87453ff
|
@ -0,0 +1,84 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Numerics;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace KRY_0x04
|
||||
{
|
||||
class Dsapls
|
||||
{
|
||||
internal string sha256sum_from_path(string path)
|
||||
{
|
||||
string sha256sum = "";
|
||||
try
|
||||
{
|
||||
FileStream my_filestream = File.OpenRead(path);
|
||||
SHA256Managed my_sha_256_managed = new SHA256Managed();
|
||||
byte[] byte_array_of_sha256 = my_sha_256_managed.ComputeHash(my_filestream);
|
||||
sha256sum = BitConverter.ToString(byte_array_of_sha256).Replace("-", string.Empty).ToLower();
|
||||
|
||||
my_filestream.Close();
|
||||
my_sha_256_managed.Clear();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
show_exc_msgbox(e.ToString());
|
||||
}
|
||||
return sha256sum;
|
||||
}
|
||||
|
||||
|
||||
internal string sha256sum_from_string(string text)
|
||||
{
|
||||
string sha256sum = "";
|
||||
try
|
||||
{
|
||||
SHA256Managed sha256_provider = new SHA256Managed();
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(text);
|
||||
byte[] hash = sha256_provider.ComputeHash(bytes);
|
||||
sha256sum = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
show_exc_msgbox(e.ToString());
|
||||
}
|
||||
return sha256sum;
|
||||
}
|
||||
|
||||
|
||||
internal string get_file_metrics(string filename)
|
||||
{
|
||||
string metrics = "";
|
||||
metrics += $"created: " + $"{File.GetCreationTime(filename)}" + Environment.NewLine;
|
||||
metrics += $"file type: " + $"{Path.GetExtension(filename)}";
|
||||
return metrics;
|
||||
}
|
||||
|
||||
|
||||
internal BigInteger return_bigint_representation_of_message(string input)
|
||||
{
|
||||
byte[] part_of_message = Encoding.ASCII.GetBytes(input);
|
||||
return new BigInteger(part_of_message);
|
||||
}
|
||||
|
||||
|
||||
internal string return_string_representation_of_bigint(BigInteger bA_number)
|
||||
{
|
||||
byte[] decrypted_b_a = bA_number.ToByteArray();
|
||||
string decrypted_text = "";
|
||||
for (int i = 0; i < decrypted_b_a.Length; i++)
|
||||
{
|
||||
decrypted_text += Convert.ToString(Convert.ToChar(decrypted_b_a[i]));
|
||||
}
|
||||
return decrypted_text;
|
||||
}
|
||||
|
||||
|
||||
private void show_exc_msgbox(string e)
|
||||
{
|
||||
MessageBox.Show("https://xkcd.com/2200 \nerror: " + e, "this should never have happened", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -28,36 +28,41 @@
|
|||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
|
||||
this.msgbox = new System.Windows.Forms.RichTextBox();
|
||||
this.button1 = new System.Windows.Forms.Button();
|
||||
this.textBox1 = new System.Windows.Forms.TextBox();
|
||||
this.button2 = new System.Windows.Forms.Button();
|
||||
this.msgpathbox = new System.Windows.Forms.TextBox();
|
||||
this.button3 = new System.Windows.Forms.Button();
|
||||
this.button4 = new System.Windows.Forms.Button();
|
||||
this.button5 = new System.Windows.Forms.Button();
|
||||
this.richTextBox2 = new System.Windows.Forms.RichTextBox();
|
||||
this.richTextBox3 = new System.Windows.Forms.RichTextBox();
|
||||
this.textBox2 = new System.Windows.Forms.TextBox();
|
||||
this.privkeybox = new System.Windows.Forms.RichTextBox();
|
||||
this.pubkeybox = new System.Windows.Forms.RichTextBox();
|
||||
this.sha256sumbox = new System.Windows.Forms.TextBox();
|
||||
this.button6 = new System.Windows.Forms.Button();
|
||||
this.button7 = new System.Windows.Forms.Button();
|
||||
this.textBox3 = new System.Windows.Forms.TextBox();
|
||||
this.button8 = new System.Windows.Forms.Button();
|
||||
this.button9 = new System.Windows.Forms.Button();
|
||||
this.button10 = new System.Windows.Forms.Button();
|
||||
this.button12 = new System.Windows.Forms.Button();
|
||||
this.button13 = new System.Windows.Forms.Button();
|
||||
this.textBox5 = new System.Windows.Forms.TextBox();
|
||||
this.textBox6 = new System.Windows.Forms.TextBox();
|
||||
this.textBox4 = new System.Windows.Forms.TextBox();
|
||||
this.privkeypathbox = new System.Windows.Forms.TextBox();
|
||||
this.pubkeypathbox = new System.Windows.Forms.TextBox();
|
||||
this.msgdetailsbox = new System.Windows.Forms.TextBox();
|
||||
this.msgs_differ_label = new System.Windows.Forms.Label();
|
||||
this.keygenbutton = new System.Windows.Forms.Button();
|
||||
this.threadsbox = new System.Windows.Forms.TextBox();
|
||||
this.keysizebutton = new System.Windows.Forms.Button();
|
||||
this.label1 = new System.Windows.Forms.Label();
|
||||
this.label2 = new System.Windows.Forms.Label();
|
||||
this.label_threads_warning = new System.Windows.Forms.Label();
|
||||
this.label_keygen_running = new System.Windows.Forms.Label();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// richTextBox1
|
||||
// msgbox
|
||||
//
|
||||
this.richTextBox1.Location = new System.Drawing.Point(12, 12);
|
||||
this.richTextBox1.Name = "richTextBox1";
|
||||
this.richTextBox1.Size = new System.Drawing.Size(437, 102);
|
||||
this.richTextBox1.TabIndex = 5;
|
||||
this.richTextBox1.Text = "";
|
||||
this.msgbox.Location = new System.Drawing.Point(12, 12);
|
||||
this.msgbox.Name = "msgbox";
|
||||
this.msgbox.Size = new System.Drawing.Size(437, 102);
|
||||
this.msgbox.TabIndex = 5;
|
||||
this.msgbox.Text = "";
|
||||
this.msgbox.TextChanged += new System.EventHandler(this.msgbox_TextChanged);
|
||||
//
|
||||
// button1
|
||||
//
|
||||
|
@ -67,25 +72,15 @@
|
|||
this.button1.TabIndex = 0;
|
||||
this.button1.Text = "load msg";
|
||||
this.button1.UseVisualStyleBackColor = true;
|
||||
this.button1.Click += new System.EventHandler(this.button1_Click);
|
||||
this.button1.Click += new System.EventHandler(this.load_msg_button_Click);
|
||||
//
|
||||
// textBox1
|
||||
// msgpathbox
|
||||
//
|
||||
this.textBox1.Location = new System.Drawing.Point(12, 120);
|
||||
this.textBox1.Name = "textBox1";
|
||||
this.textBox1.ReadOnly = true;
|
||||
this.textBox1.Size = new System.Drawing.Size(437, 20);
|
||||
this.textBox1.TabIndex = 2;
|
||||
//
|
||||
// button2
|
||||
//
|
||||
this.button2.Location = new System.Drawing.Point(467, 229);
|
||||
this.button2.Name = "button2";
|
||||
this.button2.Size = new System.Drawing.Size(75, 23);
|
||||
this.button2.TabIndex = 6;
|
||||
this.button2.Text = "re-compute";
|
||||
this.button2.UseVisualStyleBackColor = true;
|
||||
this.button2.Click += new System.EventHandler(this.button2_Click);
|
||||
this.msgpathbox.Location = new System.Drawing.Point(12, 120);
|
||||
this.msgpathbox.Name = "msgpathbox";
|
||||
this.msgpathbox.ReadOnly = true;
|
||||
this.msgpathbox.Size = new System.Drawing.Size(437, 20);
|
||||
this.msgpathbox.TabIndex = 2;
|
||||
//
|
||||
// button3
|
||||
//
|
||||
|
@ -95,7 +90,7 @@
|
|||
this.button3.TabIndex = 7;
|
||||
this.button3.Text = "load privkey";
|
||||
this.button3.UseVisualStyleBackColor = true;
|
||||
this.button3.Click += new System.EventHandler(this.button3_Click);
|
||||
this.button3.Click += new System.EventHandler(this.load_privkey_button_Click);
|
||||
//
|
||||
// button4
|
||||
//
|
||||
|
@ -105,7 +100,7 @@
|
|||
this.button4.TabIndex = 8;
|
||||
this.button4.Text = "load pubkey";
|
||||
this.button4.UseVisualStyleBackColor = true;
|
||||
this.button4.Click += new System.EventHandler(this.button4_Click);
|
||||
this.button4.Click += new System.EventHandler(this.load_pubkey_button_Click);
|
||||
//
|
||||
// button5
|
||||
//
|
||||
|
@ -115,35 +110,35 @@
|
|||
this.button5.TabIndex = 9;
|
||||
this.button5.Text = "save msg";
|
||||
this.button5.UseVisualStyleBackColor = true;
|
||||
this.button5.Click += new System.EventHandler(this.button5_Click);
|
||||
this.button5.Click += new System.EventHandler(this.save_msg_button_Click);
|
||||
//
|
||||
// richTextBox2
|
||||
// privkeybox
|
||||
//
|
||||
this.richTextBox2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.richTextBox2.Location = new System.Drawing.Point(12, 309);
|
||||
this.richTextBox2.Name = "richTextBox2";
|
||||
this.richTextBox2.ReadOnly = true;
|
||||
this.richTextBox2.Size = new System.Drawing.Size(437, 55);
|
||||
this.richTextBox2.TabIndex = 10;
|
||||
this.richTextBox2.Text = "";
|
||||
this.privkeybox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.privkeybox.Location = new System.Drawing.Point(12, 309);
|
||||
this.privkeybox.Name = "privkeybox";
|
||||
this.privkeybox.ReadOnly = true;
|
||||
this.privkeybox.Size = new System.Drawing.Size(437, 55);
|
||||
this.privkeybox.TabIndex = 10;
|
||||
this.privkeybox.Text = "";
|
||||
//
|
||||
// richTextBox3
|
||||
// pubkeybox
|
||||
//
|
||||
this.richTextBox3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.richTextBox3.Location = new System.Drawing.Point(12, 401);
|
||||
this.richTextBox3.Name = "richTextBox3";
|
||||
this.richTextBox3.ReadOnly = true;
|
||||
this.richTextBox3.Size = new System.Drawing.Size(437, 55);
|
||||
this.richTextBox3.TabIndex = 11;
|
||||
this.richTextBox3.Text = "";
|
||||
this.pubkeybox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
|
||||
this.pubkeybox.Location = new System.Drawing.Point(12, 401);
|
||||
this.pubkeybox.Name = "pubkeybox";
|
||||
this.pubkeybox.ReadOnly = true;
|
||||
this.pubkeybox.Size = new System.Drawing.Size(437, 55);
|
||||
this.pubkeybox.TabIndex = 11;
|
||||
this.pubkeybox.Text = "";
|
||||
//
|
||||
// textBox2
|
||||
// sha256sumbox
|
||||
//
|
||||
this.textBox2.Location = new System.Drawing.Point(12, 203);
|
||||
this.textBox2.Name = "textBox2";
|
||||
this.textBox2.ReadOnly = true;
|
||||
this.textBox2.Size = new System.Drawing.Size(437, 20);
|
||||
this.textBox2.TabIndex = 12;
|
||||
this.sha256sumbox.Location = new System.Drawing.Point(12, 203);
|
||||
this.sha256sumbox.Name = "sha256sumbox";
|
||||
this.sha256sumbox.ReadOnly = true;
|
||||
this.sha256sumbox.Size = new System.Drawing.Size(437, 20);
|
||||
this.sha256sumbox.TabIndex = 12;
|
||||
//
|
||||
// button6
|
||||
//
|
||||
|
@ -153,7 +148,7 @@
|
|||
this.button6.TabIndex = 13;
|
||||
this.button6.Text = "sign";
|
||||
this.button6.UseVisualStyleBackColor = true;
|
||||
this.button6.Click += new System.EventHandler(this.button6_Click);
|
||||
this.button6.Click += new System.EventHandler(this.sign_button_Click);
|
||||
//
|
||||
// button7
|
||||
//
|
||||
|
@ -163,27 +158,7 @@
|
|||
this.button7.TabIndex = 14;
|
||||
this.button7.Text = "verify";
|
||||
this.button7.UseVisualStyleBackColor = true;
|
||||
this.button7.Click += new System.EventHandler(this.button7_Click);
|
||||
//
|
||||
// textBox3
|
||||
//
|
||||
this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.None;
|
||||
this.textBox3.Location = new System.Drawing.Point(12, 229);
|
||||
this.textBox3.Name = "textBox3";
|
||||
this.textBox3.ReadOnly = true;
|
||||
this.textBox3.Size = new System.Drawing.Size(437, 13);
|
||||
this.textBox3.TabIndex = 15;
|
||||
//
|
||||
// button8
|
||||
//
|
||||
this.button8.Location = new System.Drawing.Point(558, 229);
|
||||
this.button8.Name = "button8";
|
||||
this.button8.Size = new System.Drawing.Size(75, 23);
|
||||
this.button8.TabIndex = 16;
|
||||
this.button8.Tag = "";
|
||||
this.button8.Text = "save sha";
|
||||
this.button8.UseVisualStyleBackColor = true;
|
||||
this.button8.Click += new System.EventHandler(this.button8_Click);
|
||||
this.button7.Click += new System.EventHandler(this.verify_button_Click);
|
||||
//
|
||||
// button9
|
||||
//
|
||||
|
@ -193,7 +168,7 @@
|
|||
this.button9.TabIndex = 17;
|
||||
this.button9.Text = "zip";
|
||||
this.button9.UseVisualStyleBackColor = true;
|
||||
this.button9.Click += new System.EventHandler(this.button9_Click);
|
||||
this.button9.Click += new System.EventHandler(this.zip_button_Click);
|
||||
//
|
||||
// button10
|
||||
//
|
||||
|
@ -203,7 +178,7 @@
|
|||
this.button10.TabIndex = 18;
|
||||
this.button10.Text = "unzip";
|
||||
this.button10.UseVisualStyleBackColor = true;
|
||||
this.button10.Click += new System.EventHandler(this.button10_Click);
|
||||
this.button10.Click += new System.EventHandler(this.unzip_button_Click);
|
||||
//
|
||||
// button12
|
||||
//
|
||||
|
@ -213,69 +188,142 @@
|
|||
this.button12.TabIndex = 21;
|
||||
this.button12.Text = "clean ALL";
|
||||
this.button12.UseVisualStyleBackColor = true;
|
||||
this.button12.Click += new System.EventHandler(this.button12_Click);
|
||||
this.button12.Click += new System.EventHandler(this.cleanALL_button_Click);
|
||||
//
|
||||
// button13
|
||||
// privkeypathbox
|
||||
//
|
||||
this.button13.Location = new System.Drawing.Point(467, 203);
|
||||
this.button13.Name = "button13";
|
||||
this.button13.Size = new System.Drawing.Size(75, 23);
|
||||
this.button13.TabIndex = 22;
|
||||
this.button13.Text = "load sha256";
|
||||
this.button13.UseVisualStyleBackColor = true;
|
||||
this.button13.Click += new System.EventHandler(this.button13_Click);
|
||||
this.privkeypathbox.Location = new System.Drawing.Point(12, 370);
|
||||
this.privkeypathbox.Name = "privkeypathbox";
|
||||
this.privkeypathbox.ReadOnly = true;
|
||||
this.privkeypathbox.Size = new System.Drawing.Size(437, 20);
|
||||
this.privkeypathbox.TabIndex = 23;
|
||||
//
|
||||
// textBox5
|
||||
// pubkeypathbox
|
||||
//
|
||||
this.textBox5.Location = new System.Drawing.Point(12, 370);
|
||||
this.textBox5.Name = "textBox5";
|
||||
this.textBox5.ReadOnly = true;
|
||||
this.textBox5.Size = new System.Drawing.Size(437, 20);
|
||||
this.textBox5.TabIndex = 23;
|
||||
this.pubkeypathbox.Location = new System.Drawing.Point(12, 462);
|
||||
this.pubkeypathbox.Name = "pubkeypathbox";
|
||||
this.pubkeypathbox.ReadOnly = true;
|
||||
this.pubkeypathbox.Size = new System.Drawing.Size(437, 20);
|
||||
this.pubkeypathbox.TabIndex = 24;
|
||||
//
|
||||
// textBox6
|
||||
// msgdetailsbox
|
||||
//
|
||||
this.textBox6.Location = new System.Drawing.Point(12, 462);
|
||||
this.textBox6.Name = "textBox6";
|
||||
this.textBox6.ReadOnly = true;
|
||||
this.textBox6.Size = new System.Drawing.Size(437, 20);
|
||||
this.textBox6.TabIndex = 24;
|
||||
this.msgdetailsbox.Location = new System.Drawing.Point(12, 146);
|
||||
this.msgdetailsbox.Multiline = true;
|
||||
this.msgdetailsbox.Name = "msgdetailsbox";
|
||||
this.msgdetailsbox.ReadOnly = true;
|
||||
this.msgdetailsbox.Size = new System.Drawing.Size(437, 51);
|
||||
this.msgdetailsbox.TabIndex = 25;
|
||||
//
|
||||
// textBox4
|
||||
// msgs_differ_label
|
||||
//
|
||||
this.textBox4.Location = new System.Drawing.Point(12, 146);
|
||||
this.textBox4.Multiline = true;
|
||||
this.textBox4.Name = "textBox4";
|
||||
this.textBox4.ReadOnly = true;
|
||||
this.textBox4.Size = new System.Drawing.Size(437, 51);
|
||||
this.textBox4.TabIndex = 25;
|
||||
this.msgs_differ_label.AutoSize = true;
|
||||
this.msgs_differ_label.BackColor = System.Drawing.SystemColors.HighlightText;
|
||||
this.msgs_differ_label.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
||||
this.msgs_differ_label.ForeColor = System.Drawing.Color.Firebrick;
|
||||
this.msgs_differ_label.Location = new System.Drawing.Point(464, 204);
|
||||
this.msgs_differ_label.Name = "msgs_differ_label";
|
||||
this.msgs_differ_label.Size = new System.Drawing.Size(198, 16);
|
||||
this.msgs_differ_label.TabIndex = 26;
|
||||
this.msgs_differ_label.Text = "msg changed but not saved";
|
||||
this.msgs_differ_label.Visible = false;
|
||||
//
|
||||
// keygenbutton
|
||||
//
|
||||
this.keygenbutton.Location = new System.Drawing.Point(653, 473);
|
||||
this.keygenbutton.Name = "keygenbutton";
|
||||
this.keygenbutton.Size = new System.Drawing.Size(75, 23);
|
||||
this.keygenbutton.TabIndex = 27;
|
||||
this.keygenbutton.Text = "keygen";
|
||||
this.keygenbutton.UseVisualStyleBackColor = true;
|
||||
this.keygenbutton.Click += new System.EventHandler(this.keygen_button_Click);
|
||||
//
|
||||
// threadsbox
|
||||
//
|
||||
this.threadsbox.Location = new System.Drawing.Point(734, 435);
|
||||
this.threadsbox.Name = "threadsbox";
|
||||
this.threadsbox.Size = new System.Drawing.Size(39, 20);
|
||||
this.threadsbox.TabIndex = 28;
|
||||
this.threadsbox.TextChanged += new System.EventHandler(this.threadsbox_TextChanged);
|
||||
//
|
||||
// keysizebutton
|
||||
//
|
||||
this.keysizebutton.Location = new System.Drawing.Point(653, 433);
|
||||
this.keysizebutton.Name = "keysizebutton";
|
||||
this.keysizebutton.Size = new System.Drawing.Size(75, 23);
|
||||
this.keysizebutton.TabIndex = 29;
|
||||
this.keysizebutton.Text = "1024b";
|
||||
this.keysizebutton.UseVisualStyleBackColor = true;
|
||||
this.keysizebutton.Click += new System.EventHandler(this.keysize_button_Click);
|
||||
//
|
||||
// label1
|
||||
//
|
||||
this.label1.AutoSize = true;
|
||||
this.label1.Location = new System.Drawing.Point(650, 417);
|
||||
this.label1.Name = "label1";
|
||||
this.label1.Size = new System.Drawing.Size(45, 13);
|
||||
this.label1.TabIndex = 30;
|
||||
this.label1.Text = "key size";
|
||||
//
|
||||
// label2
|
||||
//
|
||||
this.label2.AutoSize = true;
|
||||
this.label2.Location = new System.Drawing.Point(731, 417);
|
||||
this.label2.Name = "label2";
|
||||
this.label2.Size = new System.Drawing.Size(42, 13);
|
||||
this.label2.TabIndex = 31;
|
||||
this.label2.Text = "threads";
|
||||
//
|
||||
// label_threads_warning
|
||||
//
|
||||
this.label_threads_warning.AutoSize = true;
|
||||
this.label_threads_warning.Location = new System.Drawing.Point(734, 458);
|
||||
this.label_threads_warning.Name = "label_threads_warning";
|
||||
this.label_threads_warning.Size = new System.Drawing.Size(0, 13);
|
||||
this.label_threads_warning.TabIndex = 32;
|
||||
//
|
||||
// label_keygen_running
|
||||
//
|
||||
this.label_keygen_running.AutoSize = true;
|
||||
this.label_keygen_running.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
||||
this.label_keygen_running.ForeColor = System.Drawing.Color.Red;
|
||||
this.label_keygen_running.Location = new System.Drawing.Point(734, 480);
|
||||
this.label_keygen_running.Name = "label_keygen_running";
|
||||
this.label_keygen_running.Size = new System.Drawing.Size(125, 16);
|
||||
this.label_keygen_running.TabIndex = 33;
|
||||
this.label_keygen_running.Text = "keygen running...";
|
||||
this.label_keygen_running.Visible = false;
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(814, 496);
|
||||
this.Controls.Add(this.textBox4);
|
||||
this.Controls.Add(this.textBox6);
|
||||
this.Controls.Add(this.textBox5);
|
||||
this.Controls.Add(this.button13);
|
||||
this.ClientSize = new System.Drawing.Size(866, 508);
|
||||
this.Controls.Add(this.label_keygen_running);
|
||||
this.Controls.Add(this.label_threads_warning);
|
||||
this.Controls.Add(this.label2);
|
||||
this.Controls.Add(this.label1);
|
||||
this.Controls.Add(this.keysizebutton);
|
||||
this.Controls.Add(this.threadsbox);
|
||||
this.Controls.Add(this.keygenbutton);
|
||||
this.Controls.Add(this.msgs_differ_label);
|
||||
this.Controls.Add(this.msgdetailsbox);
|
||||
this.Controls.Add(this.pubkeypathbox);
|
||||
this.Controls.Add(this.privkeypathbox);
|
||||
this.Controls.Add(this.button12);
|
||||
this.Controls.Add(this.button10);
|
||||
this.Controls.Add(this.button9);
|
||||
this.Controls.Add(this.button8);
|
||||
this.Controls.Add(this.textBox3);
|
||||
this.Controls.Add(this.button7);
|
||||
this.Controls.Add(this.button6);
|
||||
this.Controls.Add(this.textBox2);
|
||||
this.Controls.Add(this.richTextBox3);
|
||||
this.Controls.Add(this.richTextBox2);
|
||||
this.Controls.Add(this.sha256sumbox);
|
||||
this.Controls.Add(this.pubkeybox);
|
||||
this.Controls.Add(this.privkeybox);
|
||||
this.Controls.Add(this.button5);
|
||||
this.Controls.Add(this.button4);
|
||||
this.Controls.Add(this.button3);
|
||||
this.Controls.Add(this.button2);
|
||||
this.Controls.Add(this.textBox1);
|
||||
this.Controls.Add(this.msgpathbox);
|
||||
this.Controls.Add(this.button1);
|
||||
this.Controls.Add(this.richTextBox1);
|
||||
this.Controls.Add(this.msgbox);
|
||||
this.DoubleBuffered = true;
|
||||
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
|
||||
this.Name = "Form1";
|
||||
|
@ -290,27 +338,31 @@
|
|||
|
||||
#endregion
|
||||
|
||||
private System.Windows.Forms.RichTextBox richTextBox1;
|
||||
private System.Windows.Forms.RichTextBox msgbox;
|
||||
private System.Windows.Forms.Button button1;
|
||||
private System.Windows.Forms.TextBox textBox1;
|
||||
private System.Windows.Forms.Button button2;
|
||||
private System.Windows.Forms.TextBox msgpathbox;
|
||||
private System.Windows.Forms.Button button3;
|
||||
private System.Windows.Forms.Button button4;
|
||||
private System.Windows.Forms.Button button5;
|
||||
private System.Windows.Forms.RichTextBox richTextBox2;
|
||||
private System.Windows.Forms.RichTextBox richTextBox3;
|
||||
private System.Windows.Forms.TextBox textBox2;
|
||||
private System.Windows.Forms.RichTextBox privkeybox;
|
||||
private System.Windows.Forms.RichTextBox pubkeybox;
|
||||
private System.Windows.Forms.TextBox sha256sumbox;
|
||||
private System.Windows.Forms.Button button6;
|
||||
private System.Windows.Forms.Button button7;
|
||||
private System.Windows.Forms.TextBox textBox3;
|
||||
private System.Windows.Forms.Button button8;
|
||||
private System.Windows.Forms.Button button9;
|
||||
private System.Windows.Forms.Button button10;
|
||||
private System.Windows.Forms.Button button12;
|
||||
private System.Windows.Forms.Button button13;
|
||||
private System.Windows.Forms.TextBox textBox5;
|
||||
private System.Windows.Forms.TextBox textBox6;
|
||||
private System.Windows.Forms.TextBox textBox4;
|
||||
private System.Windows.Forms.TextBox privkeypathbox;
|
||||
private System.Windows.Forms.TextBox pubkeypathbox;
|
||||
private System.Windows.Forms.TextBox msgdetailsbox;
|
||||
private System.Windows.Forms.Label msgs_differ_label;
|
||||
private System.Windows.Forms.Button keygenbutton;
|
||||
private System.Windows.Forms.TextBox threadsbox;
|
||||
private System.Windows.Forms.Button keysizebutton;
|
||||
private System.Windows.Forms.Label label1;
|
||||
private System.Windows.Forms.Label label2;
|
||||
private System.Windows.Forms.Label label_threads_warning;
|
||||
private System.Windows.Forms.Label label_keygen_running;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
428
Form1.cs
428
Form1.cs
|
@ -1,15 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace KRY_0x04
|
||||
|
@ -21,410 +10,89 @@ namespace KRY_0x04
|
|||
InitializeComponent();
|
||||
}
|
||||
|
||||
public string path = "";
|
||||
LogicHandler lh = new LogicHandler();
|
||||
|
||||
string sha256sum(string path)
|
||||
{
|
||||
string sha256sum = "";
|
||||
try
|
||||
{
|
||||
FileStream my_filestream = File.OpenRead(path);
|
||||
SHA256Managed my_sha_256_managed = new SHA256Managed();
|
||||
byte[] byte_array_of_sha256 = my_sha_256_managed.ComputeHash(my_filestream);
|
||||
sha256sum = BitConverter.ToString(byte_array_of_sha256).Replace("-", string.Empty).ToLower();
|
||||
|
||||
my_filestream.Close();
|
||||
my_sha_256_managed.Clear();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
MessageBox.Show("https://xkcd.com/2200 \nerror: " + e.ToString(), "this should never have happened",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
return sha256sum;
|
||||
}
|
||||
|
||||
string get_file_metrics(string filename)
|
||||
{
|
||||
string metrics = "";
|
||||
metrics += $"created: " + $"{File.GetCreationTime(filename)}" + Environment.NewLine;
|
||||
metrics += $"file type: " + $"{Path.GetExtension(filename)}";
|
||||
return metrics;
|
||||
}
|
||||
|
||||
BigInteger return_bigint_representation_of_message(string input)
|
||||
{
|
||||
byte[] part_of_message = Encoding.ASCII.GetBytes(input);
|
||||
return new BigInteger(part_of_message);
|
||||
}
|
||||
|
||||
string return_string_representation_of_bigint(BigInteger bA_number)
|
||||
{
|
||||
byte[] decrypted_b_a = bA_number.ToByteArray();
|
||||
string decrypted_text = "";
|
||||
for (int i = 0; i <decrypted_b_a.Length; i++)
|
||||
{
|
||||
decrypted_text += Convert.ToString(Convert.ToChar(decrypted_b_a[i]));
|
||||
}
|
||||
return decrypted_text;
|
||||
}
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
private void load_msg_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* load msg */
|
||||
|
||||
using (OpenFileDialog dlg = new OpenFileDialog())
|
||||
{
|
||||
dlg.Title = "pick a msg";
|
||||
/* freedom for all */
|
||||
dlg.Filter = "message (*.msg)|*.msg|custom (*.*)|*.*";
|
||||
|
||||
if (dlg.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
richTextBox1.Text = File.ReadAllText(dlg.FileName);
|
||||
textBox1.Text = dlg.FileName;
|
||||
string f = dlg.FileName;
|
||||
textBox4.Text = get_file_metrics(f);
|
||||
}
|
||||
}
|
||||
lh.load_msg(msgbox, msgpathbox, msgdetailsbox, msgs_differ_label);
|
||||
}
|
||||
|
||||
private void button5_Click(object sender, EventArgs e)
|
||||
private void save_msg_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* save msg */
|
||||
|
||||
if (richTextBox1.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, richTextBox1.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);
|
||||
}
|
||||
}
|
||||
lh.save_msg(msgbox);
|
||||
}
|
||||
|
||||
private void button2_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* compute sha256 */
|
||||
|
||||
if (textBox1.Text != "")
|
||||
{
|
||||
textBox3.Text = sha256sum(textBox1.Text);
|
||||
textBox3.BorderStyle = BorderStyle.Fixed3D;
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("please first load a file, thanks","why are you doing this to me");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void button3_Click(object sender, EventArgs e)
|
||||
private void load_privkey_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* load privkey */
|
||||
|
||||
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)
|
||||
{
|
||||
richTextBox2.Text = File.ReadAllText(dlg.FileName);
|
||||
textBox5.Text = dlg.FileName;
|
||||
//path = textBox5.Text;
|
||||
}
|
||||
}
|
||||
lh.load_privkey(privkeybox, privkeypathbox);
|
||||
}
|
||||
|
||||
private void button4_Click(object sender, EventArgs e)
|
||||
private void load_pubkey_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* load pubkey */
|
||||
|
||||
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)
|
||||
{
|
||||
richTextBox3.Text = File.ReadAllText(dlg.FileName);
|
||||
textBox6.Text = dlg.FileName;
|
||||
}
|
||||
}
|
||||
lh.load_pubkey(pubkeybox, pubkeypathbox);
|
||||
}
|
||||
|
||||
private void button8_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* save sha256sum */
|
||||
|
||||
if (textBox3.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, textBox3.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void button13_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* load sha256 */
|
||||
|
||||
using (OpenFileDialog dlg = new OpenFileDialog())
|
||||
{
|
||||
dlg.Title = "pick a sha256sum";
|
||||
/* freedom for all */
|
||||
dlg.Filter = "sha256sum (*.sha256sum)|*.sha256sum|custom (*.*)|*.*";
|
||||
|
||||
if (dlg.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
textBox2.Text = File.ReadAllText(dlg.FileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void button9_Click(object sender, EventArgs e)
|
||||
private void zip_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* zip */
|
||||
if (textBox1.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;
|
||||
// VYTVORENI ADRESARE dirToZip
|
||||
if (path_to_msg == "")
|
||||
{
|
||||
MessageBox.Show("please specify a path first", "not this again, come on");
|
||||
return;
|
||||
}
|
||||
Directory.CreateDirectory(pathTo_dirToZip);
|
||||
|
||||
// VYTVORENI/ZAPIS SOUBORU sha256OfMessage.txt DO ADRESARE dirToZip
|
||||
// OBSAHEM SOUBORU sha256OfMessage.txt je SHA256 OTISK SOUBORU, KTERY SE NACHAZI NA CESTE pathToMessageForTransfer
|
||||
File.WriteAllText(path_to_msg, textBox1.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
lh.do_zip(msgbox, msgpathbox, msgs_differ_label);
|
||||
}
|
||||
private void button10_Click(object sender, EventArgs e)
|
||||
private void unzip_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* 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;
|
||||
path = pathTo_UnzippedDir;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
lh.do_unzip();
|
||||
}
|
||||
|
||||
private void button6_Click(object sender, EventArgs e)
|
||||
private void sign_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* sign */
|
||||
if (richTextBox1.Text == "" || richTextBox2.Text == "" || richTextBox3.Text == "" || textBox1.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 = richTextBox2.Text.Split(' ');
|
||||
MessageBox.Show("we good?");
|
||||
Console.WriteLine(BigInteger.Parse(keys[1]));
|
||||
Console.WriteLine(BigInteger.Parse(keys[0]));
|
||||
string signed = Convert.ToString(BigInteger.ModPow(return_bigint_representation_of_message(textBox3.Text),BigInteger.Parse(keys[1]),BigInteger.Parse(keys[0])));
|
||||
|
||||
|
||||
saveFileDialog1.Title = ".sign";
|
||||
saveFileDialog1.Filter = "sign file (*.sign)|*.sign|custom (*.*)|*.*";
|
||||
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);
|
||||
}
|
||||
}
|
||||
/* sign */
|
||||
lh.do_sign(msgbox, privkeybox, pubkeybox, sha256sumbox);
|
||||
}
|
||||
|
||||
private void button7_Click(object sender, EventArgs e)
|
||||
private void verify_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* verify */
|
||||
string[] keys = richTextBox3.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 = 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 (sha256sum(orig_message) == verified)
|
||||
{
|
||||
MessageBox.Show($"{sha256sum(orig_message)} == {verified}", "we good");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"orig message: {orig_message}");
|
||||
Console.WriteLine($"verif message: {verified}");
|
||||
MessageBox.Show("something is seriously wrong", "checksums differ!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
MessageBox.Show($"{sha256sum(orig_message)} == {verified}", "we good");
|
||||
}
|
||||
lh.do_verify_wrapper(privkeybox, pubkeybox);
|
||||
}
|
||||
|
||||
private void button12_Click(object sender, EventArgs e)
|
||||
private void cleanALL_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
textBox1.Text = "";
|
||||
textBox2.Text = "";
|
||||
textBox3.Text = "";
|
||||
textBox4.Text = "";
|
||||
textBox5.Text = "";
|
||||
textBox6.Text = "";
|
||||
richTextBox1.Text = "";
|
||||
richTextBox2.Text = "";
|
||||
richTextBox3.Text = "";
|
||||
path = "";
|
||||
msgpathbox.Text = "";
|
||||
msgbox.Text = "";
|
||||
msgdetailsbox.Text = "";
|
||||
privkeypathbox.Text = "";
|
||||
pubkeypathbox.Text = "";
|
||||
sha256sumbox.Text = "";
|
||||
privkeybox.Text = "";
|
||||
pubkeybox.Text = "";
|
||||
}
|
||||
|
||||
private void msgbox_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
lh.do_msgs_differ(msgbox.Text, msgpathbox.Text, msgs_differ_label, sha256sumbox);
|
||||
}
|
||||
|
||||
private void keysize_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
lh.keysize_handler(keysizebutton);
|
||||
}
|
||||
|
||||
private void keygen_button_Click(object sender, EventArgs e)
|
||||
{
|
||||
lh.genprimes_handler(threadsbox, label_threads_warning, label_keygen_running);
|
||||
}
|
||||
|
||||
private void threadsbox_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
/* threads textbox */
|
||||
lh.threads_tb_check(threadsbox, label_threads_warning);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,14 +51,17 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Dsapls.cs" />
|
||||
<Compile Include="Form1.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Form1.Designer.cs">
|
||||
<DependentUpon>Form1.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="LogicHandler.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Rsa.cs" />
|
||||
<EmbeddedResource Include="Form1.resx">
|
||||
<DependentUpon>Form1.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
|
|
|
@ -0,0 +1,421 @@
|
|||
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();
|
||||
Rsa rsapls = new Rsa();
|
||||
|
||||
|
||||
internal void load_msg(RichTextBox msgbox, TextBox msgpathbox, TextBox msgdetailsbox, Label msgs_differ_label)
|
||||
{
|
||||
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);
|
||||
/* hide the label as it's show "on text changed" but since
|
||||
* we're just loading a new message, informing us that it
|
||||
* changed is not really relevant */
|
||||
msgs_differ_label.Visible = false;
|
||||
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 do_msgs_differ(string msg, string path, Label do_they_differ, TextBox sha256sumbox)
|
||||
{
|
||||
if (msg != "" && path != "")
|
||||
{
|
||||
string msg_hash = dsapls.sha256sum_from_string(msg);
|
||||
string file_msg_hash = dsapls.sha256sum_from_path(path);
|
||||
if (msg_hash.Equals(file_msg_hash))
|
||||
{
|
||||
do_they_differ.Visible = false;
|
||||
sha256sumbox.Text = msg_hash;
|
||||
}
|
||||
else
|
||||
{
|
||||
do_they_differ.Visible = true;
|
||||
sha256sumbox.Text = msg_hash;
|
||||
}
|
||||
} else if (msg != "")
|
||||
{
|
||||
do_they_differ.Visible = false;
|
||||
string msg_hash = dsapls.sha256sum_from_string(msg);
|
||||
sha256sumbox.Text = msg_hash;
|
||||
} else if (msg == "")
|
||||
{
|
||||
do_they_differ.Visible = false;
|
||||
string msg_hash = dsapls.sha256sum_from_string(msg);
|
||||
sha256sumbox.Text = msg_hash;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal void load_privkey(RichTextBox privkeybox, TextBox privkeypathbox)
|
||||
{
|
||||
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)
|
||||
{
|
||||
OpenFileDialog dlg = new OpenFileDialog();
|
||||
dlg.Title = "pick a pubkey";
|
||||
/* freedom for all */
|
||||
dlg.Filter = "public key (*.pub)|*.pub|custom (*.*)|*.*";
|
||||
dlg.CheckFileExists = false;
|
||||
dlg.CheckPathExists = false;
|
||||
|
||||
if (dlg.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
pubkeybox.Text = File.ReadAllText(dlg.FileName);
|
||||
pubkeypathbox.Text = dlg.FileName;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal void do_zip(RichTextBox msgbox, TextBox msgpathbox, Label msgs_differ_label)
|
||||
{
|
||||
if (msgpathbox.Text == "")
|
||||
{
|
||||
MessageBox.Show("load a msg file first, please", "oh hey");
|
||||
return;
|
||||
}
|
||||
else if (msgs_differ_label.Visible == true)
|
||||
{
|
||||
MessageBox.Show("msg changed.\nsave changes first, please", "oh hey");
|
||||
return;
|
||||
}
|
||||
|
||||
MessageBox.Show("you're going to pick a sign file and a path where the final zip containing the signature and message will be placed.", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
|
||||
string sign_path = "";
|
||||
OpenFileDialog dlg_sign = new OpenFileDialog();
|
||||
dlg_sign.Title = "pick a .sign file";
|
||||
dlg_sign.CheckFileExists = false;
|
||||
dlg_sign.CheckPathExists = false;
|
||||
dlg_sign.DefaultExt = "";
|
||||
dlg_sign.Filter = "sign file (*.sign)|*.sign|custom (*.*)|*.*";
|
||||
dlg_sign.FilterIndex = 1;
|
||||
dlg_sign.RestoreDirectory = true;
|
||||
|
||||
DialogResult ds = dlg_sign.ShowDialog();
|
||||
if (ds == DialogResult.OK)
|
||||
{
|
||||
sign_path = dlg_sign.FileName;
|
||||
}
|
||||
else if (ds == DialogResult.Cancel)
|
||||
{
|
||||
MessageBox.Show("error: you have to choose a signature file to proceed", "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
FolderBrowserDialog dlg = new FolderBrowserDialog();
|
||||
dlg.ShowNewFolderButton = true;
|
||||
dlg.Description = "choose a folder where the zip will be placed";
|
||||
DialogResult d = dlg.ShowDialog();
|
||||
if (d == DialogResult.OK)
|
||||
{
|
||||
string pathTo_dirToZip = dlg.SelectedPath;
|
||||
/* the way we're handling this, it needs to be our workfolder,
|
||||
* really, but at the same time must not be touched, so the way
|
||||
* to go is to create another, disposable directory inside there */
|
||||
pathTo_dirToZip += Path.DirectorySeparatorChar + "disposable_workdir_pls";
|
||||
|
||||
Directory.CreateDirectory(pathTo_dirToZip);
|
||||
string path_to_msg = msgpathbox.Text;
|
||||
string msg_filename = Path.GetFileName(path_to_msg);
|
||||
string sign_filename = Path.GetFileName(sign_path);
|
||||
try
|
||||
{
|
||||
File.WriteAllText(pathTo_dirToZip + Path.DirectorySeparatorChar + msg_filename, msgbox.Text);
|
||||
File.WriteAllText(pathTo_dirToZip + Path.DirectorySeparatorChar + sign_filename, msgbox.Text);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
MessageBox.Show("error: " + e.ToString(), "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
try
|
||||
{
|
||||
string filename = Path.Combine(pathTo_dirToZip + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar, "zipfile.zip");
|
||||
if (File.Exists(filename))
|
||||
{
|
||||
MessageBox.Show($"file exists: {filename}\ndeleting", "ayy");
|
||||
File.Delete(filename);
|
||||
}
|
||||
|
||||
/* 0 for optimal compression, false for not including base dir */
|
||||
ZipFile.CreateFromDirectory(pathTo_dirToZip, filename, 0, false);
|
||||
MessageBox.Show("great success!", "gj, zip created");
|
||||
}
|
||||
catch (IOException exception)
|
||||
{
|
||||
MessageBox.Show("error: " + exception.ToString(), "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
Directory.Delete(pathTo_dirToZip, true);
|
||||
}
|
||||
else if (d == DialogResult.Cancel)
|
||||
{
|
||||
MessageBox.Show("error: you have to choose a path where the final zip will be placed", "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal void do_unzip()
|
||||
{
|
||||
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;
|
||||
FolderBrowserDialog dlg_folder = new FolderBrowserDialog();
|
||||
|
||||
dlg_folder.Description = "select folder to unzip the zip to yeeeaaah";
|
||||
DialogResult dr = dlg_folder.ShowDialog();
|
||||
if (dr == DialogResult.OK)
|
||||
{
|
||||
string pathTo_UnzippedDir = dlg_folder.SelectedPath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(zipfile);
|
||||
|
||||
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 sha256sumbox)
|
||||
{
|
||||
if (msgbox.Text == "" || privkeybox.Text == "" || pubkeybox.Text == "" || sha256sumbox.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(sha256sumbox.Text), BigInteger.Parse(keys[1]), BigInteger.Parse(keys[0])));
|
||||
|
||||
|
||||
saveFileDialog1.Title = ".sign";
|
||||
saveFileDialog1.CheckFileExists = false;
|
||||
saveFileDialog1.CheckPathExists = false;
|
||||
saveFileDialog1.DefaultExt = "";
|
||||
saveFileDialog1.Filter = "sign file (*.sign)|*.sign|custom (*.*)|*.*";
|
||||
saveFileDialog1.FilterIndex = 1;
|
||||
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_wrapper(RichTextBox privkeybox, RichTextBox pubkeybox)
|
||||
{
|
||||
if (privkeybox.Text != "" && pubkeybox.Text != "")
|
||||
{
|
||||
do_verify(pubkeybox);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("error: privkey, pubkey or both are not set. load your keys before proceeding.", "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
private void do_verify(RichTextBox pubkeybox)
|
||||
{
|
||||
string[] keys = pubkeybox.Text.Split(' ');
|
||||
string verified = "";
|
||||
string orig_message = "";
|
||||
bool proceed = true;
|
||||
|
||||
OpenFileDialog dlg_sign = new OpenFileDialog();
|
||||
dlg_sign.Title = "pick a .sign file";
|
||||
/* freedom for all */
|
||||
dlg_sign.Filter = "sign file (*.sign)|*.sign|custom (*.*)|*.*";
|
||||
|
||||
MessageBox.Show("you'll be choosing a signature and then a message file to verify", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
|
||||
DialogResult d = dlg_sign.ShowDialog();
|
||||
if (d == DialogResult.OK)
|
||||
{
|
||||
try
|
||||
{
|
||||
verified = dsapls.return_string_representation_of_bigint(BigInteger.ModPow(BigInteger.Parse(File.ReadAllText(dlg_sign.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);
|
||||
}
|
||||
|
||||
|
||||
} else if (d == DialogResult.Cancel)
|
||||
{
|
||||
proceed = false;
|
||||
MessageBox.Show("error: you have to choose a signature file to proceed", "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
|
||||
if (proceed)
|
||||
{
|
||||
OpenFileDialog dlg_msg = new OpenFileDialog();
|
||||
|
||||
dlg_msg.Title = "pick a .msg file";
|
||||
/* freedom for all */
|
||||
dlg_msg.Filter = "msg file (*.msg)|*.msg|custom (*.*)|*.*";
|
||||
|
||||
DialogResult dr = dlg_msg.ShowDialog();
|
||||
if (dr == DialogResult.OK)
|
||||
{
|
||||
try
|
||||
{
|
||||
orig_message = dlg_msg.FileName;
|
||||
}
|
||||
catch (Exception excep)
|
||||
{
|
||||
MessageBox.Show("error: " + excep.ToString(), "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
else if (dr == DialogResult.Cancel)
|
||||
{
|
||||
proceed = false;
|
||||
MessageBox.Show("error: you have to choose a msg file to proceed", "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
if (proceed)
|
||||
{
|
||||
if (dsapls.sha256sum_from_path(orig_message) == verified)
|
||||
{
|
||||
MessageBox.Show($"{dsapls.sha256sum_from_path(orig_message)} == {verified}", "we good, signatures match!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"orig message: {orig_message}");
|
||||
Console.WriteLine($"verif message: {verified}");
|
||||
MessageBox.Show($"something is seriously wrong\n{dsapls.sha256sum_from_path(orig_message)} != {verified}", "checksums differ!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal void threads_tb_check(TextBox tb, Label threads_warning)
|
||||
{
|
||||
rsapls.tb_check(tb, threads_warning);
|
||||
}
|
||||
|
||||
|
||||
internal void keysize_handler(Button keysizebutton)
|
||||
{
|
||||
rsapls.keysize_handler(keysizebutton);
|
||||
}
|
||||
|
||||
|
||||
internal void genprimes_handler(TextBox threadsbox, Label threads_warning, Label keygen_running)
|
||||
{
|
||||
MessageBox.Show("in the next step, you will be choosing path for saving the key pair", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
string path = "";
|
||||
|
||||
FolderBrowserDialog dlg = new FolderBrowserDialog();
|
||||
dlg.Description = "pick a location for your new keypair";
|
||||
dlg.ShowNewFolderButton = true;
|
||||
|
||||
DialogResult dr = dlg.ShowDialog();
|
||||
if (dr == DialogResult.OK)
|
||||
{
|
||||
try
|
||||
{
|
||||
path = dlg.SelectedPath;
|
||||
}
|
||||
catch (Exception excep)
|
||||
{
|
||||
MessageBox.Show("error: " + excep.ToString(), "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
else if (dr == DialogResult.Cancel)
|
||||
{
|
||||
MessageBox.Show("error: you have to choose a path", "this didn't work...", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
keygen_running.Visible = true;
|
||||
rsapls.genprimes_handler(threadsbox, threads_warning, path);
|
||||
keygen_running.Visible = false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,401 @@
|
|||
using System;
|
||||
using System.Drawing;
|
||||
using System.Numerics;
|
||||
using System.Security.Cryptography;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Threading;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.IO;
|
||||
|
||||
namespace KRY_0x04
|
||||
{
|
||||
class Rsa
|
||||
{
|
||||
private RandomNumberGenerator rng = new RNGCryptoServiceProvider();
|
||||
private static int block_size = ((Convert.ToInt32(key_size.b1024) * 2) / 8) - 1;
|
||||
private static int b1024 = Convert.ToInt32(key_size.b1024); //512b primes
|
||||
private static int b2048 = Convert.ToInt32(key_size.b2048);
|
||||
private static int b3072 = Convert.ToInt32(key_size.b3072);
|
||||
private static int b4096 = Convert.ToInt32(key_size.b4096);
|
||||
private static int randbytesize = 0;
|
||||
private static BigInteger P = 0;
|
||||
private static BigInteger Q = 0;
|
||||
private static BigInteger E = 0;
|
||||
private static BigInteger D = 0;
|
||||
private static BigInteger N = 0;
|
||||
private static bool isprime_p, isprime_q;
|
||||
private static int candidates_tried;
|
||||
private static DateTime gp_start, gp_finish;
|
||||
private static int numThreads = 3;
|
||||
private int custom_threads = 0;
|
||||
|
||||
enum key_size : uint
|
||||
{
|
||||
b1024 = 512,
|
||||
b2048 = 1024,
|
||||
b3072 = 1536,
|
||||
b4096 = 2048,
|
||||
}
|
||||
|
||||
private void gen_primes()
|
||||
{
|
||||
if (randbytesize == 0)
|
||||
{
|
||||
randbytesize = b1024; // default to something
|
||||
}
|
||||
|
||||
int min_checks = mr_min_checks(randbytesize * 2);
|
||||
byte[] random = new byte[randbytesize];
|
||||
BigInteger rand_int = 0;
|
||||
candidates_tried = 0;
|
||||
isprime_p = false;
|
||||
isprime_q = false;
|
||||
|
||||
while (!isprime_p || !isprime_q)
|
||||
{
|
||||
/* P(isprime(2^randbytesize)) == 2/(randbytesize*ln(2)) */
|
||||
candidates_tried++;
|
||||
rng.GetBytes(random);
|
||||
rand_int = new BigInteger(random);
|
||||
if (rand_int.IsEven || rand_int < 2) { continue; }
|
||||
if (!is_huge_prime(rand_int, min_checks))
|
||||
{
|
||||
Console.WriteLine($"[*] rand_int is not a_prime! (tried: {candidates_tried}:\n{rand_int})");
|
||||
continue;
|
||||
}
|
||||
Console.WriteLine($"[*] rand_int: {rand_int} \tis a prime!\n");
|
||||
if (P != 0)
|
||||
{
|
||||
if (rand_int != P)
|
||||
{
|
||||
Rsa qq = new Rsa();
|
||||
lock (qq)
|
||||
{
|
||||
Q = rand_int;
|
||||
isprime_q = true;
|
||||
}
|
||||
Console.WriteLine("[*] found q!\n");
|
||||
break;
|
||||
}
|
||||
Console.WriteLine("\n[!] q cannot equal p");
|
||||
continue;
|
||||
}
|
||||
|
||||
Rsa pp = new Rsa();
|
||||
lock (pp)
|
||||
{
|
||||
P = rand_int;
|
||||
isprime_p = true;
|
||||
}
|
||||
Console.WriteLine("[*] found p!\n");
|
||||
}
|
||||
}
|
||||
|
||||
private static int mr_min_checks(int bits)
|
||||
{
|
||||
if (bits > 2048)
|
||||
return 128;
|
||||
return 64;
|
||||
}
|
||||
|
||||
private static int[] low_primes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
|
||||
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
|
||||
181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269,
|
||||
271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367,
|
||||
373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461,
|
||||
463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571,
|
||||
577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661,
|
||||
673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773,
|
||||
787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883,
|
||||
887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};
|
||||
|
||||
private bool is_huge_prime(BigInteger source, int mr_min_rounds)
|
||||
{
|
||||
for (int i = 0; i < low_primes.Length; i++)
|
||||
{
|
||||
if (source % low_primes[i] == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
BigInteger r = source - BigInteger.One;
|
||||
int s = 0;
|
||||
|
||||
//while (r % 2 == BigInteger.Zero)
|
||||
while (0 == (r & 1))
|
||||
{
|
||||
r /= 2;
|
||||
s += 1;
|
||||
}
|
||||
|
||||
byte[] bytes = new byte[source.ToByteArray().LongLength];
|
||||
BigInteger a = BigInteger.Zero;
|
||||
|
||||
/* Miller–Rabin test composite detection probability: 75% (each round)
|
||||
* i.e. test false negative probability: 4^-n per round
|
||||
* 64 rounds --> undetected composite probability: 2^-128
|
||||
*/
|
||||
for (int i = 0; i < mr_min_rounds; i++)
|
||||
{
|
||||
while (a < 2 || a >= source - 2)
|
||||
{
|
||||
rng.GetBytes(bytes);
|
||||
a = new BigInteger(bytes);
|
||||
}
|
||||
|
||||
BigInteger x = BigInteger.ModPow(a, r, source);
|
||||
if (x != BigInteger.One)
|
||||
{
|
||||
int xy = 0;
|
||||
while (x != source - 1)
|
||||
{
|
||||
if (xy == s - 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
xy++;
|
||||
x = BigInteger.ModPow(x, 2, source);
|
||||
//if (x == BigInteger.One) return false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* probably prime */
|
||||
return true;
|
||||
}
|
||||
|
||||
private BigInteger mmi(BigInteger a, BigInteger nn)
|
||||
{
|
||||
BigInteger i = nn, v = BigInteger.Zero, d = BigInteger.One;
|
||||
while (a > BigInteger.Zero)
|
||||
{
|
||||
BigInteger t = i / a, x = a;
|
||||
a = i % x;
|
||||
i = x;
|
||||
x = d;
|
||||
d = v - t * x;
|
||||
v = x;
|
||||
}
|
||||
v %= nn;
|
||||
if (v < BigInteger.Zero) v = (v + nn) % nn;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
internal void genprimes_handler(TextBox threadsbox, Label threads_warning, string path)
|
||||
{
|
||||
string privkeypath = Path.Combine(path, "privkey-" + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".pri");
|
||||
string pubkeypath = Path.Combine(path, "pubkey-" + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".pub");
|
||||
string privkey = "";
|
||||
string pubkey = "";
|
||||
|
||||
ManualResetEvent resetEvent = new ManualResetEvent(false);
|
||||
int toProcess = 0;
|
||||
tb_check(threadsbox, threads_warning);
|
||||
bool is_invalid = Regex.IsMatch(threadsbox.Text, @"^\D+$");
|
||||
if (is_invalid)
|
||||
{
|
||||
toProcess = numThreads;
|
||||
tb_check(threadsbox, threads_warning);
|
||||
return;
|
||||
}
|
||||
|
||||
if (threads_valid(threadsbox.Text) == 1 || threads_valid(threadsbox.Text) == 2)
|
||||
{
|
||||
tb_check(threadsbox, threads_warning);
|
||||
MessageBox.Show("threads number needs to be 0 < n < 4", "check your numbah");
|
||||
return;
|
||||
}
|
||||
else if (threads_valid(threadsbox.Text) == 0)
|
||||
{
|
||||
bool is_whitespace = Regex.IsMatch(threadsbox.Text, @"\s+$");
|
||||
if (is_whitespace)
|
||||
{
|
||||
toProcess = numThreads;
|
||||
}
|
||||
else if (threadsbox.Text == "")
|
||||
{
|
||||
toProcess = numThreads;
|
||||
}
|
||||
else
|
||||
{
|
||||
custom_threads = Convert.ToInt32(threadsbox.Text);
|
||||
}
|
||||
}
|
||||
if (custom_threads != 0)
|
||||
{
|
||||
numThreads = custom_threads;
|
||||
}
|
||||
|
||||
/* Start workers. */
|
||||
gp_start = DateTime.Now;
|
||||
Console.WriteLine($"\n[*] spawning genprimes threads \t({gp_start})");
|
||||
Parallel.For(0, numThreads, i =>
|
||||
{
|
||||
gen_primes();
|
||||
});
|
||||
gp_finish = DateTime.Now;
|
||||
Console.WriteLine($"[*] genprimes finished.\t({gp_finish})\n");
|
||||
|
||||
Console.WriteLine($"[*] p:\n{P}");
|
||||
Console.WriteLine($"[*] q:\n{Q}\n");
|
||||
|
||||
N = P * Q;
|
||||
Console.WriteLine($"[*] n:\n{N}\n");
|
||||
|
||||
Console.WriteLine("[*] eulerFunction = (p - 1) * (q - 1)");
|
||||
BigInteger eulerFunction = (P - 1) * (Q - 1);
|
||||
Console.WriteLine($"[*] eulerFunction:\n{eulerFunction}\n");
|
||||
|
||||
do
|
||||
{
|
||||
using (RNGCryptoServiceProvider rg = new RNGCryptoServiceProvider())
|
||||
{
|
||||
byte[] random = new byte[block_size];
|
||||
rg.GetBytes(random);
|
||||
E = new BigInteger(random);
|
||||
E = BigInteger.Abs(E);
|
||||
}
|
||||
} while (!(E > 1) || !(E < eulerFunction) || !(BigInteger.GreatestCommonDivisor(E, eulerFunction) == 1));
|
||||
|
||||
Console.WriteLine($"[*] gcd(e, eulerFunction) = {BigInteger.GreatestCommonDivisor(E, eulerFunction)}");
|
||||
Console.WriteLine($"[*] e:\n{E}\n");
|
||||
|
||||
Console.WriteLine($"d = e exp -1");
|
||||
D = mmi(E, eulerFunction);
|
||||
Console.WriteLine($"[*] d:\n{D}\n");
|
||||
|
||||
Console.WriteLine("[*] check inverse: e * d mod eulerFunction = 1");
|
||||
Console.WriteLine($"{E} * {D} mod {eulerFunction} = {(E * D) % eulerFunction}\n");
|
||||
|
||||
Console.WriteLine("***********\n* summary *\n***********");
|
||||
Console.WriteLine($"[*] p size: {P.ToString().Length} bits");
|
||||
Console.WriteLine($"[*] q size: {Q.ToString().Length} bits");
|
||||
Console.WriteLine($"[*] n size: {N.ToString().Length} bits");
|
||||
Console.WriteLine($"[*] e size: {E.ToString().Length} bits");
|
||||
Console.WriteLine($"[*] d size: {D.ToString().Length} bits");
|
||||
Console.WriteLine($"[*] tried primes: {candidates_tried}");
|
||||
Console.WriteLine($"[*] gp_start: {gp_start}");
|
||||
Console.WriteLine($"[*] gp_finish: {gp_finish}");
|
||||
Console.WriteLine($"[*] generating primes took: {gp_finish - gp_start}");
|
||||
Console.WriteLine("\n");
|
||||
|
||||
privkey = string.Join(" ", N, D);
|
||||
pubkey = string.Join(" ", N, E);
|
||||
write_keys(privkey, privkeypath, pubkey, pubkeypath);
|
||||
|
||||
MessageBox.Show("got primes y'all", "info");
|
||||
}
|
||||
|
||||
private void write_keys(string privkey, string privkeypath, string pubkey, string pubkeypath)
|
||||
{
|
||||
File.WriteAllText(privkeypath, privkey);
|
||||
File.WriteAllText(pubkeypath, pubkey);
|
||||
}
|
||||
|
||||
private int threads_valid(string input)
|
||||
{
|
||||
int returnme = 0;
|
||||
bool is_whitespace = Regex.IsMatch(input, @"\s");
|
||||
bool is_invalid = Regex.IsMatch(input, @"\D");
|
||||
bool has_num = Regex.IsMatch(input, @"\d");
|
||||
if (input == "" || is_whitespace)
|
||||
{
|
||||
returnme = 0;
|
||||
}
|
||||
if (is_invalid)
|
||||
{
|
||||
returnme = 2;
|
||||
}
|
||||
else if (is_invalid)
|
||||
{
|
||||
if (has_num)
|
||||
{
|
||||
returnme = 1;
|
||||
}
|
||||
else if (Convert.ToInt32(input) <= 0 || Convert.ToInt32(input) > 3)
|
||||
{
|
||||
returnme = 1;
|
||||
}
|
||||
}
|
||||
else if (has_num)
|
||||
{
|
||||
if (is_invalid)
|
||||
{
|
||||
returnme = 2;
|
||||
}
|
||||
else if (Convert.ToInt32(input) <= 0 || Convert.ToInt32(input) > 3)
|
||||
{
|
||||
returnme = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return returnme;
|
||||
}
|
||||
|
||||
private void tb_invalid_input(TextBox tb)
|
||||
{
|
||||
tb.BackColor = Color.PaleVioletRed;
|
||||
}
|
||||
private void tb_valid_input(TextBox tb)
|
||||
{
|
||||
tb.BackColor = Color.White;
|
||||
}
|
||||
internal void tb_check(TextBox tb, Label threads_warning)
|
||||
{
|
||||
if (threads_valid(tb.Text) == 0)
|
||||
{
|
||||
tb_valid_input(tb);
|
||||
threads_warning.Text = "";
|
||||
threads_warning.Enabled = false;
|
||||
}
|
||||
else if (threads_valid(tb.Text) == 1)
|
||||
{
|
||||
tb_invalid_input(tb);
|
||||
threads_warning.Enabled = true;
|
||||
threads_warning.Text = "needs to be 0 < n < 4";
|
||||
return;
|
||||
}
|
||||
else if (threads_valid(tb.Text) == 2)
|
||||
{
|
||||
tb_invalid_input(tb);
|
||||
threads_warning.Enabled = true;
|
||||
threads_warning.Text = "needs to be a number";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal void keysize_handler(Button keysizebutton)
|
||||
{
|
||||
if (keysizebutton.Text == "1024b")
|
||||
{
|
||||
keysizebutton.Text = "2048b";
|
||||
randbytesize = b2048;
|
||||
block_size = ((Convert.ToInt32(key_size.b2048) * 2) / 8) - 1;
|
||||
}
|
||||
else if (keysizebutton.Text == "2048b")
|
||||
{
|
||||
keysizebutton.Text = "3072b";
|
||||
randbytesize = b3072;
|
||||
block_size = ((Convert.ToInt32(key_size.b3072) * 2) / 8) - 1;
|
||||
MessageBox.Show("gen_primes will likely take some time", "caution");
|
||||
}
|
||||
else if (keysizebutton.Text == "3072b")
|
||||
{
|
||||
keysizebutton.Text = "4096b";
|
||||
randbytesize = b4096;
|
||||
block_size = ((Convert.ToInt32(key_size.b4096) * 2) / 8) - 1;
|
||||
MessageBox.Show("gen_primes will likely take some time", "cautioncautioncautioncautioncaution");
|
||||
}
|
||||
else
|
||||
{
|
||||
keysizebutton.Text = "1024b";
|
||||
randbytesize = b1024;
|
||||
block_size = ((Convert.ToInt32(key_size.b1024) * 2) / 8) - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue