Example #1
0
        static void Main(string[] args)
        {
            // most common words according to wikipedia: http://en.wikipedia.org/wiki/Most_common_words_in_English
            var commonWords = new[] { "the", "be", "to", "of", "and" };

            var filesToDelete = Directory.EnumerateFiles(@".", "*.txt").Where(fn => fn != ".\\cipher1.txt");

            foreach (var file in filesToDelete)
            {
                File.Delete(file);
            }

            var cipherText = File.ReadAllText(@"cipher1.txt").Split(',').Select(n => (byte)int.Parse(n)).ToArray();

            // a = 97, z = 122
            for (byte i = 97; i <= 122; i++)
            {
                for (byte j = 97; j <= 122; j++)
                {
                    for (byte k = 97; k <= 122; k++)
                    {
                        var secret      = new byte[] { i, j, k };
                        var decodedText = new Xor(secret).DoXor(cipherText, Encoding.ASCII);

                        bool isPotential = true;
                        foreach (var commonWord in commonWords)
                        {
                            if (!decodedText.Contains(commonWord))
                            {
                                isPotential = false;
                                break;
                            }
                        }

                        if (isPotential)
                        {
                            var bytes = Encoding.ASCII.GetBytes(decodedText);
                            Console.WriteLine(bytes.Select(b => (int)b).Sum());
                        }
                    }
                }
            }

            Console.WriteLine("Done");
            Console.ReadKey();
        }