Advertisement
Pearlfromsu

Untitled

Nov 6th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.08 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. class CodesHandler
  8. {
  9. List<int[]>[] combinations;
  10. public CodesHandler()
  11. {
  12. combinations = new List<int[]>[1000];
  13. for(int i = 0; i < 1000; i++)
  14. {
  15. combinations[i] = new List<int[]>();
  16. }
  17. }
  18. public void addCombination(int code, int[] comb)
  19. {
  20. combinations[code].Add(comb);
  21. }
  22. public void addSingle(int code, int a, int b)
  23. {
  24. combinations[code].Add(new int[2] {a,b});
  25. }
  26. public List<int[]> getCombs(int code)
  27. {
  28. return combinations[code];
  29. }
  30. }
  31. class SymbolHandler
  32. {
  33. List<int[]> combinations;
  34. public SymbolHandler()
  35. {
  36. combinations = new List<int[]>();
  37. }
  38. public void addCombs(List<int[]> combs)
  39. {
  40. combinations.AddRange(combs);
  41. }
  42. public List<int[]> getCombs()
  43. {
  44. return combinations;
  45. }
  46. public int getMostPopularChar()
  47. {
  48. int[] many = new int[1000];
  49. for(int i = 0; i < combinations.Count; i++) {
  50. many[combinations[i][0]]++;
  51. many[combinations[i][1]]++;
  52. }
  53. return Array.IndexOf(many, many.Max());
  54. }
  55.  
  56. static Encoding encoding = Encoding.ASCII;
  57. public int[][] getTopChars()
  58. {
  59. int[] many = new int[1000];
  60. for (int i = 0; i < combinations.Count; i++)
  61. {
  62. many[combinations[i][0]]++;
  63. many[combinations[i][1]]++;
  64. }
  65. List<int> ke = new List<int>();
  66. List<int> val = new List<int>();
  67. for (int i = 0; i < many.Length; i++)
  68. {
  69. if(many[i] != 0)
  70. {
  71. ke.Add(i);
  72. val.Add(many[i]);
  73. }
  74. }
  75. int[] arrkeys = ke.ToArray();
  76. int[] arrvals = val.ToArray();
  77. Array.Sort(arrvals, arrkeys);
  78. Array.Reverse(arrvals);
  79. Array.Reverse(arrkeys);
  80.  
  81. return new int[][] {arrkeys, arrvals};
  82. }
  83. }
  84. class Nice
  85. {
  86. static string[] symbols = new String[] { " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "/", "*", "=", "^", "%", ".", ",", ":" };
  87.  
  88. static string changeWord(string str)
  89. {
  90. bool[] buk = new bool[28];
  91. string res = "";
  92. for (int i = 0; i < str.Length; i++)
  93. {
  94. int ch = Char.ToLower(str[i]) - 'a';
  95. if (!buk[ch])
  96. {
  97. res += str[i];
  98. buk[Char.ToLower(str[i]) - 'a'] = true;
  99. }
  100. }
  101. return res;
  102. }
  103. static Encoding encoding = Encoding.ASCII;
  104. static byte[] crypt(string text, string keying)
  105. {
  106. byte[] plainText = encoding.GetBytes(text);
  107. byte[] key = encoding.GetBytes(keying);
  108.  
  109.  
  110. // encrypt: key XOR plainText
  111.  
  112. byte[] cipherText = new byte[plainText.Length];
  113. new BitArray(key).Xor(new BitArray(plainText)).CopyTo(cipherText, 0);
  114.  
  115.  
  116. // decrypt: key XOR chipherText
  117.  
  118. //byte[] decripted = new byte[cipherText.Length];
  119. //new BitArray(key).Xor(new BitArray(cipherText)).CopyTo(decripted, 0);
  120.  
  121. return /*encoding.GetString(*/cipherText/*)*/;
  122. }
  123. static byte[] cryptBytes(byte[] plainText, byte[] key)
  124. {
  125. // encrypt: key XOR plainText
  126.  
  127. byte[] cipherText = new byte[plainText.Length];
  128. new BitArray(key).Xor(new BitArray(plainText)).CopyTo(cipherText, 0);
  129.  
  130.  
  131. // decrypt: key XOR chipherText
  132.  
  133. //byte[] decripted = new byte[cipherText.Length];
  134. //new BitArray(key).Xor(new BitArray(cipherText)).CopyTo(decripted, 0);
  135.  
  136. return /*encoding.GetString(*/cipherText/*)*/;
  137. }
  138. static byte cryptOneBytes(byte plainText, byte key)
  139. {
  140. return cryptBytes(new byte[] { plainText }, new byte[] { key })[0];
  141. }
  142. static byte[] bt(byte b)
  143. {
  144. return new Byte[] { b };
  145. }
  146. public static string HextoString(string InputText)
  147. {
  148.  
  149. byte[] bb = Enumerable.Range(0, InputText.Length)
  150. .Where(x => x % 2 == 0)
  151. .Select(x => Convert.ToByte(InputText.Substring(x, 2), 16))
  152. .ToArray();
  153. return System.Text.Encoding.ASCII.GetString(bb);
  154. // or System.Text.Encoding.UTF7.GetString
  155. // or System.Text.Encoding.UTF8.GetString
  156. // or System.Text.Encoding.Unicode.GetString
  157. // or etc.
  158. }
  159. public static byte[] HextoBytes(string InputText)
  160. {
  161.  
  162. byte[] bb = Enumerable.Range(0, InputText.Length)
  163. .Where(x => x % 2 == 0)
  164. .Select(x => Convert.ToByte(InputText.Substring(x, 2), 16))
  165. .ToArray();
  166. return bb;
  167. }
  168. static string twoSybm(string str)
  169. {
  170. return (str.Length == 1 ? "0" + str : str);
  171. }
  172. static string ToHexString(byte[] hexstring)
  173. {
  174. StringBuilder sb = new StringBuilder();
  175. foreach (char t in hexstring)
  176. {
  177. //Note: X for upper, x for lower case letters
  178. sb.Append(Convert.ToInt32(t).ToString("x"));
  179. }
  180. return twoSybm(sb.ToString());
  181. }
  182. static string printHex(string hex)
  183. {
  184. string res = "";
  185. for (int i = 0; i < hex.Length; i++)
  186. {
  187. if (i != 0 && i % 2 == 0)
  188. res += " ";
  189. res += hex[i];
  190. }
  191. return res;
  192. }
  193. static byte[][] texts = new byte[][]{ch("315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e"),
  194. ch("234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f"),
  195. ch("32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb"),
  196. ch("32510ba9aab2a8a4fd06414fb517b5605cc0aa0dc91a8908c2064ba8ad5ea06a029056f47a8ad3306ef5021eafe1ac01a81197847a5c68a1b78769a37bc8f4575432c198ccb4ef63590256e305cd3a9544ee4160ead45aef520489e7da7d835402bca670bda8eb775200b8dabbba246b130f040d8ec6447e2c767f3d30ed81ea2e4c1404e1315a1010e7229be6636aaa"),
  197. ch("3f561ba9adb4b6ebec54424ba317b564418fac0dd35f8c08d31a1fe9e24fe56808c213f17c81d9607cee021dafe1e001b21ade877a5e68bea88d61b93ac5ee0d562e8e9582f5ef375f0a4ae20ed86e935de81230b59b73fb4302cd95d770c65b40aaa065f2a5e33a5a0bb5dcaba43722130f042f8ec85b7c2070"),
  198. ch("32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d84aba34d8de287be84d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d79eccf52ff111284b4cc61d11902aebc66f2b2e436434eacc0aba938220b084800c2ca4e693522643573b2c4ce35050b0cf774201f0fe52ac9f26d71b6cf61a711cc229f77ace7aa88a2f19983122b11be87a59c355d25f8e4"),
  199. ch("32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af51373fd9b4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f9b13f1efff71ea313c8661dd9a4ce"),
  200. ch("315c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d943ba708b8a3574f40c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e9417df7c95bba410e9aa2ca24c5474da2f276baa3ac325918b2daada43d6712150441c2e04f6565517f317da9d3"),
  201. ch("271946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f40462f9cf57f4564186a2c1778f1543efa270bda5e933421cbe88a4a52222190f471e9bd15f652b653b7071aec59a2705081ffe72651d08f822c9ed6d76e48b63ab15d0208573a7eef027"),
  202. ch("466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83"),
  203. ch("32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904")
  204. };
  205.  
  206. static byte[] ch(string str) {
  207. return HextoBytes(str);
  208. }
  209.  
  210. static string alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,? ";
  211.  
  212. static void Main(string[] args)
  213. {
  214. CodesHandler ch = new CodesHandler();
  215. for(int i = 0; i < alph.Length; i++)
  216. {
  217. for(int j = i; j < alph.Length; j++)
  218. {
  219. byte[] ttext = encoding.GetBytes(Char.ToString(alph[i])); //with spaces 97-122 for A-Z
  220. byte[] kkeying = encoding.GetBytes(Char.ToString(alph[j]));
  221. byte[] ccrypted = cryptBytes(ttext, kkeying);
  222. ch.addSingle(ccrypted[0], ttext[0], kkeying[0]);
  223. }
  224. }
  225. int kk = 5;//номер текста
  226. //int sss = 1; //номер символа //2 text: the n ie cx bn
  227. for(int sss = 30; sss < 50; sss++) { //6 text: there are a(uy)(ie) t(yw)(po) types of cryptography
  228.  
  229. SymbolHandler sh1 = new SymbolHandler();
  230. for (int i = 0; i < texts.Length; i++) //2 text: the ni
  231. { //2 text: the ne
  232. if (i == kk) //
  233. continue;
  234. sh1.addCombs(ch.getCombs(cryptOneBytes(texts[i][sss], texts[kk][sss])));
  235. //Console.WriteLine("CODING {1} xor {2} = {0}", cryptOneBytes(texts[i][sss], texts[kk][sss]), texts[i][sss], texts[kk][sss]);
  236. }
  237. int[][] getting = sh1.getTopChars();
  238.  
  239. //getting[0][0] - топ символ
  240. int[] stats = new int[10000];
  241. //if (getting[1][0] != getting[1][1])
  242. stats[cryptOneBytes((byte)getting[0][0], texts[kk][sss])]++;
  243. for (int opo = 1; opo < getting[0].Length; opo++)
  244. {
  245. if (getting[1][opo] == getting[1][0])
  246. {
  247. stats[cryptOneBytes((byte)getting[0][opo], texts[kk][sss])]++;
  248. }
  249. }
  250. for (int p = 0; p < texts.Length; p++)
  251. {
  252. if (p == kk)
  253. continue;
  254. SymbolHandler nowsh2 = new SymbolHandler();
  255. for (int i = 0; i < texts.Length; i++) //2 text: the ni
  256. { //2 text: the ne
  257. if (i == p) //
  258. continue;
  259. nowsh2.addCombs(ch.getCombs(cryptOneBytes(texts[i][sss], texts[p][sss])));
  260. //Console.WriteLine("CODING {1} xor {2} = {0}", cryptOneBytes(texts[i][sss], texts[kk][sss]), texts[i][sss], texts[kk][sss]);
  261. }
  262. int[][] nowgetting = nowsh2.getTopChars();
  263.  
  264. //nowgetting[0][0] - топ символ
  265. for(int opo = 1; opo < nowgetting[0].Length; opo++)
  266. {
  267. if(nowgetting[1][opo] == nowgetting[1][0]) {
  268. stats[cryptOneBytes((byte)nowgetting[0][opo], texts[p][sss])]++;
  269. }
  270. }
  271.  
  272. //if (nowgetting[1][0] != nowgetting[1][1])
  273. stats[cryptOneBytes((byte)nowgetting[0][0], texts[p][sss])]++;
  274. }
  275. Console.WriteLine("RESULTED: " + encoding.GetString(new byte[] { cryptOneBytes((byte)Array.IndexOf(stats, stats.Max()), texts[kk][sss]) }));
  276.  
  277. char[] res = new char[getting[0].Length];
  278. int cccnt = 0;
  279. for (int i = 0; i < getting[0].Length; i++)
  280. {
  281. res[i] = (encoding.GetString(new byte[] { (byte)getting[0][i] }))[0];
  282. if(cccnt < 3) Console.WriteLine(res[i] + " - " + getting[1][i] + " times");
  283. cccnt++;
  284. }
  285. Console.WriteLine(" ");
  286. }
  287.  
  288. //Console.WriteLine("most popular "+encoding.GetString(new byte[] { (byte)sh1.getMostPopularChar() }));
  289.  
  290. /*byte[] text = HextoBytes("315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b22");
  291. byte[] keying = HextoBytes("234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f");
  292. byte[] crypted = cryptBytes(text, keying);
  293. */
  294. //byte[] text = encoding.GetBytes("abcdefghijklmnopqrstuvwxyz"); //with spaces 65-90 for a-z
  295. byte[] text = encoding.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); //with spaces 97-122 for A-Z
  296. byte[] keying = encoding.GetBytes("..........................");
  297. byte[] crypted = cryptBytes(text, keying);
  298.  
  299. byte[] text1 = HextoBytes("315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc4");
  300. byte[] keying1 = HextoBytes("32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb");
  301. //byte[] text = HextoBytes("271946f9"); //HextoBytes("32510b");
  302. //byte[] keying = new Byte[] { 102, 57, 110};//102 57 110 The
  303. //Разница между заглавными буквами = 32 = 2^8
  304. byte[] crypted1 = cryptBytes(text1, keying1);
  305. for (int i = 0; i < Math.Min(text.Length, text1.Length); i++)
  306. {
  307. 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])));
  308. }
  309. Console.WriteLine("\n Result: \n {0} \n =to= \n {1}", text, encoding.GetString(crypted));
  310.  
  311.  
  312. Console.WriteLine("\n comparing: \n {0} \n =with= \n {1}", printHex(ToHexString(crypted)), printHex(ToHexString(crypted1)));
  313.  
  314. //диапазон 0-31
  315. //заглавная буква +32 к разнице
  316. //То есть числа > 31 - заглавные буквы
  317. //Пробел - диапазон от 65-90 (ш16-я с. с. 41-5a)
  318. //Точка 1-93 с заглавными
  319. //Точка 0-93 с заглавными
  320. int max = 0;
  321. char[] maxi = new char[] { 'A', 'A' };
  322. int min = int.MaxValue;
  323. char[] mini = new char[] { 'A', 'A' };
  324. for (byte i = 0; i < 26; i++)
  325. {
  326. for (byte j = 0; j < 26; j++)
  327. {
  328. char ct = (char)('A' + i);
  329. char ct2 = (char)('a' + j);
  330. byte[] cry = crypt(Char.ToString(ct), Char.ToString(ct2));
  331. if (cry[0] >= max)
  332. {
  333. max = cry[0];
  334. maxi = new char[] { ct, ct2 };
  335. }
  336. if (cry[0] <= min)
  337. {
  338. min = cry[0];
  339. mini = new char[] { ct, ct2 };
  340. }
  341. }
  342. }
  343. Console.WriteLine("MAX: " + max + " = " + maxi[0] + " xor " + maxi[1]);
  344. Console.WriteLine("MIN: " + min + " = " + mini[0] + " xor " + mini[1]);
  345.  
  346. //Console.WriteLine(HextoString("32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904") + " - eto");
  347. Console.ReadKey();
  348. }
  349. }
  350. /*
  351. using System;
  352. using System.Collections.Generic;
  353. using System.IO;
  354. using System.Linq;
  355.  
  356. class Nice {
  357.  
  358. static string[] symbols = new String[] { " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "/", "*", "=", "^", "%", ".", ",", ":" };
  359.  
  360. static string changeWord(string str) {
  361. bool[] buk = new bool[28];
  362. string res = "";
  363. for (int i = 0; i < str.Length; i++) {
  364. int ch = Char.ToLower(str[i]) - 'a';
  365. if (!buk[ch]) {
  366. res += str[i];
  367. buk[Char.ToLower(str[i]) - 'a'] = true;
  368. }
  369. }
  370. return res;
  371. }
  372.  
  373. static void Main(string[] args) {
  374. string[] lines = File.ReadAllLines("input.txt");
  375. string[] lastline = Array.ConvertAll((lines[lines.Length - 1]).Split(symbols, StringSplitOptions.RemoveEmptyEntries), changeWord);
  376. foreach (string line in lines) {
  377. string[] words = Array.ConvertAll(line.Split(symbols, StringSplitOptions.RemoveEmptyEntries), changeWord);
  378. foreach (string str in words)
  379. if (str != lastline[lastline.Length - 1])
  380. Console.Write(str + " ");
  381. }
  382. Console.ReadKey();
  383. }
  384. }*/
  385.  
  386.  
  387.  
  388.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement