Advertisement
Pearlfromsu

LABA 4

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