Example #1
0
 void Cipher_Click(object sender, RoutedEventArgs e)
 {
     if (Message.Foreground == Brushes.Gray || string.IsNullOrWhiteSpace(Message.Text))
     {
         MessageBoxError("Message can`t be empty!");
         GetAllDefault(key: false);
     }
     else if (Key.Foreground == Brushes.Gray || string.IsNullOrWhiteSpace(Key.Text))
     {
         MessageBoxError("Key can`t be empty!");
         GetAllDefault(message: false);
     }
     else
     {
         if (ComboBox.SelectedIndex == 0)
         {
             bool isNumber = int.TryParse(Key.Text, out int KeyNumber);
             if (isNumber)
             {
                 Result.Foreground = Brushes.Black;
                 if (KeyNumber >= 0 && KeyNumber <= 26)
                 {
                     Result.Text = (bool)CheckBoxDecrypt.IsChecked ? CaesarCipher.Decrypt(Message.Text, KeyNumber) : CaesarCipher.Encrypt(Message.Text, KeyNumber);
                 }
                 else
                 {
                     MessageBoxError("Rotation can`t be longer than 26 or shorter than 0!");
                     GetAllDefault(message: false);
                 }
             }
             else
             {
                 MessageBoxError("Key must be number!");
                 GetAllDefault(message: false);
             }
         }
         else
         {
             if (Key.Text.Any(x => char.IsWhiteSpace(x) || char.IsNumber(x)))
             {
                 MessageBoxError("Key should not contain spaces or numbers!");
                 GetAllDefault(message: false);
             }
             else
             {
                 Result.Foreground = Brushes.Black;
                 Result.Text       = (bool)CheckBoxDecrypt.IsChecked ? VigenereCipher.Decrypt(Message.Text, Key.Text) : VigenereCipher.Encrypt(Message.Text, Key.Text);
             }
         }
     }
 }