Ejemplo n.º 1
0
        /// <summary>
        /// Run a test case, evaluate the result, and print the evaulation to the console
        /// </summary>
        /// <param name="testString">The string to be encrypted</param>
        /// <param name="testTitle">The name of the test case</param>
        /// <returns>True if the testString encrypted then decrypted and ended up equal to testString, false otherwise </returns>
        private static Boolean Test(String testString, String testTitle)
        {
            String text = testString;

            Console.WriteLine(testTitle + ": Starting with " + text);
            Byte[] encryptedText;
            encryptedText = Step01.Encrypt(Encoding.ASCII.GetBytes(text));
            encryptedText = Step02.Encrypt(encryptedText, 'q', 2);
            encryptedText = Step03.Encrypt(encryptedText);
            encryptedText = Step04.Encrypt(encryptedText);
            encryptedText = Step05.Encrypt(encryptedText);
            encryptedText = Step06.Encrypt(encryptedText);
            encryptedText = Step07.Encrypt(encryptedText);
            encryptedText = Step08.Encrypt(encryptedText);
            encryptedText = Step09.Encrypt(encryptedText);
            int[] mapping = BuildRandomMapping();
            encryptedText = Step10.Encrypt(encryptedText, mapping);
            // Slip Step 11 because it's a one-way encryption
            encryptedText = Step12.Encrypt(encryptedText);

            Console.WriteLine("Decrypting " + encryptedText);

            Byte[] decryptedText;
            decryptedText = Step12.Decrypt(encryptedText);
            // Slip Step 11 because it's a one-way encryption
            decryptedText = Step10.Decrypt(decryptedText, mapping);
            decryptedText = Step09.Decrypt(decryptedText);
            decryptedText = Step08.Decrypt(decryptedText);
            decryptedText = Step07.Decrypt(decryptedText);
            decryptedText = Step06.Decrypt(decryptedText);
            decryptedText = Step05.Decrypt(decryptedText);
            decryptedText = Step04.Decrypt(decryptedText);
            decryptedText = Step03.Decrypt(decryptedText);
            decryptedText = Step02.Decrypt(decryptedText, 2);
            decryptedText = Step01.Decrypt(decryptedText);

            Console.WriteLine("Result = " + decryptedText);
            if (text.Equals(decryptedText))
            {
                Console.WriteLine(testTitle + " Passed");
                return(true);
            }
            else
            {
                Console.WriteLine(testTitle + " FAILED");
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary> Run a single test case and print every intermediate step for debugging.
        /// The results of each step are printed but not evaluated. </summary>
        private static void PerformStepByStepTest()
        {
            String clearText = "abcde";

            Console.WriteLine("Starting with " + clearText);
            Console.WriteLine("Encrypting...");
            // Step 01
            Byte[] encryptedTextStep01 = Step01.Encrypt(clearText);
            Console.WriteLine("Step 01: " + encryptedTextStep01);
            Byte[] decryptedTextStep01 = Step01.Decrypt(encryptedTextStep01);
            Console.WriteLine("         Decrypts to " + decryptedTextStep01);
            // Step 02
            Byte[] encryptedTextStep02 = Step02.Encrypt(encryptedTextStep01, 'q', 2);
            Console.WriteLine("Step 02: " + encryptedTextStep02);
            Byte[] decryptedTextStep02 = Step02.Decrypt(encryptedTextStep02, 2);
            Console.WriteLine("         Decrypts to " + decryptedTextStep02);
            //Step 03
            Byte[] encryptedTextStep03 = Step03.Encrypt(encryptedTextStep02);
            Console.WriteLine("Step 03: " + encryptedTextStep03);
            Byte[] decryptedTextStep03 = Step03.Decrypt(encryptedTextStep03);
            Console.WriteLine("         Decrypts to " + decryptedTextStep03);
            //Step 04
            Byte[] encryptedTextStep04 = Step04.Encrypt(encryptedTextStep03);
            Console.WriteLine("Step 04: " + encryptedTextStep04);
            Byte[] decryptedTextStep04 = Step04.Decrypt(encryptedTextStep04);
            Console.WriteLine("         Decrypts to " + decryptedTextStep04);
            //Step 05
            Byte[] encryptedTextStep05 = Step05.Encrypt(encryptedTextStep04);
            Console.WriteLine("Step 05: " + encryptedTextStep05);
            Byte[] decryptedTextStep05 = Step05.Decrypt(encryptedTextStep05);
            Console.WriteLine("         Decrypts to " + decryptedTextStep05);
            //Step 06
            Byte[] encryptedTextStep06 = Step06.Encrypt(encryptedTextStep05);
            Console.WriteLine("Step 06: " + encryptedTextStep06);
            Byte[] decryptedTextStep06 = Step06.Decrypt(encryptedTextStep06);
            Console.WriteLine("         Decrypts to " + decryptedTextStep06);
            //Step 07
            Byte[] encryptedTextStep07 = Step07.Encrypt(encryptedTextStep06);
            Console.WriteLine("Step 07: " + encryptedTextStep07);
            Byte[] decryptedTextStep07 = Step07.Decrypt(encryptedTextStep07);
            Console.WriteLine("         Decrypts to " + decryptedTextStep07);
            //Step 08
            Byte[] encryptedTextStep08 = Step08.Encrypt(encryptedTextStep07);
            Console.WriteLine("Step 08: " + encryptedTextStep08);
            Byte[] decryptedTextStep08 = Step08.Decrypt(encryptedTextStep08);
            Console.WriteLine("         Decrypts to " + decryptedTextStep08);
            //Step 09
            Byte[] encryptedTextStep09 = Step09.Encrypt(encryptedTextStep08);
            Console.WriteLine("Step 09: " + encryptedTextStep09);
            Byte[] decryptedTextStep09 = Step09.Decrypt(encryptedTextStep09);
            Console.WriteLine("         Decrypts to " + decryptedTextStep09);
            //Step 10
            int[]  mapping             = BuildRandomMapping();
            Byte[] encryptedTextStep10 = Step10.Encrypt(encryptedTextStep09, mapping);
            Console.WriteLine("Step 10: " + encryptedTextStep10);
            Byte[] decryptedTextStep10 = Step10.Decrypt(encryptedTextStep10, mapping);
            Console.WriteLine("         Decrypts to " + decryptedTextStep10);
            // We skip Step 11 because it uses one-way encryption
            // Step 12
            Byte[] encryptedTextStep12 = Step12.Encrypt(encryptedTextStep10);
            Console.WriteLine("Step 12: " + encryptedTextStep12);
            Byte[] decryptedTextStep12 = Step12.Decrypt(encryptedTextStep12);
            Console.WriteLine("         Decrypts to " + decryptedTextStep12);
        }