コード例 #1
0
        public static bool Initialize()
        {
            //#1 https://cryptopals.com/sets/1/challenges/1
            if (string.Equals(Converter.ConvertHexToBase64(HexToConvert), HexConvertTarget))
            {
                System.Console.WriteLine("TEST: EX1: Converting hex to base64 successful!");
            }
            else
            {
                System.Console.WriteLine("TEST: EX1: Converting hex to base64 failed! Exiting...");
                return(false);
            }

            //#2 https://cryptopals.com/sets/1/challenges/2
            var output = XOR.XOREqualLengthInputs(Convert.FromBase64String(Converter.ConvertHexToBase64(Xor1)), Convert.FromBase64String(Converter.ConvertHexToBase64(Xor2)));

            if (String.Equals(output, ExpectedXor))
            {
                System.Console.WriteLine("TEST: EX2: XOR Successful!");
            }
            else
            {
                System.Console.WriteLine("TEST: EX2: XOR failed! Output: {0}", output);
                return(false);
            }

            //#3 https://cryptopals.com/sets/1/challenges/3
            var keyScores = new Dictionary <byte, int>();

            var keyOutput = new Dictionary <byte, string>();

            for (var idx = byte.MinValue; idx < byte.MaxValue; idx++)
            {
                var xorOutput = XOR.XORInputToByte(Convert.FromBase64String(Converter.ConvertHexToBase64(xord)), idx);

                keyOutput.Add(idx, xorOutput);

                keyScores.Add(idx, Frequency.ScoreFrequencies(xorOutput));
            }

            var topScores = keyScores.OrderByDescending(x => x.Value).First();

            var topOutput = keyOutput[topScores.Key];

            if (String.Equals(topOutput, ex3ExpectedAnswer))
            {
                System.Console.WriteLine("TEST: EX3: Single byte XOR successful!");
            }
            else
            {
                System.Console.WriteLine("TEST: EX3: Single byte XOR unsuccesful, output was {0}", topOutput);
            }

            //#4 https://cryptopals.com/sets/1/challenges/4

            var lines = File.ReadLines("TestFiles/4.txt").ToArray();

            var lineScores = new Dictionary <int, KeyValuePair <int, string> >();

            for (var lineNr = 0; lineNr < lines.Count(); lineNr++)
            {
                var byteOutput = new Dictionary <byte, string>();

                var byteScores = Frequency.ScoreInput(Convert.FromBase64String(Converter.ConvertHexToBase64(lines[lineNr])), out byteOutput);

                var highestScore = byteScores.OrderByDescending(x => x.Value).First();

                lineScores.Add(lineNr, new KeyValuePair <int, string>(highestScore.Value, byteOutput[highestScore.Key]));

                lineNr++;
            }

            var highestScoringLine = lineScores.OrderByDescending(x => x.Value.Key).First();

            if (String.Equals(highestScoringLine.Value.Value, ex4ExpectedAnswer))
            {
                System.Console.WriteLine("TEST: EX4: Detect single character XOR successful!");
            }
            else
            {
                System.Console.WriteLine("TEST: EX4: Detect single character XOR not successful, output was: {0}", highestScoringLine.Value.Value);
                return(false);
            }

            //#5 https://cryptopals.com/sets/1/challenges/5

            var ex5Output = XOR.RepeatingKeyXOR(Encoding.ASCII.GetBytes(ex5Input), Encoding.ASCII.GetBytes(ex5Key));
            var reversed  = XOR.RepeatingKeyXOR(Convert.FromBase64String(Converter.ConvertHexToBase64(ex5Output)), Encoding.ASCII.GetBytes(ex5Key), false);

            if (string.Equals(ex5Output, ex5ExpectedOutput) && string.Equals(reversed, ex5Input))
            {
                System.Console.WriteLine("TEST: EX5: Repeating character XOR successful! It even works both ways!");
            }
            else
            {
                System.Console.WriteLine("TEST: EX5: Repeating character XOR not successful, output: {0}", ex5Output);
                return(false);
            }

            var hammingDistance = Frequency.CalculateHammingDistance(Encoding.ASCII.GetBytes(hammingDistanceInput1), Encoding.ASCII.GetBytes(hammingDistanceInput2));

            if (hammingDistance == hammingDistanceCheck)
            {
                System.Console.WriteLine("TEST: EX6.1: Hamming distance check completed.");
            }
            else
            {
                System.Console.WriteLine("TEST: EX6.1: Hamming distance check failed. Output: {0}", hammingDistance);
                return(false);
            }

            //#6 https://cryptopals.com/sets/1/challenges/6

            var ex6InputBytes = Convert.FromBase64String(File.ReadAllText("TestFiles/6.txt"));

            //The most probable key size has the smallest hamming distance between the first two keysize sized blocks of bytes.
            var probableKeySize = XOR.FindProbableKeySize(ex6InputBytes);

            //Determine the key by transposing the blocks and executing single byte XOR operations on said blocks, scoring the outputs. The single bytes that output the highest scores will probably be part of the key.
            var probableKey = XOR.ReturnRepeatingXORKey(ex6InputBytes, probableKeySize);

            //After this, execute repeating key XOR.
            var ex6Output = XOR.RepeatingKeyXOR(ex6InputBytes, probableKey, false);

            if (string.Equals(System.Convert.ToBase64String(probableKey), System.Convert.ToBase64String(probableKey)))
            {
                System.Console.WriteLine("TEST: EX6: Breaking repeating key XOR solved!");
            }
            else
            {
                System.Console.WriteLine("Unable to solve EX6, something went wrong.");
                return(false);
            }

            return(true);
        }