feat: added decryption functionality

This commit is contained in:
citizen-VM 2020-08-17 12:31:39 +02:00
parent 180cc7c2da
commit e3a3a633d9
Signed by: wanderer
GPG Key ID: 6391444A736EEE7E
2 changed files with 140 additions and 9 deletions

80
main_form.Designer.cs generated
View File

@ -36,6 +36,13 @@
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.label7 = new System.Windows.Forms.Label();
this.textBox6 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
@ -106,11 +113,77 @@
this.label4.TabIndex = 7;
this.label4.Text = "cryptmessage";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(467, 166);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(24, 13);
this.label5.TabIndex = 12;
this.label5.Text = "key";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(467, 115);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(72, 13);
this.label6.TabIndex = 10;
this.label6.Text = "cryptmessage";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(470, 131);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(318, 20);
this.textBox4.TabIndex = 9;
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(470, 182);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(318, 20);
this.textBox5.TabIndex = 8;
//
// button2
//
this.button2.Location = new System.Drawing.Point(470, 222);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 11;
this.button2.Text = "decrypt";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(467, 295);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(49, 13);
this.label7.TabIndex = 14;
this.label7.Text = "message";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(470, 311);
this.textBox6.Name = "textBox6";
this.textBox6.ReadOnly = true;
this.textBox6.Size = new System.Drawing.Size(318, 20);
this.textBox6.TabIndex = 13;
//
// main_form
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label7);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label6);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.button2);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.label3);
@ -140,6 +213,13 @@
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox textBox6;
}
}

View File

@ -39,11 +39,11 @@ namespace KRY_0x07
MessageBox.Show("The size of the key is to be of the exact same length as the message (or shorter, in which case the key is repeated until it's as long as the message).\nNo message means nothing to incur key length from.","Error: empty message");
return;
}
int check_message_and_key()
int check_message_and_key(TextBox m_tb, TextBox k_tb)
{
int status = 0;
string message = textBox1.Text.ToUpper();
string key = textBox2.Text.ToUpper();
string message = m_tb.Text.ToUpper();
string key = k_tb.Text.ToUpper();
if (message == "")
{
@ -63,10 +63,10 @@ namespace KRY_0x07
return status;
}
void prepare_key()
void prepare_key(TextBox tb_m, TextBox tb_k)
{
string message = textBox1.Text.ToUpper();
string key = textBox2.Text.ToUpper();
string message = tb_m.Text.ToUpper();
string key = tb_k.Text.ToUpper();
if (key.Length < message.Length)
{
int length_diff = message.Length - key.Length;
@ -74,7 +74,7 @@ namespace KRY_0x07
{
key += key.Substring(i,1);
}
textBox2.Text = key;
tb_k.Text = key;
}
}
@ -113,18 +113,69 @@ namespace KRY_0x07
textBox3.Text = cryptmessage;
}
void decrypt()
{
fill_alph_square();
string key = textBox5.Text.ToUpper();
string cryptmessage = textBox4.Text.ToUpper();
int klength = key.Length;
int mlength = cryptmessage.Length;
int[] m_ints = new int[mlength];
int[] k_ints = new int[klength];
string message = "";
for (int i = 0; i < mlength; i++)
{
string mchar_to_deal_with = cryptmessage.Substring(i, 1);
string kchar_to_deal_with = key.Substring(i, 1);
for (int j = 0; j < alphabet.Length; j++)
{
if (mchar_to_deal_with == Convert.ToString(alphabet[j]))
{
m_ints[i] = j;
}
if (kchar_to_deal_with == Convert.ToString(alphabet[j]))
{
k_ints[i] = j;
}
}
}
for (int i = 0; i < mlength; i++)
{
message += alphabet[(m_ints[i] - k_ints[i] + 26) % alphabet.Length];
}
textBox6.Text = message;
}
private void button1_Click(object sender, EventArgs e)
{
int status = check_message_and_key();
/* encrypt */
int status = check_message_and_key(textBox1, textBox2);
if (status != 0)
{
return;
}
else
{
prepare_key();
prepare_key(textBox1, textBox2);
encrypt();
}
}
private void button2_Click(object sender, EventArgs e)
{
/* decrypt */
int status = check_message_and_key(textBox4, textBox5);
if (status != 0)
{
return;
}
else
{
prepare_key(textBox4,textBox5);
decrypt();
}
}
}
}