Beispiel #1
0
        /*
         * Get keys function, the function get as a parameter key length window. it done because we need to read the length from this form
         * */
        public void getKeys(KeyLengthWindow form)
        {
            // Generate two random numbers
            // Lets use our entropy based random generator
            Generator.Initialize(2);

            // For 1024 bit key length, we take 512 bits for first prime and 512 for second prime
            // Get same min and max
            BigInteger numMin = BigInteger.Pow(2, (form.BitLength / 2) - 1);
            BigInteger numMax = BigInteger.Pow(2, (form.BitLength / 2));

            // Create two prime numbers
            var p = new PrimeNumber();
            var q = new PrimeNumber();

            p.SetNumber(Generator.Random(numMin, numMin));
            q.SetNumber(Generator.Random(numMin, numMax));

            // Create threaded p and q searches
            pThread = new Thread(p.RabinMiller);
            qThread = new Thread(q.RabinMiller);

            // For timeout
            DateTime start = DateTime.Now;

            pThread.Start();
            qThread.Start();

            while (pThread.IsAlive || qThread.IsAlive)
            {
                TimeSpan ts = DateTime.Now - start;

                if (ts.TotalMilliseconds > (1000 * 60 * 5))
                {
                    try
                    {
                        pThread.Abort();
                    }
                    // ReSharper disable EmptyGeneralCatchClause
                    catch (Exception)
                    // ReSharper restore EmptyGeneralCatchClause
                    {
                    }

                    try
                    {
                        qThread.Abort();
                    }
                    // ReSharper disable EmptyGeneralCatchClause
                    catch (Exception)
                    // ReSharper restore EmptyGeneralCatchClause
                    {
                    }

                    MessageBox.Show("Key generating error: timeout.\r\n\r\nIs your bit length too large?", "Error");

                    break;
                }
            }


            // If we found numbers, we continue to create
            if (p.GetFoundPrime() && q.GetFoundPrime())
            {
                BigInteger n     = p.GetPrimeNumber() * q.GetPrimeNumber();
                BigInteger euler = (p.GetPrimeNumber() - 1) * (q.GetPrimeNumber() - 1);

                this.Dispatcher.Invoke((Action)(() =>
                {
                    messageStatusBar.Text = "Generating e prime number ...";
                }));

                var primeNumber = new PrimeNumber();

                while (true)
                {
                    primeNumber.SetNumber(Generator.Random(2, euler - 1));

                    start = DateTime.Now;

                    eThread = new Thread(primeNumber.RabinMiller);
                    eThread.Start();

                    while (eThread.IsAlive)
                    {
                        TimeSpan ts = DateTime.Now - start;

                        if (ts.TotalMilliseconds > (1000 * 60 * 5))
                        {
                            MessageBox.Show("Key generating error: timeout.\r\n\r\nIs your bit length too large?", "Error");

                            break;
                        }
                    }

                    if (primeNumber.GetFoundPrime() && (BigInteger.GreatestCommonDivisor(primeNumber.GetPrimeNumber(), euler) == 1))
                    {
                        break;
                    }
                }

                if (primeNumber.GetFoundPrime())
                {
                    this.Dispatcher.Invoke((Action)(() =>
                    {
                        messageStatusBar.Text = "Calculating key components ...";
                    }));

                    // Calculate secret exponent D as inverse number of E
                    BigInteger d = MathExtended.ModularLinearEquationSolver(primeNumber.GetPrimeNumber(), 1, euler);
                    //BigInteger d = MathExtended.ModularLinearEquationSolver(83, 1, 120);

                    // Displaying keys
                    if (d > 0)
                    {
                        // N
                        (keysVM as KeysVM).PublicN  = n.ToString();
                        (keysVM as KeysVM).PrivateN = n.ToString();

                        // E
                        (keysVM as KeysVM).PublicE = primeNumber.GetPrimeNumber().ToString();

                        // D
                        (keysVM as KeysVM).PrivateD = d.ToString();

                        this.Dispatcher.Invoke((Action)(() =>
                        {
                            tabControl.SelectedIndex = 2;

                            messageStatusBar.Text = "Successfully created key pair.";
                        }));
                    }
                    else
                    {
                        this.Dispatcher.Invoke((Action)(() =>
                        {
                            messageStatusBar.Text = "Error: Modular equation solver fault.";
                        }));

                        MessageBox.Show(
                            "Error using mathematical extensions.\r\ne = " + primeNumber + "\r\neuler = " + euler + "\r\np = " +
                            p.GetPrimeNumber() + "\r\n" + q.GetPrimeNumber(), "Error");
                    }
                }
            }
            else
            {
                this.Dispatcher.Invoke((Action)(() =>
                {
                    messageStatusBar.Text = "Idle";
                }));
            }
            this.Dispatcher.Invoke((Action)(() =>
            {
                this.progressBar.IsIndeterminate = false;
            }));
        }