2020-08-21 03:15:35 +02:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Data ;
using System.Drawing ;
using System.Linq ;
using System.Text ;
2020-08-21 03:35:56 +02:00
using System.Text.RegularExpressions ;
2020-08-21 03:15:35 +02:00
using System.Threading.Tasks ;
using System.Windows.Forms ;
namespace KRY_0x01_ng
{
public partial class main_form : Form
{
public main_form ( )
{
InitializeComponent ( ) ;
}
2020-08-21 03:35:56 +02:00
char [ , ] arrayTable = new char [ 5 , 5 ] ;
char [ ] alphabet = "ABCDEFGHIJKLMNOPQRSTUVXYZ" . ToCharArray ( ) ; // no W
char [ ] nualphabet = new char [ 25 ] ;
2020-08-21 20:25:59 +02:00
string prep_msg_str = "" ;
2020-08-21 03:35:56 +02:00
public string restore_order ( string str )
{
2020-08-21 18:19:32 +02:00
string alpha_str = Regex . Replace ( str , "SPACEBRO" , " " ) ;
2020-08-21 03:35:56 +02:00
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' ;
}
2020-08-21 20:10:07 +02:00
else if ( ( i = = str . Length | | str [ i ] = = first_char ) & & ( first_char ! = 'Z' ) )
2020-08-21 03:35:56 +02:00
{
2020-08-21 20:10:07 +02:00
second_char = 'Z' ;
2020-08-21 03:35:56 +02:00
}
else
{
second_char = str [ i ] ;
i + + ;
}
padded_str + = first_char ;
padded_str + = second_char ;
}
2020-08-21 20:10:07 +02:00
if ( padded_str . Length % 2 ! = 0 )
{
padded_str = pad ( padded_str ) ;
}
2020-08-21 03:35:56 +02:00
}
return padded_str ;
}
2020-08-21 18:19:32 +02:00
void check_key ( TextBox tb_k )
2020-08-21 03:35:56 +02:00
{
2020-08-21 18:19:32 +02:00
if ( tb_k . Text . Length = = 0 )
{
MessageBox . Show ( "Empty key." , "Warning" ) ;
return ;
}
2020-08-21 20:10:07 +02:00
string str_to_check = String . Join ( "" , tb_k . Text . ToUpper ( ) . Distinct ( ) ) ;
2020-08-21 18:19:32 +02:00
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 ;
2020-08-21 20:10:07 +02:00
TextBox tb_nualph = textBox3 ;
2020-08-21 18:19:32 +02:00
tb_pruned_k . Text = str_to_check ;
2020-08-21 20:10:07 +02:00
nualphabet = alphabet . Except ( str_to_check . ToArray ( ) ) . ToArray ( ) ;
string nualph_str = "" ;
for ( int i = 0 ; i < nualphabet . Length ; i + + )
{
nualph_str + = nualphabet [ i ] ;
}
tb_nualph . Text = str_to_check + nualph_str ;
2020-08-21 03:35:56 +02:00
return ;
}
void check_message ( TextBox tb_m )
{
2020-08-21 18:19:32 +02:00
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 ;
}
2020-08-21 20:25:59 +02:00
string msg_hack_up ( string msg )
{
string digraph_msg = "" ;
for ( int i = 0 ; i < msg . Length - 1 ; i + = 2 )
{
digraph_msg + = msg . Substring ( i , 2 ) + " " ;
}
return digraph_msg . TrimEnd ( " " . ToCharArray ( ) ) ;
}
2020-08-21 18:19:32 +02:00
void prep_message ( TextBox tb_m )
{
2020-08-21 20:10:07 +02:00
string msg = pad ( tb_m . Text ) ;
2020-08-21 20:25:59 +02:00
prep_msg_str = msg ;
textBox6 . Text = msg_hack_up ( msg ) ;
2020-08-21 03:35:56 +02:00
return ;
}
void encrypt ( )
{
2020-08-21 18:19:32 +02:00
check_message ( textBox4 ) ;
prep_message ( textBox5 ) ;
2020-08-21 03:35:56 +02:00
string cryptmessage = "" ;
2020-08-21 18:19:32 +02:00
textBox7 . Text = cryptmessage ;
2020-08-21 03:35:56 +02:00
return ;
}
private void button1_Click ( object sender , EventArgs e )
{
/* encrypt button */
encrypt ( ) ;
}
private void button2_Click ( object sender , EventArgs e )
{
/* check key button */
2020-08-21 18:19:32 +02:00
check_key ( textBox1 ) ;
2020-08-21 03:35:56 +02:00
}
2020-08-21 03:15:35 +02:00
}
}