Пример #1
0
        public void Challenge06_BreakRepeatingXOR()
        {
            //Console.WriteLine(Utils.GetHammingDistance(Encoding.UTF8.GetBytes("this is a test"),
            //                                           Encoding.UTF8.GetBytes("wokka wokka!!!")));

            var base64 = File.ReadAllText("6.txt").Replace("\n", "");
            var cipher = Convert.FromBase64String(base64);

            var len = Xor.GuessRepeatingKeyLength(cipher, 40);
            var key = Xor.BreakRepeating(cipher, len);

            Assert.Equal("Terminator X: Bring the noise", Encoding.UTF8.GetString(key));

            //Console.WriteLine($"Password:\"{Encoding.UTF8.GetString(key)}\"");
            //Console.WriteLine(Set1.XORDecrypt(cipher, key));
        }
Пример #2
0
        public void Challenge20_Fixed_Nonce_CTR_Statistically()
        {
            var(encryptedLines, plainTextLines) = ReadAndEncryptWithCTR("20.txt", 0);

            var minLength     = encryptedLines.Min(x => x.Length);
            var maxLength     = encryptedLines.Max(x => x.Length);
            var expectedChars = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM-'\".,:;!? /";

            var keystream = new List <byte>(maxLength);

            for (int i = minLength; i <= maxLength; ++i)
            {
                var cipher = encryptedLines.Where(x => x.Length >= i)
                             .Select(x => x.Take(i))
                             .SelectMany(x => x)
                             .ToArray();

                var key = Xor.BreakRepeating(cipher, i, expectedChars);
                if (i == minLength)
                {
                    keystream.AddRange(key);
                }
                else
                {
                    keystream.Add(key[i - 1]);
                }
            }

            for (int j = 0; j < encryptedLines.Count; j++)
            {
                byte[] encryptedLine = encryptedLines[j];
                var    line          = new char[encryptedLine.Length];
                for (int i = 0; i < line.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 21:
                    //            shake 'till your clear, make it disappear, make the next / after the ceremony, let the rhyme rest in peace
                    Assert.Equal("shake 'till your clear, make it disappear, make the next / after the ceremony, let the rhyme rest iy peace", decryptedLowerCase);
                    break;

                case 26:
                    Assert.Equal(new string(originalLowerCase.Replace("observe", "observr").SkipLast(12).ToArray()), new string(decryptedLowerCase.SkipLast(12).ToArray()));
                    break;

                case 29:
                    //            program into the speed of the rhyme, prepare to start / rhythm's out of the radius, insane as the craziest
                    Assert.Equal("program into the speed of the rhyme, prepare to start / rhythm's out of the radius, insane as the ceaziest", decryptedLowerCase);
                    break;

                case 41:
                    //            i wanna hear some of them def rhymes, you know what i'm sayin'? / and together, we can get paid in full
                    Assert.Equal("i wanna hear some of them def rhymes, you know what i'm sayin'? / and together, we can get paid in qull", decryptedLowerCase);
                    break;

                case 46:
                    Assert.Equal(new string(originalLowerCase.Replace("move", "mxve").SkipLast(10).ToArray()), new string(decryptedLowerCase.SkipLast(10).ToArray()));
                    break;

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