Beispiel #1
0
        public void Challenge23_Clone_an_MT19937_RNG_from_its_output()
        {
            var rng = new MT19937();

            rng.SeedMt(5489);

            // run random number of ExtractNumber
            // the cloning doesn't depend on internal index state
            using (var crnd = RandomNumberGenerator.Create())
            {
                var runCount = CryptoRandom.GetInt(crnd, 1, 2000);
                for (int i = 0; i < runCount; ++i)
                {
                    rng.ExtractNumber();
                }
            }

            var states = new List <uint>(624);

            for (int i = 0; i < states.Capacity; ++i)
            {
                states.Add(rng.ExtractNumber());
            }

            var clone = MT19937Cloner.Clone(states);

            for (int i = 0; i < 1000; ++i)
            {
                Assert.Equal(rng.ExtractNumber(), clone.ExtractNumber());
            }
        }
Beispiel #2
0
        public void Challenge22_Crack_an_MT19937_seed()
        {
            int  unixTimestamp;
            uint seed;
            uint currentRandomNumber;

            using (var crnd = RandomNumberGenerator.Create())
            {
                unixTimestamp  = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                unixTimestamp += CryptoRandom.GetInt(crnd, 40, 1000);

                var rng = new MT19937();
                seed = (uint)unixTimestamp;
                rng.SeedMt(seed);

                unixTimestamp      += CryptoRandom.GetInt(crnd, 40, 1000);
                currentRandomNumber = rng.ExtractNumber();
            }

            // check every possible second from the last twenty-four hours
            uint foundSeed = 0;

            for (int i = unixTimestamp; i > (unixTimestamp - 86400); --i)
            {
                var rng = new MT19937();
                rng.SeedMt((uint)i);

                if (rng.ExtractNumber() == currentRandomNumber)
                {
                    foundSeed = (uint)i;
                    break;
                }
            }

            Assert.Equal(seed, foundSeed);
        }