chore: various refactorings + improved UX; decryption WIP
This commit is contained in:
parent
49e996d602
commit
8c52b6c39b
109
Form1.Designer.cs
generated
109
Form1.Designer.cs
generated
@ -40,6 +40,15 @@
|
||||
this.textBox4 = new System.Windows.Forms.TextBox();
|
||||
this.label4 = new System.Windows.Forms.Label();
|
||||
this.label5 = new System.Windows.Forms.Label();
|
||||
this.textBox5 = new System.Windows.Forms.TextBox();
|
||||
this.label6 = new System.Windows.Forms.Label();
|
||||
this.label7 = new System.Windows.Forms.Label();
|
||||
this.label8 = new System.Windows.Forms.Label();
|
||||
this.textBox7 = new System.Windows.Forms.TextBox();
|
||||
this.label9 = new System.Windows.Forms.Label();
|
||||
this.label10 = new System.Windows.Forms.Label();
|
||||
this.textBox6 = new System.Windows.Forms.TextBox();
|
||||
this.button5 = new System.Windows.Forms.Button();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// button1
|
||||
@ -81,6 +90,7 @@
|
||||
//
|
||||
// textBox2
|
||||
//
|
||||
this.textBox2.Enabled = false;
|
||||
this.textBox2.Location = new System.Drawing.Point(105, 137);
|
||||
this.textBox2.Name = "textBox2";
|
||||
this.textBox2.ReadOnly = true;
|
||||
@ -147,11 +157,99 @@
|
||||
this.label5.TabIndex = 4;
|
||||
this.label5.Text = "cryptmessage";
|
||||
//
|
||||
// textBox5
|
||||
//
|
||||
this.textBox5.Location = new System.Drawing.Point(707, 108);
|
||||
this.textBox5.Name = "textBox5";
|
||||
this.textBox5.Size = new System.Drawing.Size(404, 23);
|
||||
this.textBox5.TabIndex = 6;
|
||||
//
|
||||
// label6
|
||||
//
|
||||
this.label6.AutoSize = true;
|
||||
this.label6.Location = new System.Drawing.Point(105, 90);
|
||||
this.label6.Name = "label6";
|
||||
this.label6.Size = new System.Drawing.Size(226, 15);
|
||||
this.label6.TabIndex = 7;
|
||||
this.label6.Text = "replacing nasty characters in the message";
|
||||
this.label6.Visible = false;
|
||||
//
|
||||
// label7
|
||||
//
|
||||
this.label7.AutoSize = true;
|
||||
this.label7.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
|
||||
this.label7.Location = new System.Drawing.Point(105, 64);
|
||||
this.label7.Name = "label7";
|
||||
this.label7.Size = new System.Drawing.Size(413, 19);
|
||||
this.label7.TabIndex = 8;
|
||||
this.label7.Text = "Allowed characters: ABCDEFGHIJKLMNOPQRSTUVWXYZ <space>";
|
||||
//
|
||||
// label8
|
||||
//
|
||||
this.label8.AutoSize = true;
|
||||
this.label8.Location = new System.Drawing.Point(621, 111);
|
||||
this.label8.Name = "label8";
|
||||
this.label8.Size = new System.Drawing.Size(80, 15);
|
||||
this.label8.TabIndex = 9;
|
||||
this.label8.Text = "cryptmessage";
|
||||
//
|
||||
// textBox7
|
||||
//
|
||||
this.textBox7.Location = new System.Drawing.Point(707, 169);
|
||||
this.textBox7.Name = "textBox7";
|
||||
this.textBox7.Size = new System.Drawing.Size(404, 23);
|
||||
this.textBox7.TabIndex = 11;
|
||||
//
|
||||
// label9
|
||||
//
|
||||
this.label9.AutoSize = true;
|
||||
this.label9.Location = new System.Drawing.Point(676, 172);
|
||||
this.label9.Name = "label9";
|
||||
this.label9.Size = new System.Drawing.Size(25, 15);
|
||||
this.label9.TabIndex = 12;
|
||||
this.label9.Text = "key";
|
||||
//
|
||||
// label10
|
||||
//
|
||||
this.label10.AutoSize = true;
|
||||
this.label10.Location = new System.Drawing.Point(648, 252);
|
||||
this.label10.Name = "label10";
|
||||
this.label10.Size = new System.Drawing.Size(53, 15);
|
||||
this.label10.TabIndex = 16;
|
||||
this.label10.Text = "message";
|
||||
//
|
||||
// textBox6
|
||||
//
|
||||
this.textBox6.Location = new System.Drawing.Point(707, 249);
|
||||
this.textBox6.Name = "textBox6";
|
||||
this.textBox6.ReadOnly = true;
|
||||
this.textBox6.Size = new System.Drawing.Size(404, 23);
|
||||
this.textBox6.TabIndex = 15;
|
||||
//
|
||||
// button5
|
||||
//
|
||||
this.button5.Location = new System.Drawing.Point(707, 278);
|
||||
this.button5.Name = "button5";
|
||||
this.button5.Size = new System.Drawing.Size(75, 23);
|
||||
this.button5.TabIndex = 14;
|
||||
this.button5.Text = "decrypt";
|
||||
this.button5.UseVisualStyleBackColor = true;
|
||||
this.button5.Click += new System.EventHandler(this.button5_Click);
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(800, 450);
|
||||
this.ClientSize = new System.Drawing.Size(1134, 450);
|
||||
this.Controls.Add(this.label10);
|
||||
this.Controls.Add(this.textBox6);
|
||||
this.Controls.Add(this.button5);
|
||||
this.Controls.Add(this.label9);
|
||||
this.Controls.Add(this.textBox7);
|
||||
this.Controls.Add(this.label8);
|
||||
this.Controls.Add(this.label7);
|
||||
this.Controls.Add(this.label6);
|
||||
this.Controls.Add(this.textBox5);
|
||||
this.Controls.Add(this.label5);
|
||||
this.Controls.Add(this.label4);
|
||||
this.Controls.Add(this.textBox4);
|
||||
@ -185,5 +283,14 @@
|
||||
private System.Windows.Forms.TextBox textBox4;
|
||||
private System.Windows.Forms.Label label4;
|
||||
private System.Windows.Forms.Label label5;
|
||||
private System.Windows.Forms.TextBox textBox5;
|
||||
private System.Windows.Forms.Label label6;
|
||||
private System.Windows.Forms.Label label7;
|
||||
private System.Windows.Forms.Label label8;
|
||||
private System.Windows.Forms.TextBox textBox7;
|
||||
private System.Windows.Forms.Label label9;
|
||||
private System.Windows.Forms.Label label10;
|
||||
private System.Windows.Forms.TextBox textBox6;
|
||||
private System.Windows.Forms.Button button5;
|
||||
}
|
||||
}
|
130
Form1.cs
130
Form1.cs
@ -42,6 +42,12 @@ namespace KRY_0x05_netcore
|
||||
otp = null;
|
||||
}
|
||||
|
||||
void empty_message_warning()
|
||||
{
|
||||
MessageBox.Show("The key is to be of the exact same length as the message.\nNo message means no key length to incur.", "Empty message yo");
|
||||
return;
|
||||
}
|
||||
|
||||
public byte[] gimme_bytes(int howmuch)
|
||||
{
|
||||
RNGCryptoServiceProvider rnd = new RNGCryptoServiceProvider();
|
||||
@ -52,27 +58,29 @@ namespace KRY_0x05_netcore
|
||||
|
||||
void gen_nice_key()
|
||||
{
|
||||
textBox2.Text = "";
|
||||
textBox3.Text = "";
|
||||
byte[] random = new byte[1337];
|
||||
if (textBox1.Text == "")
|
||||
{
|
||||
MessageBox.Show("empty message yo", "just so you know");
|
||||
return;
|
||||
}
|
||||
else if (textBox1.Text == "")
|
||||
{
|
||||
MessageBox.Show("kind of forgot about the key, didn't you", "heeey");
|
||||
empty_message_warning();
|
||||
textBox2.Text = "";
|
||||
return;
|
||||
}
|
||||
Match match = Regex.Match(textBox1.Text, @"\d|\s+");
|
||||
if (match.Success)
|
||||
{
|
||||
label6.Visible = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
label6.Visible = false;
|
||||
}
|
||||
string m = textBox1.Text;
|
||||
m = m.ToUpper();
|
||||
m = Regex.Replace(m, @" ", "MEZERABRO");
|
||||
m = Regex.Replace(m, @"\d|\s+", "");
|
||||
textBox2.Text = m;
|
||||
if (match.Success){MessageBox.Show("nasty message", "replacing forbidden stuff");}
|
||||
|
||||
|
||||
byte[] random = new byte[1337];
|
||||
|
||||
int mlength = textBox2.Text.Length;
|
||||
otp = new int[mlength];
|
||||
for (int i = 0; i < mlength; i++)
|
||||
@ -81,12 +89,79 @@ namespace KRY_0x05_netcore
|
||||
random = gimme_bytes(random.Length);
|
||||
otp[i] = Math.Abs(BitConverter.ToInt32(random, 0) % 26);
|
||||
}
|
||||
textBox3.Text = "";
|
||||
for (int i = 0; i < otp.Length; i++)
|
||||
{
|
||||
textBox3.Text += alphabet[otp[i]];
|
||||
}
|
||||
}
|
||||
|
||||
void encrypt()
|
||||
{
|
||||
if (textBox1.Text == "")
|
||||
{
|
||||
empty_message_warning();
|
||||
return;
|
||||
}
|
||||
else if (otp == null)
|
||||
{
|
||||
MessageBox.Show("Please regenerate the key");
|
||||
return;
|
||||
}
|
||||
|
||||
MessageBox.Show("The key **WILL DISAPPEAR** the instant you click OK and you will never see it here again.\nMake sure you write it down (or that you remember it) if you're interested in ever decrypting your precious message.", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
string m = textBox2.Text;
|
||||
int[] num_val = new int[m.Length];
|
||||
for (int i = 0; i < m.Length; i++)
|
||||
{
|
||||
string a = m.Substring(i, 1);
|
||||
for (int j = 0; j < alphabet.Length; j++)
|
||||
{
|
||||
if (a == Convert.ToString(alphabet[j]))
|
||||
{
|
||||
num_val[i] = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
string cryptmessage = "";
|
||||
for (int i = 0; i < otp.Length; i++)
|
||||
{
|
||||
cryptmessage += alphabet[(num_val[i] + otp[i]) % 26];
|
||||
}
|
||||
textBox3.Text = "";
|
||||
clear_otp();
|
||||
textBox4.Text = cryptmessage;
|
||||
}
|
||||
|
||||
void check_and_replace_d_key()
|
||||
{
|
||||
string k = textBox7.Text;
|
||||
Match match = Regex.Match(k, @"\d|\s+");
|
||||
if (match.Success)
|
||||
{
|
||||
MessageBox.Show("Invalid characters in the key, not even attempting decryption.\nOnly the letters of the English alphabet are allowed for the key.","Error");
|
||||
return;
|
||||
}
|
||||
k = k.ToUpper();
|
||||
k = Regex.Replace(k, @"MEZERABRO", " ");
|
||||
return;
|
||||
}
|
||||
|
||||
void decrypt()
|
||||
{
|
||||
if (textBox5.Text == "")
|
||||
{
|
||||
MessageBox.Show("Nothing to decrypt", "Error");
|
||||
return;
|
||||
}
|
||||
else if (textBox7.Text == "")
|
||||
{
|
||||
MessageBox.Show("No key provided","Error");
|
||||
return;
|
||||
}
|
||||
check_and_replace_d_key();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
@ -102,34 +177,13 @@ namespace KRY_0x05_netcore
|
||||
private void button3_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* encrypt */
|
||||
if (otp == null)
|
||||
{
|
||||
MessageBox.Show("Please regenerate the key");
|
||||
return;
|
||||
}
|
||||
encrypt();
|
||||
}
|
||||
|
||||
MessageBox.Show("The key **WILL DISAPPEAR** the instant you click OK and you will never see it here again.\nMake sure you write it down (or that you remember it) if you're interested in ever decrypting your precious message.","WARNING",MessageBoxButtons.OK,MessageBoxIcon.Warning);
|
||||
string m = textBox2.Text;
|
||||
int[] num_val = new int[m.Length];
|
||||
for (int i = 0; i < m.Length; i++)
|
||||
{
|
||||
string a = m.Substring(i, 1);
|
||||
for (int j = 0; j < alphabet.Length; j++)
|
||||
{
|
||||
if (a == Convert.ToString(alphabet[j])) ;
|
||||
{
|
||||
num_val[i] = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
string cryptmessage = "";
|
||||
for (int i = 0; i < otp.Length; i++)
|
||||
{
|
||||
cryptmessage += alphabet[(num_val[i] + otp[i]) % 26];
|
||||
}
|
||||
textBox4.Text = cryptmessage;
|
||||
textBox3.Text = "";
|
||||
clear_otp();
|
||||
private void button5_Click(object sender, EventArgs e)
|
||||
{
|
||||
/* decrypt */
|
||||
decrypt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user