MainWindow.xaml.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace Encrypter
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для MainWindow.xaml
  19. /// </summary>
  20. public partial class MainWindow : Window
  21. {
  22. Encoding ascii = Encoding.ASCII;
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. }
  27. private void sposob1_encrypt(object sender, RoutedEventArgs e)
  28. {
  29. string input = sposob1_input.Text, output = "";
  30. Dictionary<char, int> dictionarys = input.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
  31. int uniqueSymbCount = dictionarys.Count();
  32. Byte[] encodedBytes = ascii.GetBytes(input);
  33. foreach (Byte b in encodedBytes)
  34. output += $"{b + Convert.ToInt32(dictionarys.Where(item => item.Key == Convert.ToChar(b)).Select(item => item.Value).FirstOrDefault())}%";
  35. sposob1_output.Text = output;
  36. }
  37. private void sposob1_decrypt(object sender, RoutedEventArgs e)
  38. {
  39. //not working
  40. string input = sposob1_input2.Text, output = "";
  41. Dictionary<char, int> dictionarys = input.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
  42. int uniqueSymbCount = dictionarys.Count();
  43. int pos = 0;
  44. while (input.IndexOf('%') != -1)
  45. {
  46. byte temp = Convert.ToByte(Convert.ToInt32(input.Substring(0, input.IndexOf('%'))) - Convert.ToInt32(dictionarys.Where(item => item.Key == Convert.ToChar(b)).Select(item => item.Value).FirstOrDefault()));
  47. pos++;
  48. output += Convert.ToChar(temp);
  49. input = input.Remove(0, input.IndexOf('%') + 1);
  50. }
  51. sposob1_output2.Text = output;
  52. }
  53. private void sposob2_encrypt(object sender, RoutedEventArgs e)
  54. {
  55. string input = sposob2_input.Text, output = "";
  56. int pos = 0;
  57. Byte[] encodedBytes = ascii.GetBytes(input);
  58. foreach (Byte b in encodedBytes)
  59. {
  60. output += $"{b+pos}%";
  61. pos++;
  62. }
  63. sposob2_output.Text = output;
  64. }
  65. private void sposob2_decrypt(object sender, RoutedEventArgs e)
  66. {
  67. string input = sposob2_input2.Text, output = "";
  68. int pos = 0;
  69. while (input.IndexOf('%') != -1)
  70. {
  71. byte temp = Convert.ToByte(Convert.ToInt32(input.Substring(0, input.IndexOf('%'))) - pos);
  72. pos++;
  73. output += Convert.ToChar(temp);
  74. input = input.Remove(0, input.IndexOf('%') +1);
  75. }
  76. sposob2_output2.Text = output;
  77. }
  78. private void sposob3_encrypt(object sender, RoutedEventArgs e)
  79. {
  80. string input = sposob3_input.Text, output = "";
  81. Byte[] encodedBytes = ascii.GetBytes(input);
  82. foreach (Byte b in encodedBytes)
  83. output += $"{b + input.Length}%{input.Length}^?";
  84. sposob3_output.Text = output;
  85. }
  86. private void sposob3_decrypt(object sender, RoutedEventArgs e)
  87. {
  88. string input = sposob3_input2.Text, output = "";
  89. int count = input.Count(c => c == '%');
  90. while (input.IndexOf($"%{count}^?") != -1)
  91. {
  92. byte temp = Convert.ToByte(Convert.ToInt32(input.Substring(0, input.IndexOf($"%{count}^?"))) - count);
  93. output += Convert.ToChar(temp);
  94. input = input.Remove(0, input.IndexOf($"%{count}^?") + 3 + count.ToString().Length);
  95. }
  96. sposob3_output2.Text = output;
  97. }
  98. }
  99. }