165 lines
4.8 KiB
C#
165 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace KRY_0x01_ng
|
|
{
|
|
public partial class main_form : Form
|
|
{
|
|
public main_form()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
char[,] arrayTable = new char[5, 5];
|
|
char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVXYZ".ToCharArray(); // no W
|
|
char[] nualphabet = new char[25];
|
|
|
|
|
|
|
|
public string restore_order(string str)
|
|
{
|
|
string alpha_str = Regex.Replace(str, "SPACEBRO", " ");
|
|
return alpha_str;
|
|
}
|
|
|
|
public string pad(string str)
|
|
{
|
|
string padded_str = "";
|
|
if (str.Length > 0)
|
|
{
|
|
int i = 0;
|
|
while (i < str.Length)
|
|
{
|
|
char first_char = str[i];
|
|
i++;
|
|
char second_char;
|
|
if ((i == str.Length || str[i] == first_char) && (first_char != 'X'))
|
|
{
|
|
second_char = 'X';
|
|
}
|
|
else if ((i == str.Length || str[i] == first_char) && (first_char != 'Q'))
|
|
{
|
|
second_char = 'Q';
|
|
}
|
|
else if ((i == str.Length || str[i] == first_char) && (first_char != 'W'))
|
|
{
|
|
second_char = 'W';
|
|
}
|
|
else
|
|
{
|
|
second_char = str[i];
|
|
i++;
|
|
}
|
|
padded_str += first_char;
|
|
padded_str += second_char;
|
|
}
|
|
}
|
|
return padded_str;
|
|
}
|
|
|
|
|
|
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)
|
|
{
|
|
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 = "";
|
|
textBox7.Text = cryptmessage;
|
|
return;
|
|
}
|
|
|
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
/* encrypt button */
|
|
encrypt();
|
|
}
|
|
|
|
private void button2_Click(object sender, EventArgs e)
|
|
{
|
|
/* check key button */
|
|
check_key(textBox1);
|
|
}
|
|
}
|
|
}
|