diff --git a/main_form.cs b/main_form.cs index 6178cbd..746de30 100644 --- a/main_form.cs +++ b/main_form.cs @@ -24,16 +24,9 @@ namespace KRY_0x01_ng - public string prune_input(TextBox tb) - { - string str = tb.Text.ToUpper(); - string alpha_str = Regex.Replace(str, " ", "MEZERABRO"); - alpha_str = alpha_str.Replace("[^A-Z]+", ""); - return alpha_str.Replace("W", "V"); - } public string restore_order(string str) { - string alpha_str = Regex.Replace(str, "MEZERABRO", " "); + string alpha_str = Regex.Replace(str, "SPACEBRO", " "); return alpha_str; } @@ -73,22 +66,84 @@ namespace KRY_0x01_ng } - void check_key() + void check_key(TextBox tb_k) { + if (tb_k.Text.Length == 0) + { + MessageBox.Show("Empty key.", "Warning"); + return; + } + + string str_to_check = str_to_check = String.Join("", tb_k.Text.ToUpper().Distinct()); + if (str_to_check.Length < 9) + { + MessageBox.Show($"The key is too short ({str_to_check.Length} characters).\nKey requirements: 9-25 unique alphabetic characters", "Error"); + return; + } + else if (str_to_check.Length > 25) + { + MessageBox.Show("The key is too long", "Warning"); + return; + } + + Match match = Regex.Match(str_to_check, @"\d|\s+"); + if (match.Success) + { + MessageBox.Show("Only alphabetic characters and spaces are allowed.\nCheck the key for numbers, symbols or tab whitespace and remove them before continuing.", "Error"); + return; + } + TextBox tb_pruned_k = textBox2; + tb_pruned_k.Text = str_to_check; return; } void check_message(TextBox tb_m) { - string msg = pad(tb_m.Text); + if (tb_m.Text.Length == 0) + { + MessageBox.Show("Empty message, nothing to do.", "Warning"); + return; + } + string msg = Regex.Replace(tb_m.Text.ToUpper(), " ", "SPACEBRO"); + Match match = Regex.Match(msg, @"\d|\s+"); + if (match.Success) + { + MessageBox.Show("Only alphabetic characters and spaces are allowed.\nCheck the message for numbers, symbols or tab whitespace and remove them before continuing.\nAlso, \"W\" will be replaced by \"V\".", "Error"); + return; + } + msg = msg.Replace("W", "V"); + textBox5.Text = msg; + return; + } + void prep_message(TextBox tb_m) + { + string msg = tb_m.Text; + if (msg.Length % 2 != 0) + { + if (! msg.EndsWith("X")) + { + msg += "X"; + } + else if (! msg.EndsWith("Q")) + { + msg += "Q"; + } + else if (! msg.EndsWith("Z")) + { + msg += "Z"; + } + } + textBox6.Text = pad(msg); return; } void encrypt() { + check_message(textBox4); + prep_message(textBox5); string cryptmessage = ""; - string m_text = prune_input(textBox4); + textBox7.Text = cryptmessage; return; } @@ -97,14 +152,13 @@ namespace KRY_0x01_ng private void button1_Click(object sender, EventArgs e) { /* encrypt button */ - check_message(textBox4); encrypt(); } private void button2_Click(object sender, EventArgs e) { /* check key button */ - check_key(); + check_key(textBox1); } } }