Advertisement
Pearlfromsu

laba 4

Nov 3rd, 2021
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. class Nice {
  7. static string[] symbols = new String[] { " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "/", "*", "=", "^", "%", ".", ",", ":" };
  8. static string changeWord(string str) {
  9. bool[] buk = new bool[28];
  10. string res = "";
  11. for (int i = 0; i < str.Length; i++) {
  12. int ch = Char.ToLower(str[i]) - 'a';
  13. if (!buk[ch]) {
  14. res += str[i];
  15. buk[Char.ToLower(str[i]) - 'a'] = true;
  16. }
  17. }
  18. return res;
  19. }
  20. static Encoding encoding = Encoding.ASCII;
  21. static byte[] crypt(string text, string keying) {
  22. byte[] plainText = encoding.GetBytes(text);
  23. byte[] key = encoding.GetBytes(keying);
  24.  
  25.  
  26. // encrypt: key XOR plainText
  27.  
  28. byte[] cipherText = new byte[plainText.Length];
  29. new BitArray(key).Xor(new BitArray(plainText)).CopyTo(cipherText, 0);
  30.  
  31.  
  32. // decrypt: key XOR chipherText
  33.  
  34. //byte[] decripted = new byte[cipherText.Length];
  35. //new BitArray(key).Xor(new BitArray(cipherText)).CopyTo(decripted, 0);
  36.  
  37. return /*encoding.GetString(*/cipherText/*)*/;
  38. }
  39. static byte[] cryptBytes(byte[] plainText, byte[] key) {
  40. // encrypt: key XOR plainText
  41.  
  42. byte[] cipherText = new byte[plainText.Length];
  43. new BitArray(key).Xor(new BitArray(plainText)).CopyTo(cipherText, 0);
  44.  
  45.  
  46. // decrypt: key XOR chipherText
  47.  
  48. //byte[] decripted = new byte[cipherText.Length];
  49. //new BitArray(key).Xor(new BitArray(cipherText)).CopyTo(decripted, 0);
  50.  
  51. return /*encoding.GetString(*/cipherText/*)*/;
  52. }
  53. static byte[] bt(byte b) {
  54. return new Byte[] { b };
  55. }
  56. public static string HextoString(string InputText) {
  57.  
  58. byte[] bb = Enumerable.Range(0, InputText.Length)
  59. .Where(x => x % 2 == 0)
  60. .Select(x => Convert.ToByte(InputText.Substring(x, 2), 16))
  61. .ToArray();
  62. return System.Text.Encoding.ASCII.GetString(bb);
  63. // or System.Text.Encoding.UTF7.GetString
  64. // or System.Text.Encoding.UTF8.GetString
  65. // or System.Text.Encoding.Unicode.GetString
  66. // or etc.
  67. }
  68. public static byte[] HextoBytes(string InputText) {
  69.  
  70. byte[] bb = Enumerable.Range(0, InputText.Length)
  71. .Where(x => x % 2 == 0)
  72. .Select(x => Convert.ToByte(InputText.Substring(x, 2), 16))
  73. .ToArray();
  74. return bb;
  75. }
  76. static string twoSybm(string str) {
  77. return (str.Length == 1 ? "0" + str : str);
  78. }
  79. static string ToHexString(byte[] hexstring) {
  80. StringBuilder sb = new StringBuilder();
  81. foreach (char t in hexstring) {
  82. //Note: X for upper, x for lower case letters
  83. sb.Append(Convert.ToInt32(t).ToString("x"));
  84. }
  85. return twoSybm(sb.ToString());
  86. }
  87. static string printHex(string hex) {
  88. string res = "";
  89. for (int i = 0; i < hex.Length; i++) {
  90. if(i != 0 && i % 2 == 0)
  91. res += " ";
  92. res += hex[i];
  93. }
  94. return res;
  95. }
  96. static void Main(string[] args) {
  97.  
  98. /*byte[] text = HextoBytes("315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b22");
  99. byte[] keying = HextoBytes("234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f");
  100. byte[] crypted = cryptBytes(text, keying);
  101. */
  102. byte[] text = encoding.GetBytes("abcdefghijklmnopqrstuvwxyz");
  103. byte[] keying = encoding.GetBytes(" ");
  104. byte[] crypted = cryptBytes(text, keying);
  105.  
  106. byte[] text1 = HextoBytes("315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc4");
  107. byte[] keying1 = HextoBytes("32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb");
  108. //byte[] text = HextoBytes("271946f9"); //HextoBytes("32510b");
  109. //byte[] keying = new Byte[] { 102, 57, 110};//102 57 110 The
  110. //Разница между заглавными буквами = 32 = 2^8
  111. byte[] crypted1 = cryptBytes(text1, keying1);
  112. for (int i = 0; i < Math.Min(text.Length, text1.Length); i++) {
  113. Console.WriteLine("{0}({1}) xor {2}({3}) = {4}({5}) : {6}({7})", (int)text[i], encoding.GetString(bt(text[i])), (int)keying[i], encoding.GetString(bt(keying[i])), (int)crypted[i], ToHexString(bt(crypted[i])), (int)crypted1[i], ToHexString(bt(crypted1[i])));
  114. }
  115. Console.WriteLine("\n Result: \n {0} \n =to= \n {1}", text, encoding.GetString(crypted));
  116.  
  117.  
  118. Console.WriteLine("\n comparing: \n {0} \n =with= \n {1}", printHex(ToHexString(crypted)), printHex(ToHexString(crypted1)));
  119.  
  120. //диапазон 0-31
  121. //заглавная буква +32 к разнице
  122. //То есть числа > 31 - заглавные буквы
  123. //Пробел - диапазон от 65-90 (ш. с. с. 41-5a)
  124. int max = 0;
  125. char[] maxi = new char[] {'A', 'A'};
  126. int min = int.MaxValue;
  127. char[] mini = new char[] { 'A', 'A' };
  128. for (byte i = 0; i < 26; i++) {
  129. for (byte j = 0; j < 26; j++) {
  130. char ct = (char)('A' + i);
  131. char ct2 = (char)('a' + j);
  132. byte[] cry = crypt(Char.ToString(ct), Char.ToString(ct2));
  133. if (cry[0] >= max) {
  134. max = cry[0];
  135. maxi = new char[] { ct, ct2 };
  136. }
  137. if (cry[0] <= min) {
  138. min = cry[0];
  139. mini = new char[] { ct, ct2 };
  140. }
  141. }
  142. }
  143. Console.WriteLine("MAX: " + max + " = " + maxi[0] + " xor " + maxi[1]);
  144. Console.WriteLine("MIN: " + min + " = " + mini[0] + " xor " + mini[1]);
  145.  
  146. //Console.WriteLine(HextoString("32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904") + " - eto");
  147. Console.ReadKey();
  148. }
  149. }
  150. /*
  151. using System;
  152. using System.Collections.Generic;
  153. using System.IO;
  154. using System.Linq;
  155.  
  156. class Nice {
  157.  
  158. static string[] symbols = new String[] { " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "/", "*", "=", "^", "%", ".", ",", ":" };
  159.  
  160. static string changeWord(string str) {
  161. bool[] buk = new bool[28];
  162. string res = "";
  163. for (int i = 0; i < str.Length; i++) {
  164. int ch = Char.ToLower(str[i]) - 'a';
  165. if (!buk[ch]) {
  166. res += str[i];
  167. buk[Char.ToLower(str[i]) - 'a'] = true;
  168. }
  169. }
  170. return res;
  171. }
  172.  
  173. static void Main(string[] args) {
  174. string[] lines = File.ReadAllLines("input.txt");
  175. string[] lastline = Array.ConvertAll((lines[lines.Length - 1]).Split(symbols, StringSplitOptions.RemoveEmptyEntries), changeWord);
  176. foreach (string line in lines) {
  177. string[] words = Array.ConvertAll(line.Split(symbols, StringSplitOptions.RemoveEmptyEntries), changeWord);
  178. foreach (string str in words)
  179. if (str != lastline[lastline.Length - 1])
  180. Console.Write(str + " ");
  181. }
  182. Console.ReadKey();
  183. }
  184. }*/
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement