/* Convert hex to base64 * The string: * "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" * Should produce: * "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t" * So go ahead and make that happen. You'll need to use this code for the rest of the exercises. * Cryptopals Rule * Always operate on raw bytes, never on encoded strings. Only use hex and base64 for pretty-printing. */ public static void Set1Challenge1() { string input = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"; string expect = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"; string output = Convert.ToBase64String(BinaryOperations.HexToBytes(input)); Console.WriteLine("Set 1 Challenge 1: " + expect.ToUpper().Equals(output.ToUpper())); Console.WriteLine("Set 1 Challenge 1: (input) " + Encoding.ASCII.GetString(BinaryOperations.HexToBytes(input))); }
/* Single-byte XOR cipher * The hex encoded string: * "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736" * ... has been XOR'd against a single character. Find the key, decrypt the message. * You can do this by hand. But don't: write code to do it for you. * How? Devise some method for "scoring" a piece of English plaintext. Character frequency is a good metric. Evaluate each output and choose the one with the best score. * Achievement Unlocked * You now have our permission to make "ETAOIN SHRDLU" jokes on Twitter. */ public static void Set1Challenge3() { string str = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"; List <byte[]> input = new List <byte[]> { BinaryOperations.HexToBytes(str) }; string output = Crypto.SingleXorDecrypt(input)[0].Item2; Console.WriteLine("Set 1 Challenge 3: " + output); }
/* Fixed XOR * Write a function that takes two equal-length buffers and produces their XOR combination. * If your function works properly, then when you feed it the string: * "1c0111001f010100061a024b53535009181c" * ... after hex decoding, and when XOR'd against: * "686974207468652062756c6c277320657965" * ... should produce: * "746865206b696420646f6e277420706c6179" */ public static void Set1Challenge2() { string input = "1c0111001f010100061a024b53535009181c"; string key = "686974207468652062756c6c277320657965"; string expect = "746865206b696420646f6e277420706c6179"; string output = BitConverter.ToString(BinaryOperations.Xor(BinaryOperations.HexToBytes(input), BinaryOperations.HexToBytes(key))).Replace("-", string.Empty); Console.WriteLine("Set 1 Challenge 2: " + expect.ToUpper().Equals(output.ToUpper())); Console.WriteLine("Set 1 Challenge 2: (key) " + Encoding.ASCII.GetString(BinaryOperations.HexToBytes(key))); Console.WriteLine("Set 1 Challenge 2: (expect) " + Encoding.ASCII.GetString(BinaryOperations.HexToBytes(expect))); }
/* Detect single-character XOR * One of the 60-character strings in this file(@"../../set1challenge4.txt") has been encrypted by single-character XOR. * Find it. * (Your code from #3 should help.) */ public static void Set1Challenge4() { string[] lines = File.ReadAllLines(@"../../../data/set1/4.txt"); List <byte[]> input = new List <byte[]>(); foreach (string line in lines) { input.Add(BinaryOperations.HexToBytes(line)); } string output = Crypto.SingleXorDecrypt(input)[0].Item2; Console.WriteLine("Set 1 Challenge 4: " + output.Trim()); }