Esempio n. 1
0
        public void Challenge19_Fixed_Nonce_CTR_Substitutions()
        {
            var(encryptedLines, plainTextLines) = ReadAndEncryptWithCTR("19.txt", 0);

            var expectedChars = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-'\".,:;!? ";

            var keystream = Xor.GetCommonKeyStream(encryptedLines, expectedChars);

            for (int j = 0; j < encryptedLines.Count; j++)
            {
                byte[] encryptedLine = encryptedLines[j];
                var    line          = new char[encryptedLine.Length];
                for (int i = 0; i < encryptedLine.Length; ++i)
                {
                    line[i] = Convert.ToChar((byte)(encryptedLine[i] ^ keystream[i]));
                }

                var decryptedLowerCase = new string(line).ToLowerInvariant();
                var originalLowerCase  = plainTextLines[j].ToLowerInvariant();

                switch (j)
                {
                case 4:
                    //            i have passed with a nod of the head
                    Assert.Equal("i have passed with a nod of the hiln", decryptedLowerCase);
                    break;

                case 27:
                    //            he might have won fame in the end,
                    Assert.Equal("he might have won fame in the end ", decryptedLowerCase);
                    break;

                case 37:
                    //            he, too, has been changed in his turn,
                    Assert.Equal("he, too, has been changed in his xxxis", decryptedLowerCase);
                    break;

                default:
                    Assert.Equal(originalLowerCase, decryptedLowerCase);
                    break;
                }
            }
        }