Advertisement
Pearlfromsu

Untitled

Nov 6th, 2021
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 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"); //with spaces 65-90 for a-z
  103. byte[] text = encoding.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); //with spaces 97-122 for A-Z
  104. byte[] keying = encoding.GetBytes("..........................");
  105. byte[] crypted = cryptBytes(text, keying);
  106.  
  107. byte[] text1 = HextoBytes("315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc4");
  108. byte[] keying1 = HextoBytes("32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb");
  109. //byte[] text = HextoBytes("271946f9"); //HextoBytes("32510b");
  110. //byte[] keying = new Byte[] { 102, 57, 110};//102 57 110 The
  111. //Разница между заглавными буквами = 32 = 2^8
  112. byte[] crypted1 = cryptBytes(text1, keying1);
  113. for (int i = 0; i < Math.Min(text.Length, text1.Length); i++) {
  114. 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])));
  115. }
  116. Console.WriteLine("\n Result: \n {0} \n =to= \n {1}", text, encoding.GetString(crypted));
  117.  
  118.  
  119. Console.WriteLine("\n comparing: \n {0} \n =with= \n {1}", printHex(ToHexString(crypted)), printHex(ToHexString(crypted1)));
  120.  
  121. //диапазон 0-31
  122. //заглавная буква +32 к разнице
  123. //То есть числа > 31 - заглавные буквы
  124. //Пробел - диапазон от 65-90 (ш16-я с. с. 41-5a)
  125. //Точка 1-93 с заглавными
  126. //Точка 0-93 с заглавными
  127. int max = 0;
  128. char[] maxi = new char[] { 'A', 'A' };
  129. int min = int.MaxValue;
  130. char[] mini = new char[] { 'A', 'A' };
  131. for (byte i = 0; i < 26; i++) {
  132. for (byte j = 0; j < 26; j++) {
  133. char ct = (char)('A' + i);
  134. char ct2 = (char)('a' + j);
  135. byte[] cry = crypt(Char.ToString(ct), Char.ToString(ct2));
  136. if (cry[0] >= max) {
  137. max = cry[0];
  138. maxi = new char[] { ct, ct2 };
  139. }
  140. if (cry[0] <= min) {
  141. min = cry[0];
  142. mini = new char[] { ct, ct2 };
  143. }
  144. }
  145. }
  146. Console.WriteLine("MAX: " + max + " = " + maxi[0] + " xor " + maxi[1]);
  147. Console.WriteLine("MIN: " + min + " = " + mini[0] + " xor " + mini[1]);
  148.  
  149. //Console.WriteLine(HextoString("32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904") + " - eto");
  150. Console.ReadKey();
  151. }
  152. }
  153. /*
  154. using System;
  155. using System.Collections.Generic;
  156. using System.IO;
  157. using System.Linq;
  158.  
  159. class Nice {
  160.  
  161. static string[] symbols = new String[] { " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "/", "*", "=", "^", "%", ".", ",", ":" };
  162.  
  163. static string changeWord(string str) {
  164. bool[] buk = new bool[28];
  165. string res = "";
  166. for (int i = 0; i < str.Length; i++) {
  167. int ch = Char.ToLower(str[i]) - 'a';
  168. if (!buk[ch]) {
  169. res += str[i];
  170. buk[Char.ToLower(str[i]) - 'a'] = true;
  171. }
  172. }
  173. return res;
  174. }
  175.  
  176. static void Main(string[] args) {
  177. string[] lines = File.ReadAllLines("input.txt");
  178. string[] lastline = Array.ConvertAll((lines[lines.Length - 1]).Split(symbols, StringSplitOptions.RemoveEmptyEntries), changeWord);
  179. foreach (string line in lines) {
  180. string[] words = Array.ConvertAll(line.Split(symbols, StringSplitOptions.RemoveEmptyEntries), changeWord);
  181. foreach (string str in words)
  182. if (str != lastline[lastline.Length - 1])
  183. Console.Write(str + " ");
  184. }
  185. Console.ReadKey();
  186. }
  187. }*/
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement