|
@@ -3,105 +3,131 @@ using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Windows;
|
|
|
+using System.Windows.Controls;
|
|
|
|
|
|
namespace Encrypter
|
|
|
{
|
|
|
public partial class MainWindow : Window
|
|
|
{
|
|
|
Encoding ascii = Encoding.ASCII;
|
|
|
-
|
|
|
+ RadioButton pressed = new RadioButton();
|
|
|
public MainWindow()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
+ pressed.Content = "1 Способ";
|
|
|
}
|
|
|
- private void sposob1_encrypt(object sender, RoutedEventArgs e)
|
|
|
+ private void EncryptClick(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
- string input = sposob1_input.Text, output = "";
|
|
|
-
|
|
|
- Dictionary<char, int> dictionarys = input.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
|
|
|
- int uniqueSymbCount = dictionarys.Count();
|
|
|
-
|
|
|
- Byte[] encodedBytes = ascii.GetBytes(input);
|
|
|
- foreach (Byte b in encodedBytes)
|
|
|
- output += $"{b + Convert.ToInt32(dictionarys.Where(item => item.Key == Convert.ToChar(b)).Select(item => item.Value).FirstOrDefault())}%";
|
|
|
- sposob1_output.Text = output;
|
|
|
+ Encryptoutput.Text = Encrypt(Encryptinput.Text, Convert.ToInt32(pressed.Content.ToString().Substring(0, 1)));
|
|
|
+ Decryptinput.Text = Encryptoutput.Text;
|
|
|
}
|
|
|
-
|
|
|
- public int CountOfCoincidences(string a, string[] mass)
|
|
|
+ private void DecryptClick(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
- int count = 0;
|
|
|
- for (int i = 0; i < mass.Length; i++)
|
|
|
- if (mass[i] == a)
|
|
|
- count++;
|
|
|
- return count;
|
|
|
+ Decryptoutput.Text = Decrypt(Decryptinput.Text, Convert.ToInt32(pressed.Content.ToString().Substring(0, 1)));
|
|
|
}
|
|
|
|
|
|
- private void sposob1_decrypt(object sender, RoutedEventArgs e)
|
|
|
+ public string Encrypt(string input, int way)
|
|
|
{
|
|
|
- string input = sposob1_input2.Text, output = "";
|
|
|
- string[] mass = input.Remove(input.Length - 1, 1).Split('%');
|
|
|
-
|
|
|
- int[,] array = new int[mass.Length, 2];
|
|
|
-
|
|
|
- for (int i = 0; i < mass.Length; i++)
|
|
|
+ string output = "";
|
|
|
+ Byte[] encodedBytes;
|
|
|
+ try
|
|
|
{
|
|
|
- array[i, 0] = Convert.ToInt32(mass[i]);
|
|
|
- array[i, 1] = CountOfCoincidences(mass[i], mass);
|
|
|
+ switch (way)
|
|
|
+ {
|
|
|
+ case 1:
|
|
|
+ Dictionary<char, int> dictionarys = input.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
|
|
|
+ encodedBytes = ascii.GetBytes(input);
|
|
|
+ foreach (Byte b in encodedBytes)
|
|
|
+ output += $"{b + Convert.ToInt32(dictionarys.Where(item => item.Key == Convert.ToChar(b)).Select(item => item.Value).FirstOrDefault())}%";
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ int pos = 0;
|
|
|
+ encodedBytes = ascii.GetBytes(input);
|
|
|
+ foreach (Byte b in encodedBytes)
|
|
|
+ {
|
|
|
+ output += $"{b + pos}%";
|
|
|
+ pos++;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ encodedBytes = ascii.GetBytes(input);
|
|
|
+ foreach (Byte b in encodedBytes)
|
|
|
+ output += $"{b + input.Length}%{input.Length}^?";
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- for (int i = 0; i < mass.Length; i++)
|
|
|
- output += Convert.ToChar(Convert.ToByte(array[i, 0] - array[i, 1]));
|
|
|
-
|
|
|
- sposob1_output2.Text = output;
|
|
|
- }
|
|
|
- private void sposob2_encrypt(object sender, RoutedEventArgs e)
|
|
|
- {
|
|
|
- string input = sposob2_input.Text, output = "";
|
|
|
- int pos = 0;
|
|
|
-
|
|
|
- Byte[] encodedBytes = ascii.GetBytes(input);
|
|
|
- foreach (Byte b in encodedBytes)
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- output += $"{b + pos}%";
|
|
|
- pos++;
|
|
|
+ MessageBox.Show("Ошибка шифрования. " + ex.ToString());
|
|
|
}
|
|
|
- sposob2_output.Text = output;
|
|
|
+
|
|
|
+ return output;
|
|
|
}
|
|
|
- private void sposob2_decrypt(object sender, RoutedEventArgs e)
|
|
|
+ public string Decrypt(string input, int way)
|
|
|
{
|
|
|
- string input = sposob2_input2.Text, output = "";
|
|
|
- int pos = 0;
|
|
|
+ string output = "";
|
|
|
+ try
|
|
|
+ {
|
|
|
+ switch (way)
|
|
|
+ {
|
|
|
+ case 1:
|
|
|
+ string[] mass = input.Remove(input.Length-1, 1).Split('%');
|
|
|
+
|
|
|
+ int[,] array = new int[mass.Length, 2];
|
|
|
+
|
|
|
+ for (int i = 0; i < mass.Length; i++)
|
|
|
+ {
|
|
|
+ array[i, 0] = Convert.ToInt32(mass[i]);
|
|
|
+ array[i, 1] = CountOfCoincidences(mass[i], mass);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < mass.Length; i++)
|
|
|
+ output += Convert.ToChar(Convert.ToByte(array[i, 0] - array[i, 1]));
|
|
|
+
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ int pos = 0;
|
|
|
|
|
|
- while (input.IndexOf('%') != -1)
|
|
|
+ while (input.IndexOf('%') != -1)
|
|
|
+ {
|
|
|
+ byte temp = Convert.ToByte(Convert.ToInt32(input.Substring(0, input.IndexOf('%'))) - pos);
|
|
|
+ pos++;
|
|
|
+ output += Convert.ToChar(temp);
|
|
|
+ input = input.Remove(0, input.IndexOf('%') + 1);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 3:
|
|
|
+ int count = input.Count(c => c == '%');
|
|
|
+
|
|
|
+ while (input.IndexOf($"%{count}^?") != -1)
|
|
|
+ {
|
|
|
+ byte temp = Convert.ToByte(Convert.ToInt32(input.Substring(0, input.IndexOf($"%{count}^?"))) - count);
|
|
|
+ output += Convert.ToChar(temp);
|
|
|
+ input = input.Remove(0, input.IndexOf($"%{count}^?") + 3 + count.ToString().Length);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- byte temp = Convert.ToByte(Convert.ToInt32(input.Substring(0, input.IndexOf('%'))) - pos);
|
|
|
- pos++;
|
|
|
- output += Convert.ToChar(temp);
|
|
|
- input = input.Remove(0, input.IndexOf('%') + 1);
|
|
|
+ MessageBox.Show("Ошибка расшифровки. " + ex.ToString());
|
|
|
}
|
|
|
- sposob2_output2.Text = output;
|
|
|
- }
|
|
|
- private void sposob3_encrypt(object sender, RoutedEventArgs e)
|
|
|
- {
|
|
|
- string input = sposob3_input.Text, output = "";
|
|
|
|
|
|
- Byte[] encodedBytes = ascii.GetBytes(input);
|
|
|
- foreach (Byte b in encodedBytes)
|
|
|
- output += $"{b + input.Length}%{input.Length}^?";
|
|
|
- sposob3_output.Text = output;
|
|
|
+ return output;
|
|
|
}
|
|
|
- private void sposob3_decrypt(object sender, RoutedEventArgs e)
|
|
|
+
|
|
|
+ public int CountOfCoincidences(string a, string[] mass)
|
|
|
{
|
|
|
- string input = sposob3_input2.Text, output = "";
|
|
|
- int count = input.Count(c => c == '%');
|
|
|
+ int count = 0;
|
|
|
+ for (int i = 0; i < mass.Length; i++)
|
|
|
+ if (mass[i] == a)
|
|
|
+ count++;
|
|
|
+ return count;
|
|
|
+ }
|
|
|
|
|
|
- while (input.IndexOf($"%{count}^?") != -1)
|
|
|
- {
|
|
|
- byte temp = Convert.ToByte(Convert.ToInt32(input.Substring(0, input.IndexOf($"%{count}^?"))) - count);
|
|
|
- output += Convert.ToChar(temp);
|
|
|
- input = input.Remove(0, input.IndexOf($"%{count}^?") + 3 + count.ToString().Length);
|
|
|
- }
|
|
|
- sposob3_output2.Text = output;
|
|
|
+ private void RadioButton_Checked(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ pressed = (RadioButton)sender;
|
|
|
}
|
|
|
}
|
|
|
}
|