示例#1
0
        public void Execute()
        {
            if (!checkParameters())
            {
                return;
            }

            ProgressChanged(0, 100);

            switch (m_Settings.Mode)
            {
            case 0:   // create prime with m_Input bits
                OutputString = this.RandomPrimeBits((int)n);
                break;

            case 1:   // create prime with m_Input bits, MSB set
                OutputString = this.RandomPrimeMSBSet((int)n);
                break;

            case 2:   // create prime <= m_Input
                OutputString = BigIntegerHelper.RandomPrimeLimit(n + 1);
                break;

            case 3:   // search biggest prime < m_Input
                OutputString = this.PreviousProbablePrime(n - 1);
                break;

            case 4:   // search smallest prime > m_Input
                OutputString = this.NextProbablePrime(n + 1);
                break;
            }

            ProgressChanged(100, 100);
        }
示例#2
0
        /// <summary>
        /// Called by the environment to start generating of public/private keys
        /// </summary>
        public void Execute()
        {
            BigInteger p;
            BigInteger q;
            BigInteger n;
            BigInteger e;
            BigInteger d;

            ProgressChanged(0.0, 1.0);

            switch (settings.Source)
            {
            // manual
            case 0:
                try
                {
                    p = BigIntegerHelper.ParseExpression(settings.P);
                    q = BigIntegerHelper.ParseExpression(settings.Q);
                    e = BigIntegerHelper.ParseExpression(settings.E);

                    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;
                }

                try
                {
                    D = BigIntegerHelper.ModInverse(e, (p - 1) * (q - 1));
                }
                catch (Exception)
                {
                    GuiLogMessage("RSAKeyGenerator Error: E (" + e + ") can not be inverted.", NotificationLevel.Error);
                    return;
                }

                try
                {
                    N = p * q;
                    E = e;
                }
                catch (Exception ex)
                {
                    GuiLogMessage("Big Number fail: " + ex.Message, NotificationLevel.Error);
                    return;
                }
                break;

            case 1:
                try
                {
                    n = BigIntegerHelper.ParseExpression(settings.N);
                    d = BigIntegerHelper.ParseExpression(settings.D);
                    e = BigIntegerHelper.ParseExpression(settings.E);
                }
                catch (Exception ex)
                {
                    GuiLogMessage("Invalid Big Number input: " + ex.Message, NotificationLevel.Error);
                    return;
                }

                try
                {
                    N = n;
                    E = e;
                    D = d;
                }
                catch (Exception ex)
                {
                    GuiLogMessage("Big Number fail: " + ex.Message, NotificationLevel.Error);
                    return;
                }
                break;

            //randomly generated
            case 2:
                try
                {
                    n = BigInteger.Parse(this.settings.Range);

                    switch (this.settings.RangeType)
                    {
                    case 0:         // n = number of bits for primes
                        if ((int)n <= 2)
                        {
                            GuiLogMessage("Value for n has to be greater than 2.", NotificationLevel.Error);
                            return;
                        }

                        if (n >= 1024)
                        {
                            GuiLogMessage("Please note that the generation of prime numbers with " + n + " bits may take some time...", NotificationLevel.Warning);
                        }

                        // calculate the number of expected tries for the indeterministic prime number generation using the density of primes in the given region
                        BigInteger limit = ((BigInteger)1) << (int)n;
                        limittries    = (int)(BigInteger.Log(limit) / 6);
                        expectedtries = 2 * limittries;

                        tries = 0;
                        p     = this.RandomPrimeBits((int)n);
                        tries = limittries;  limittries = expectedtries;
                        do
                        {
                            q = this.RandomPrimeBits((int)n);
                        } while (p == q);
                        break;

                    case 1:         // n = upper limit for primes
                    default:
                        if (n <= 4)
                        {
                            GuiLogMessage("Value for n has to be greater than 4", NotificationLevel.Error);
                            return;
                        }

                        p = BigIntegerHelper.RandomPrimeLimit(n + 1);
                        ProgressChanged(0.5, 1.0);
                        do
                        {
                            q = BigIntegerHelper.RandomPrimeLimit(n + 1);
                        } while (p == q);
                        break;
                    }
                }
                catch
                {
                    GuiLogMessage("Please enter an integer value for n.", NotificationLevel.Error);
                    return;
                }

                BigInteger phi = (p - 1) * (q - 1);

                // generate E for the given values of p and q
                bool found = false;
                foreach (var ee in new BigInteger[] { 3, 5, 7, 11, 17, 65537 })
                {
                    if (ee < phi && ee.GCD(phi) == 1)
                    {
                        E = ee;  found = true; break;
                    }
                }
                if (!found)
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        e = BigIntegerHelper.RandomIntLimit(phi);
                        if (e >= 2 && e.GCD(phi) == 1)
                        {
                            E = e; found = true; break;
                        }
                    }
                }
                if (!found)
                {
                    GuiLogMessage("Could not generate a valid E for p=" + p + " and q=" + q + ".", NotificationLevel.Error);
                    return;
                }

                N = p * q;
                D = BigIntegerHelper.ModInverse(E, phi);
                break;

            //using x509 certificate
            case 3:
                try
                {
                    X509Certificate2 cert;
                    RSAParameters    par;

                    if (this.settings.Password != "")
                    {
                        GuiLogMessage("Password entered. Try getting public and private key", NotificationLevel.Info);
                        cert = new X509Certificate2(settings.CertificateFile, settings.Password, X509KeyStorageFlags.Exportable);
                        if (cert == null || cert.PrivateKey == null)
                        {
                            throw new Exception("Private Key of X509Certificate could not be fetched");
                        }
                        RSACryptoServiceProvider provider = (RSACryptoServiceProvider)cert.PrivateKey;
                        par = provider.ExportParameters(true);
                    }
                    else
                    {
                        GuiLogMessage("No Password entered. Try loading public key only", NotificationLevel.Info);
                        cert = new X509Certificate2(settings.CertificateFile);
                        if (cert == null || cert.PublicKey == null || cert.PublicKey.Key == null)
                        {
                            throw new Exception("Private Key of X509Certificate could not be fetched");
                        }
                        RSACryptoServiceProvider provider = (RSACryptoServiceProvider)cert.PublicKey.Key;
                        par = provider.ExportParameters(false);
                    }

                    try
                    {
                        N = new BigInteger(par.Modulus);
                    }
                    catch (Exception ex)
                    {
                        GuiLogMessage("Could not get N from certificate: " + ex.Message, NotificationLevel.Warning);
                    }

                    try
                    {
                        E = new BigInteger(par.Exponent);
                    }
                    catch (Exception ex)
                    {
                        GuiLogMessage("Could not get E from certificate: " + ex.Message, NotificationLevel.Warning);
                    }

                    try
                    {
                        if (this.settings.Password != "")
                        {
                            D = new BigInteger(par.D);
                        }
                        else
                        {
                            D = 0;
                        }
                    }
                    catch (Exception ex)
                    {
                        GuiLogMessage("Could not get D from certificate: " + ex.Message, NotificationLevel.Warning);
                    }
                }
                catch (Exception ex)
                {
                    GuiLogMessage("Could not load the selected certificate: " + ex.Message, NotificationLevel.Error);
                }
                break;
            }

            ProgressChanged(1.0, 1.0);
        }