示例#1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter string to match:");
            string toMatch = Console.ReadLine();
            Console.WriteLine("Start to compute keys, trying to find match for \"{0}\".", toMatch);

            char[] keyspace = new char[] {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                'u', 'v', 'w', 'x', 'y', 'z','A','B','C','D','E',
                'F','G','H','I','J','K','L','M','N','O','P','Q','R',
                'S','T','U','V','W','X','Y','Z','1','2','3','4','5',
                '6','7','8','9','0','!','$','#','@','-'
            };
            Executor executor = new Executor(keyspace);

            var iterations = Program.GetEstimatedIterations(keyspace, toMatch.ToCharArray());
            Console.WriteLine("Need to compute {0} keys to match your input.", iterations);
            // just a rough estimation
            Console.WriteLine("Approximate time needed: {0} ms", iterations / 5000);

            char[] lookupKey = toMatch.ToCharArray();
            bool isMatched = false;
            ulong generatedKeyCount = 0;

            Stopwatch stopWatch = Stopwatch.StartNew();

            while(!isMatched) {
                char[] currentKey = executor.ComputeNextKey();
                generatedKeyCount++;
                if (generatedKeyCount % 500000 == 0) {
                    Console.Write('.');
                }
                if (currentKey.Length != lookupKey.Length) {
                    continue;
                }
                for(var i = currentKey.Length - 1; i >= 0; i--) {
                    if (!(currentKey[i] == lookupKey[i])) {
                        break;
                    }
                    if (i == 0) {
                        isMatched = true;
                    }
                }
            }

            stopWatch.Stop();

            Console.WriteLine("Found match.");
            Console.WriteLine("Generated {0} keys, took {1}ms.", generatedKeyCount, stopWatch.ElapsedMilliseconds.ToString());
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
示例#2
0
        public void TestComputeNextKeyDeterminism()
        {
            var keyspace = new char[] { 'a', 'b', 'c' };
            var executor = new Executor(keyspace);

            for (var keyLength = 1; keyLength < 10; keyLength++)
            {
                var    numberOfPossibleKeys = (int)Math.Pow(keyspace.Length, keyLength);
                char[] computedKey          = new char[0];
                for (var i = 0; i < numberOfPossibleKeys; i++)
                {
                    // compute all keys for the current key length
                    computedKey = executor.ComputeNextKey();
                }
                // the last computed key for this key length should contain only the last character of the keyspace
                var lastComputedKeyShouldBe = createFilledArray <char>('c', keyLength);
                Assert.AreEqual(lastComputedKeyShouldBe, computedKey);
            }
        }
示例#3
0
        public void TestComputeNextKey()
        {
            var executor = new Executor(new char[] { 'a', 'b', 'c' });

            Assert.AreEqual(new char[] { 'a' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'b' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'c' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'a', 'a' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'a', 'b' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'a', 'c' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'b', 'a' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'b', 'b' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'b', 'c' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'c', 'a' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'c', 'b' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'c', 'c' }, executor.ComputeNextKey());
            Assert.AreEqual(new char[] { 'a', 'a', 'a' }, executor.ComputeNextKey());
            for (var i = 0; i < 350; i++)
            {
                executor.ComputeNextKey();
            }
            Assert.AreEqual(new char[] { 'a', 'a', 'a', 'a', 'a', 'a' }, executor.ComputeNextKey());
        }
示例#4
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter string to match:");
            string toMatch = Console.ReadLine();

            Console.WriteLine("Start to compute keys, trying to find match for \"{0}\".", toMatch);

            char[] keyspace = new char[] {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
                'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5',
                '6', '7', '8', '9', '0', '!', '$', '#', '@', '-'
            };
            Executor executor = new Executor(keyspace);

            var iterations = Program.GetEstimatedIterations(keyspace, toMatch.ToCharArray());

            Console.WriteLine("Need to compute {0} keys to match your input.", iterations);
            // just a rough estimation
            Console.WriteLine("Approximate time needed: {0} ms", iterations / 5000);

            char[] lookupKey         = toMatch.ToCharArray();
            bool   isMatched         = false;
            ulong  generatedKeyCount = 0;

            Stopwatch stopWatch = Stopwatch.StartNew();

            while (!isMatched)
            {
                char[] currentKey = executor.ComputeNextKey();
                generatedKeyCount++;
                if (generatedKeyCount % 500000 == 0)
                {
                    Console.Write('.');
                }
                if (currentKey.Length != lookupKey.Length)
                {
                    continue;
                }
                for (var i = currentKey.Length - 1; i >= 0; i--)
                {
                    if (!(currentKey[i] == lookupKey[i]))
                    {
                        break;
                    }
                    if (i == 0)
                    {
                        isMatched = true;
                    }
                }
            }

            stopWatch.Stop();

            Console.WriteLine("Found match.");
            Console.WriteLine("Generated {0} keys, took {1}ms.", generatedKeyCount, stopWatch.ElapsedMilliseconds.ToString());
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }