Exemple #1
0
        /// <summary>
        /// Called by the environment to start generating of public/private keys
        /// </summary>
        public void Execute()
        {
            BigInteger p = 1, q = 1;

            ProgressChanged(0, 1);

            switch (settings.Source)
            {
            // manually enter primes
            case 0:
                try
                {
                    p = BigIntegerHelper.ParseExpression(settings.P);
                    q = BigIntegerHelper.ParseExpression(settings.Q);

                    if (!BigIntegerHelper.IsProbablePrime(p))
                    {
                        GuiLogMessage(p.ToString() + " is not prime!", NotificationLevel.Error);
                        return;
                    }
                    if (!BigIntegerHelper.IsProbablePrime(q))
                    {
                        GuiLogMessage(q.ToString() + " is not prime!", NotificationLevel.Error);
                        return;
                    }
                    if (p == q)
                    {
                        GuiLogMessage("The primes P and Q can not be equal!", NotificationLevel.Error);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    GuiLogMessage("Invalid Big Number input: " + ex.Message, NotificationLevel.Error);
                    return;
                }


                break;

            //randomly generated primes
            case 1:
                try
                {
                    int keyBitLength = Convert.ToInt32(settings.KeyBitLength);
                    if (keyBitLength < 4)
                    {
                        GuiLogMessage("The keylength must be greater than 3.", NotificationLevel.Error);
                        return;
                    }

                    int i, maxtries = 10;
                    for (i = 0; i < maxtries; i++)
                    {
                        p = BigIntegerHelper.RandomPrimeMSBSet(keyBitLength - (keyBitLength / 2));
                        q = BigIntegerHelper.RandomPrimeMSBSet(keyBitLength / 2);
                        //GuiLogMessage("p = " + p.ToString(), NotificationLevel.Info);
                        //GuiLogMessage("q = " + q.ToString(), NotificationLevel.Info);
                        if (p != q)
                        {
                            break;
                        }
                    }
                    if (i == maxtries)
                    {
                        throw new Exception("Could not create two differing primes");
                    }
                }
                catch (Exception ex)
                {
                    GuiLogMessage(ex.Message, NotificationLevel.Error);
                    return;
                }
                break;

            default:
                throw new Exception("Illegal Key Generation Mode");
            }

            N      = p * q;
            G      = N + 1;
            Lambda = (p - 1) * (q - 1);

            ProgressChanged(1, 1);
        }