static byte[] function1(string inputString) { const string PREPEND_STRING = "comment1=cooking%20MCs;userdata="; const string APPEND_STRING = ";comment2=%20like%20a%20pound%20of%20bacon"; byte[] key = new byte[] { 10, 11, 23, 44, 51, 23, 34, 89, 101, 65, 43, 71, 92, 41, 85, 03 }; byte[] IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; //Construct the string string unmodified = PREPEND_STRING + inputString + APPEND_STRING; //Escape the required characters string modified = unmodified.Replace(";", " ").Replace("=", " "); //Convert to bytes byte[] plainBytes = Encoding.ASCII.GetBytes(modified); //Apply appropriate padding byte[] paddedBytes = CryptoHelpers.Pkcs7PaddUp(plainBytes); //Encrypt under CBC with key byte[] encryptedBytes = CryptoHelpers.CbcEncrypt(paddedBytes, key, IV); return(encryptedBytes); }
static string challenge11() { //Create plaintext string and random key. byte[] plainText = Encoding.ASCII.GetBytes( "\"Nothing is particularly hard if you divide it into small jobs.\" — Henry Ford"); byte[] key = new byte[16]; Random random = new Random(); random.NextBytes(key); //Create padding arrays and fill with random bytes byte[] firstPad = new byte[random.Next(5, 10)]; random.NextBytes(firstPad); byte[] secondPad = new byte[random.Next(5, 10)]; random.NextBytes(secondPad); //Create an array to store randomized plain text, //then combine data from existing arrays into plainBytes byte[] plainBytes = new byte[firstPad.Length + plainText.Length + secondPad.Length]; Array.Copy(firstPad, 0, plainBytes, 0, firstPad.Length); Array.Copy(plainText, 0, plainBytes, firstPad.Length, plainText.Length); Array.Copy(secondPad, 0, plainBytes, firstPad.Length + plainText.Length, secondPad.Length); byte[] IVector = new byte[] { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00 }; byte[] encrpyted = CryptoHelpers.Pkcs7PaddUp(plainBytes); int whichEncrypyMethod = random.Next(2); whichEncrypyMethod = 1; //Attempt encryption functions. if (whichEncrypyMethod == 0) { encrpyted = CryptoHelpers.CbcEncrypt(encrpyted, key, IVector); Console.WriteLine("Encrypted with CBC decryption"); } else if (whichEncrypyMethod == 1) { encrpyted = CryptoHelpers.EcbEncrypt(encrpyted, key); Console.WriteLine("Encrypted with ECB decryption"); } else { Console.WriteLine("Else statement hit, this is an issue"); } Console.WriteLine("Encrypted: {0}", Encoding.Default.GetString(encrpyted)); //Attempt decryption. byte[] decrypted = new byte[encrpyted.Length]; try { decrypted = CryptoHelpers.CbcDecrypt(encrpyted, key, IVector); Console.WriteLine("Decrypted: {0}", Encoding.Default.GetString(decrypted)); Console.WriteLine("Decrypted with CBC decryption"); } catch (Exception e) { Console.WriteLine(e); try { decrypted = CryptoHelpers.EcbDecrypt(encrpyted, key); Console.WriteLine("Decrypted: {0}", Encoding.Default.GetString(decrypted)); Console.WriteLine("Decrypted with ECB decryption"); } catch (Exception E) { Console.WriteLine(E); Console.WriteLine("Back to the drawing board."); } } int test = CryptoHelpers.ECBorCBC(encrpyted, key); Console.WriteLine(test); return(""); }