Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- class CodesHandler {
- List<int[]>[] combinations;
- public CodesHandler() {
- combinations = new List<int[]>[1000];
- for (int i = 0; i < 1000; i++) {
- combinations[i] = new List<int[]>();
- }
- }
- public void addCombination(int code, int[] comb) {
- combinations[code].Add(comb);
- }
- public void addSingle(int code, int a, int b) {
- combinations[code].Add(new int[2] { a, b });
- }
- public List<int[]> getCombs(int code) {
- return combinations[code];
- }
- }
- class SymbolHandler {
- List<int[]> combinations;
- public SymbolHandler() {
- combinations = new List<int[]>();
- }
- public void addCombs(List<int[]> combs) {
- combinations.AddRange(combs);
- }
- public List<int[]> getCombs() {
- return combinations;
- }
- public int getMostPopularChar() {
- int[] many = new int[1000];
- for (int i = 0; i < combinations.Count; i++) {
- many[combinations[i][0]]++;
- many[combinations[i][1]]++;
- }
- return Array.IndexOf(many, many.Max());
- }
- static Encoding encoding = Encoding.ASCII;
- public int[][] getTopChars() {
- int[] many = new int[1000];
- for (int i = 0; i < combinations.Count; i++) {
- many[combinations[i][0]]++;
- many[combinations[i][1]]++;
- }
- List<int> ke = new List<int>();
- List<int> val = new List<int>();
- for (int i = 0; i < many.Length; i++) {
- if (many[i] != 0) {
- ke.Add(i);
- val.Add(many[i]);
- }
- }
- int[] arrkeys = ke.ToArray();
- int[] arrvals = val.ToArray();
- Array.Sort(arrvals, arrkeys);
- Array.Reverse(arrvals);
- Array.Reverse(arrkeys);
- return new int[][] { arrkeys, arrvals };
- }
- }
- class Nice {
- static string[] symbols = new String[] { " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "/", "*", "=", "^", "%", ".", ",", ":" };
- static string changeWord(string str) {
- bool[] buk = new bool[28];
- string res = "";
- for (int i = 0; i < str.Length; i++) {
- int ch = Char.ToLower(str[i]) - 'a';
- if (!buk[ch]) {
- res += str[i];
- buk[Char.ToLower(str[i]) - 'a'] = true;
- }
- }
- return res;
- }
- static Encoding encoding = Encoding.ASCII;
- static byte[] crypt(string text, string keying) {
- byte[] plainText = encoding.GetBytes(text);
- byte[] key = encoding.GetBytes(keying);
- // encrypt: key XOR plainText
- byte[] cipherText = new byte[plainText.Length];
- new BitArray(key).Xor(new BitArray(plainText)).CopyTo(cipherText, 0);
- // decrypt: key XOR chipherText
- //byte[] decripted = new byte[cipherText.Length];
- //new BitArray(key).Xor(new BitArray(cipherText)).CopyTo(decripted, 0);
- return /*encoding.GetString(*/cipherText/*)*/;
- }
- static byte[] cutLength(byte[] arr, int how) {
- how = arr.Length < how ? arr.Length : how; //min()
- byte[] res = new byte[how];
- for (int i = 0; i < how; i++) {
- res[i] = arr[i];
- }
- return res;
- }
- static byte[] cryptBytes(byte[] plainText, byte[] key) {
- //равняем длины обеих текстов
- int minlength = plainText.Length < key.Length ? plainText.Length : key.Length;
- plainText = cutLength(plainText, minlength);
- key = cutLength(key, minlength);
- byte[] cipherText = new byte[plainText.Length];
- new BitArray(key).Xor(new BitArray(plainText)).CopyTo(cipherText, 0);
- return cipherText;
- }
- static byte cryptOneBytes(byte plainText, byte key) {
- return cryptBytes(new byte[] { plainText }, new byte[] { key })[0];
- }
- static byte[] bt(byte b) {
- return new Byte[] { b };
- }
- public static string HextoString(string InputText) {
- byte[] bb = Enumerable.Range(0, InputText.Length)
- .Where(x => x % 2 == 0)
- .Select(x => Convert.ToByte(InputText.Substring(x, 2), 16))
- .ToArray();
- return System.Text.Encoding.ASCII.GetString(bb);
- // or System.Text.Encoding.UTF7.GetString
- // or System.Text.Encoding.UTF8.GetString
- // or System.Text.Encoding.Unicode.GetString
- // or etc.
- }
- public static byte[] HextoBytes(string InputText) {
- byte[] bb = Enumerable.Range(0, InputText.Length)
- .Where(x => x % 2 == 0)
- .Select(x => Convert.ToByte(InputText.Substring(x, 2), 16))
- .ToArray();
- return bb;
- }
- static string twoSybm(string str) {
- return (str.Length == 1 ? "0" + str : str);
- }
- static string ToHexString(byte[] hexstring) {
- StringBuilder sb = new StringBuilder();
- foreach (char t in hexstring) {
- //Note: X for upper, x for lower case letters
- sb.Append(Convert.ToInt32(t).ToString("x"));
- }
- return twoSybm(sb.ToString());
- }
- static string printHex(string hex) {
- string res = "";
- for (int i = 0; i < hex.Length; i++) {
- if (i != 0 && i % 2 == 0)
- res += " ";
- res += hex[i];
- }
- return res;
- }
- static byte[][] texts = new byte[][]{ch("315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e"),
- ch("234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f"),
- ch("32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb"),
- ch("32510ba9aab2a8a4fd06414fb517b5605cc0aa0dc91a8908c2064ba8ad5ea06a029056f47a8ad3306ef5021eafe1ac01a81197847a5c68a1b78769a37bc8f4575432c198ccb4ef63590256e305cd3a9544ee4160ead45aef520489e7da7d835402bca670bda8eb775200b8dabbba246b130f040d8ec6447e2c767f3d30ed81ea2e4c1404e1315a1010e7229be6636aaa"),
- ch("3f561ba9adb4b6ebec54424ba317b564418fac0dd35f8c08d31a1fe9e24fe56808c213f17c81d9607cee021dafe1e001b21ade877a5e68bea88d61b93ac5ee0d562e8e9582f5ef375f0a4ae20ed86e935de81230b59b73fb4302cd95d770c65b40aaa065f2a5e33a5a0bb5dcaba43722130f042f8ec85b7c2070"),
- ch("32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d84aba34d8de287be84d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d79eccf52ff111284b4cc61d11902aebc66f2b2e436434eacc0aba938220b084800c2ca4e693522643573b2c4ce35050b0cf774201f0fe52ac9f26d71b6cf61a711cc229f77ace7aa88a2f19983122b11be87a59c355d25f8e4"),
- ch("32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af51373fd9b4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f9b13f1efff71ea313c8661dd9a4ce"),
- ch("315c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d943ba708b8a3574f40c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e9417df7c95bba410e9aa2ca24c5474da2f276baa3ac325918b2daada43d6712150441c2e04f6565517f317da9d3"),
- ch("271946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f40462f9cf57f4564186a2c1778f1543efa270bda5e933421cbe88a4a52222190f471e9bd15f652b653b7071aec59a2705081ffe72651d08f822c9ed6d76e48b63ab15d0208573a7eef027"),
- ch("466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83"),
- ch("32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904")
- };
- static byte[] ch(string str) {
- return HextoBytes(str);
- }
- static string alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,!? ";
- static void Main(string[] args) {
- CodesHandler ch = new CodesHandler();
- for (int i = 0; i < alph.Length; i++) {
- for (int j = i; j < alph.Length; j++) {
- byte[] ttext = encoding.GetBytes(Char.ToString(alph[i])); //with spaces 97-122 for A-Z
- byte[] kkeying = encoding.GetBytes(Char.ToString(alph[j]));
- byte[] ccrypted = cryptBytes(ttext, kkeying);
- ch.addSingle(ccrypted[0], ttext[0], kkeying[0]);
- }
- }
- int kk = 10;//номер текста
- //int sss = 1; //номер символа //2 text: the n ie cx bn
- string ress = "";
- 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
- //uiau uiat uhau uhat !!!(utk)(hi)a(ut)!!!
- //tiat tiau thau !!!that!!!
- SymbolHandler sh1 = new SymbolHandler();
- for (int i = 0; i < texts.Length; i++) //2 text: the ni
- { //2 text: the ne
- if (i == kk) //
- continue;
- sh1.addCombs(ch.getCombs(cryptOneBytes(texts[i][sss], texts[kk][sss])));
- //Console.WriteLine("CODING {1} xor {2} = {0}", cryptOneBytes(texts[i][sss], texts[kk][sss]), texts[i][sss], texts[kk][sss]);
- }
- int[][] getting = sh1.getTopChars();
- //getting[0][0] - топ символ
- int[] stats = new int[10000];
- //if (getting[1][0] != getting[1][1])
- stats[cryptOneBytes((byte)getting[0][0], texts[kk][sss])]++;
- for (int opo = 1; opo < getting[0].Length; opo++) {
- if (getting[1][opo] == getting[1][0]) {
- stats[cryptOneBytes((byte)getting[0][opo], texts[kk][sss])]++;
- }
- }
- for (int p = 0; p < texts.Length; p++) {
- if (p == kk)
- continue;
- SymbolHandler nowsh2 = new SymbolHandler();
- for (int i = 0; i < texts.Length; i++) //2 text: the ni
- { //2 text: the ne
- if (i == p) //
- continue;
- nowsh2.addCombs(ch.getCombs(cryptOneBytes(texts[i][sss], texts[p][sss])));
- //Console.WriteLine("CODING {1} xor {2} = {0}", cryptOneBytes(texts[i][sss], texts[kk][sss]), texts[i][sss], texts[kk][sss]);
- }
- int[][] nowgetting = nowsh2.getTopChars();
- //nowgetting[0][0] - топ символ
- for (int opo = 1; opo < nowgetting[0].Length; opo++) {
- if (nowgetting[1][opo] == nowgetting[1][0]) {
- stats[cryptOneBytes((byte)nowgetting[0][opo], texts[p][sss])]++;
- }
- }
- //if (nowgetting[1][0] != nowgetting[1][1])
- if (nowgetting[0].Length > 0)
- stats[cryptOneBytes((byte)nowgetting[0][0], texts[p][sss])]++;
- }
- Console.WriteLine("RESULTED: " + encoding.GetString(new byte[] { cryptOneBytes((byte)Array.IndexOf(stats, stats.Max()), texts[kk][sss]) }));
- char[] res = new char[getting[0].Length];
- int cccnt = 0;
- for (int i = 0; i < getting[0].Length; i++) {
- res[i] = (encoding.GetString(new byte[] { (byte)getting[0][i] }))[0];
- if (cccnt < 4)
- Console.WriteLine(res[i] + " - " + getting[1][i] + " times");
- cccnt++;
- }
- ress += res[0];
- Console.WriteLine(" ");
- }
- Console.WriteLine("RESS = "+ress);
- Console.WriteLine(" ");
- byte[] keyForEverything = cryptBytes(encoding.GetBytes("The secret message is: When using a stream cipher, never use the key more than once"), texts[10]);
- Console.WriteLine("The password is:");
- Console.WriteLine(ToHexString(keyForEverything));
- Console.WriteLine(encoding.GetString(keyForEverything));
- Console.WriteLine(" ");
- Console.WriteLine("Resulted texts: ");
- for (int i = 0; i < 11; i++) {
- Console.WriteLine(encoding.GetString(cryptBytes(texts[i], keyForEverything)));
- }
- //Twz sdcueu!czslage is, Vhzn using a rtseam ciphes, qzver use twe kdx momd timo onbz
- //The secret message is: When using a stream cipher, never use the key more than once
- /*
- We can factor the number 15 with quantum computers. We can also factor the number 1
- Euler would probably enjoy that now his theorem becomes a corner stone of crypto -
- The nice thing about Keeyloq is now we cryptographers can drive a lot of fancy cars
- The ciphertext produced by a weak encryption algorithm looks as good as ciphertext
- You don't want to buy a set of car keys from a guy who specializes in stealing cars
- There are two types of cryptography - that which will keep secrets safe from your l
- There are two types of cyptography: one that allows the Government to use brute for
- We can see the point where the chip is unhappy if a wrong bit is sent and consumes
- A (private-key) encryption scheme states 3 algorithms, namely a procedure for gene
- The Concise OxfordDictionary (2006) defines crypto as the art of writing o r solv
- The secret message is: When using a stream cipher, never use the key more than once
- */
- //Console.WriteLine("most popular "+encoding.GetString(new byte[] { (byte)sh1.getMostPopularChar() }));
- /*byte[] text = HextoBytes("315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b22");
- byte[] keying = HextoBytes("234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f");
- byte[] crypted = cryptBytes(text, keying);
- */
- //byte[] text = encoding.GetBytes("abcdefghijklmnopqrstuvwxyz"); //with spaces 65-90 for a-z
- byte[] text = encoding.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); //with spaces 97-122 for A-Z
- byte[] keying = encoding.GetBytes("..........................");
- byte[] crypted = cryptBytes(text, keying);
- byte[] text1 = HextoBytes("315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc4");
- byte[] keying1 = HextoBytes("32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb");
- //byte[] text = HextoBytes("271946f9"); //HextoBytes("32510b");
- //byte[] keying = new Byte[] { 102, 57, 110};//102 57 110 The
- //Разница между заглавными буквами = 32 = 2^8
- byte[] crypted1 = cryptBytes(text1, keying1);
- for (int i = 0; i < Math.Min(text.Length, text1.Length); i++) {
- 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])));
- }
- Console.WriteLine("\n Result: \n {0} \n =to= \n {1}", text, encoding.GetString(crypted));
- Console.WriteLine("\n comparing: \n {0} \n =with= \n {1}", printHex(ToHexString(crypted)), printHex(ToHexString(crypted1)));
- //диапазон 0-31
- //заглавная буква +32 к разнице
- //То есть числа > 31 - заглавные буквы
- //Пробел - диапазон от 65-90 (ш16-я с. с. 41-5a)
- //Точка 1-93 с заглавными
- //Точка 0-93 с заглавными
- int max = 0;
- char[] maxi = new char[] { 'A', 'A' };
- int min = int.MaxValue;
- char[] mini = new char[] { 'A', 'A' };
- for (byte i = 0; i < 26; i++) {
- for (byte j = 0; j < 26; j++) {
- char ct = (char)('A' + i);
- char ct2 = (char)('a' + j);
- byte[] cry = crypt(Char.ToString(ct), Char.ToString(ct2));
- if (cry[0] >= max) {
- max = cry[0];
- maxi = new char[] { ct, ct2 };
- }
- if (cry[0] <= min) {
- min = cry[0];
- mini = new char[] { ct, ct2 };
- }
- }
- }
- Console.WriteLine("MAX: " + max + " = " + maxi[0] + " xor " + maxi[1]);
- Console.WriteLine("MIN: " + min + " = " + mini[0] + " xor " + mini[1]);
- //Console.WriteLine(HextoString("32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904") + " - eto");
- Console.ReadKey();
- }
- }
- /*
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- class Nice {
- static string[] symbols = new String[] { " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "/", "*", "=", "^", "%", ".", ",", ":" };
- static string changeWord(string str) {
- bool[] buk = new bool[28];
- string res = "";
- for (int i = 0; i < str.Length; i++) {
- int ch = Char.ToLower(str[i]) - 'a';
- if (!buk[ch]) {
- res += str[i];
- buk[Char.ToLower(str[i]) - 'a'] = true;
- }
- }
- return res;
- }
- static void Main(string[] args) {
- string[] lines = File.ReadAllLines("input.txt");
- string[] lastline = Array.ConvertAll((lines[lines.Length - 1]).Split(symbols, StringSplitOptions.RemoveEmptyEntries), changeWord);
- foreach (string line in lines) {
- string[] words = Array.ConvertAll(line.Split(symbols, StringSplitOptions.RemoveEmptyEntries), changeWord);
- foreach (string str in words)
- if (str != lastline[lastline.Length - 1])
- Console.Write(str + " ");
- }
- Console.ReadKey();
- }
- }*/
- //7 текст(индекс 6) самый длинный
- //There are two types of cyptography: one that allows the Government to use brute force to break the code,
- //and one that requires the Government to use brute force to break you
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement