genPseudoPrime() public static method

public static genPseudoPrime ( int bits, int confidence, Random rand ) : BigInteger
bits int
confidence int
rand Random
return BigInteger
        private void Generate_Q(Object sender, DoWorkEventArgs e)
        {
            DateTime dt    = DateTime.Now;
            int      iSeed = (dt.Year + dt.Second + dt.Minute + dt.Millisecond) / 3;

            byte[] tmp = new byte[m_bitLength + 1];
            tmp = new BigInteger(BigInteger.genPseudoPrime(m_bitLength, new Random(iSeed))).getBytesRaw();


            m_RSAParams.Q = tmp;
        }
Ejemplo n.º 2
0
        public void GeneratePair(int b, BigInteger e)
        {
            this.e = e;

            int qs = b >> 1;

            while (true)
            {
                while (true)
                {
                    this.P = BigInteger.genPseudoPrime(b - qs, 1, new Random());

                    if ((this.P - 1).gcd(this.e) == 1 && this.P.isProbablePrime(10))
                    {
                        break;
                    }
                }

                while (true)
                {
                    this.Q = BigInteger.genPseudoPrime(qs, 1, new Random());

                    if ((this.Q - 1).gcd(this.e) == 1 && this.P.isProbablePrime(10))
                    {
                        break;
                    }
                }

                if (this.P < this.Q)
                {
                    BigInteger t = this.P;
                    this.P = this.Q;
                    this.Q = t;
                }

                BigInteger phi = (this.P - 1) * (this.Q - 1);
                if (phi.gcd(this.e) == 1)
                {
                    this.N     = this.P * this.Q;
                    this.D     = this.e.modInverse(phi);
                    this.Dmp1  = this.D % (this.P - 1);
                    this.Dmq1  = this.D % (this.Q - 1);
                    this.Coeff = this.Q.modInverse(this.P);
                    break;
                }
            }

            this.CanEncrypt = this.N != 0 && this.e != 0;
            this.CanDecrypt = this.CanEncrypt && this.D != 0;

            Console.WriteLine(N.ToString(16));
            Console.WriteLine(D.ToString(16));
        }
Ejemplo n.º 3
0
        public void GeneratePair(int b, BigInteger e)
        {
            this.e = e;

            int qs = b >> 1;

            while (true)
            {
                while (true)
                {
                    P = BigInteger.genPseudoPrime(b - qs, 1, new Random());

                    if ((P - 1).gcd(this.e) == 1 && P.isProbablePrime(10))
                    {
                        break;
                    }
                }

                while (true)
                {
                    Q = BigInteger.genPseudoPrime(qs, 1, new Random());

                    if ((Q - 1).gcd(this.e) == 1 && P.isProbablePrime(10))
                    {
                        break;
                    }
                }

                if (P < Q)
                {
                    BigInteger t = P;
                    P = Q;
                    Q = t;
                }

                BigInteger phi = (P - 1) * (Q - 1);
                if (phi.gcd(this.e) == 1)
                {
                    N     = P * Q;
                    D     = this.e.modInverse(phi);
                    Dmp1  = D % (P - 1);
                    Dmq1  = D % (Q - 1);
                    Coeff = Q.modInverse(P);
                    break;
                }
            }

            CanEncrypt = N != 0 && this.e != 0;
            CanDecrypt = CanEncrypt && D != 0;

            Console.WriteLine(N.ToString(16));
            Console.WriteLine(D.ToString(16));
        }
Ejemplo n.º 4
0
        public void GeneratePair(int B, BigInteger _E)
        {
            E = _E;
            int Qs = B >> 1;

            while (true)
            {
                while (true)
                {
                    P = BigInteger.genPseudoPrime(B - Qs, 1, new Random());

                    if ((P - 1).gcd(E) == 1 && P.isProbablePrime(10))
                    {
                        break;
                    }
                }

                while (true)
                {
                    Q = BigInteger.genPseudoPrime(Qs, 1, new Random());

                    if ((Q - 1).gcd(E) == 1 && P.isProbablePrime(10))
                    {
                        break;
                    }
                }

                if (P < Q)
                {
                    BigInteger t = P;
                    P = Q;
                    Q = t;
                }

                BigInteger phi = (P - 1) * (Q - 1);

                if (phi.gcd(E) == 1)
                {
                    N     = P * Q;
                    D     = E.modInverse(phi);
                    Dmp1  = D % (P - 1);
                    Dmq1  = D % (Q - 1);
                    Coeff = Q.modInverse(P);
                    break;
                }
            }

            CanEncrypt = N != 0 && E != 0;
            CanDecrypt = CanEncrypt && D != 0;
            Console.WriteLine(N.ToString(16));
            Console.WriteLine(D.ToString(16));
        }
Ejemplo n.º 5
0
        private void CreateKeyPair(int p_key_strength)
        {
            Random x_random_generator = new Random();

            o_key_struct.P = BigInteger.genPseudoPrime(p_key_strength, 16, x_random_generator);

            o_key_struct.X = new BigInteger();
            o_key_struct.X.genRandomBits(p_key_strength - 1, x_random_generator);
            o_key_struct.G = new BigInteger();
            o_key_struct.G.genRandomBits(p_key_strength - 1, x_random_generator);

            o_key_struct.Y = o_key_struct.G.modPow(o_key_struct.X, o_key_struct.P);
        }
Ejemplo n.º 6
0
        private BigInteger GenerateG(BigInteger p, BigInteger q)
        {
            var        aux = p - 1;
            var        pow = aux / q;
            BigInteger gTemp;

            do
            {
                gTemp = BigInteger.genPseudoPrime(aux.bitCount(), confidence, rand);
            } while (gTemp >= aux && gTemp <= 1);

            return(gTemp.modPow(pow, p));
        }
Ejemplo n.º 7
0
        public RSA(int b, BigInteger e)
        {
            this.e = e;

            int qs = b >> 1;

            while (true)
            {
                while (true)
                {
                    this.p = BigInteger.genPseudoPrime(b - qs, 1, new Random());

                    if ((this.p - 1).gcd(this.e) == 1 && this.p.isProbablePrime(10))
                    {
                        break;
                    }
                }

                while (true)
                {
                    this.q = BigInteger.genPseudoPrime(qs, 1, new Random());

                    if ((this.q - 1).gcd(this.e) == 1 && this.p.isProbablePrime(10))
                    {
                        break;
                    }
                }

                if (this.p < this.q)
                {
                    BigInteger t = this.p;
                    this.p = this.q;
                    this.q = t;
                }

                BigInteger phi = (this.p - 1) * (this.q - 1);
                if (phi.gcd(this.e) == 1)
                {
                    this.n     = this.p * this.q;
                    this.d     = this.e.modInverse(phi);
                    this.dmp1  = this.d % (this.p - 1);
                    this.dmq1  = this.d % (this.q - 1);
                    this.coeff = this.q.modInverse(this.p);
                    break;
                }
            }

            this.canEncrypt = this.n != 0 && this.e != 0;
            this.canDecrypt = this.canEncrypt && this.d != 0;
        }
Ejemplo n.º 8
0
Archivo: RSA.cs Proyecto: habb0/RevEmu
        public RSA(int b, BigInteger e)
        {
            this.e = e;

            int qs = b >> 1;

            while (true)
            {
                while (true)
                {
                    p = BigInteger.genPseudoPrime(b - qs, 1, new Random());

                    if ((p - 1).gcd(this.e) == 1 && p.isProbablePrime(10))
                    {
                        break;
                    }
                }

                while (true)
                {
                    q = BigInteger.genPseudoPrime(qs, 1, new Random());

                    if ((q - 1).gcd(this.e) == 1 && p.isProbablePrime(10))
                    {
                        break;
                    }
                }

                if (p < q)
                {
                    BigInteger t = p;
                    p = q;
                    q = t;
                }

                BigInteger phi = (p - 1) * (q - 1);
                if (phi.gcd(this.e) == 1)
                {
                    n     = p * q;
                    d     = this.e.modInverse(phi);
                    dmp1  = d % (p - 1);
                    dmq1  = d % (q - 1);
                    coeff = q.modInverse(p);
                    break;
                }
            }

            canEncrypt = n != 0 && this.e != 0;
            canDecrypt = canEncrypt && d != 0;
        }
Ejemplo n.º 9
0
        private BigInteger GenerateBigPrime(int bitLength)
        {
            //var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            //byte[] bytes = new byte[bitLength / 8];
            //rng.GetBytes(bytes);

            //return new BigInteger(bytes);

            Random rand     = new Random();
            var    exBigInt = BigInteger.genPseudoPrime(bitLength, 5, rand);

            //return BigInteger.Parse(exBigInt.ToString());
            return(exBigInt);
        }
Ejemplo n.º 10
0
        private void genpaywords(int nr)
        {
            pwords = new BigInteger[nr];

            Random     rn = new Random();
            BigInteger cn = BigInteger.genPseudoPrime(64, 0, rn);

            BigInteger ci = cn;

            for (int i = nr - 1; i >= 0; i--)
            {
                pwords[i] = hashf.hash(ci.ToString());
                ci        = pwords[i];
            }
        }
Ejemplo n.º 11
0
        private BigInteger genx()
        {
            Random rn = new Random();

            BigInteger x;
            int        xb = rn.Next(1, 160);

            do
            {
                x = BigInteger.genPseudoPrime(xb, 100, rn);
            }while (x > q);
            //Console.WriteLine("\nX: " + x);

            return(x);
        }
Ejemplo n.º 12
0
        public void SetPassword(string password)
        {
            BigInteger bi_s = BigInteger.genPseudoPrime(256, 5, new Random());

            PasswordSalt = bi_s.getBytes().Reverse();

            string p = (Name + ":" + password).ToUpper();

            byte[]     pHash = sha1.ComputeHash(Encoding.UTF8.GetBytes(p));
            byte[]     x     = H(bi_s.getBytes().Reverse().Concat(pHash));
            var        bi_x  = new BigInteger(x.Reverse());
            BigInteger bi_v  = bi_g.modPow(bi_x, bi_N);

            PasswordVerifier = bi_v.getBytes().Reverse();
        }
Ejemplo n.º 13
0
        private EG_Secret_Key GenerateKey(int nBits)
        {
            BigInteger    q = new BigInteger();
            BigInteger    p;
            BigInteger    g;
            BigInteger    gPowTwo;
            BigInteger    gPowQ;
            EG_Secret_Key eskKey = new EG_Secret_Key();

            /*
             * // construct a prime p = 2q + 1
             * do {
             *      q = BigInteger.genRandom(nBits - 1);
             *      System.Windows.Forms.Application.DoEvents();
             *      p = (2*q) + 1;
             * } while ((!p.isProbablePrime()) || (!q.isProbablePrime()));
             */

            q = BigInteger.genPseudoPrime(nBits - 1);
            p = BigInteger.genPseudoPrime(nBits);

            // find a generator
            do
            {
                g       = new BigInteger();
                g       = BigInteger.genRandom(nBits - 1);
                gPowTwo = g.modPow(new BigInteger(2), p);
                gPowQ   = g.modPow(q, p);
            } while ((gPowTwo == 1) || (gPowQ == 1));

            BigInteger x;

            do
            {
                x = new BigInteger();
                x = BigInteger.genRandom(nBits);
            } while (x >= p - 1);

            BigInteger y = g.modPow(x, p);

            eskKey.p = p;
            eskKey.g = g;
            eskKey.x = x;
            eskKey.y = y;

            return(eskKey);
        }
Ejemplo n.º 14
0
        public RSA()
        {
            Random randomGenerator = new Random(DateTime.Now.Millisecond);

            this.p = BigInteger.genPseudoPrime(NUMBER_OF_BITS, CONFIDENCE_VALUE, randomGenerator);
            this.q = BigInteger.genPseudoPrime(NUMBER_OF_BITS, CONFIDENCE_VALUE, randomGenerator);

            this.n        = this.p * this.q;
            this.eulerOfN = (this.p - 1) * (this.q - 1);

            do
            {
                this.e = this.eulerOfN.genCoPrime(randomGenerator.Next(1, this.eulerOfN.bitCount()), randomGenerator);
            }while ((this.e > this.eulerOfN) && this.e.gcd(this.eulerOfN) > 1);

            this.d = e.modInverse(eulerOfN);
        }
Ejemplo n.º 15
0
        private BigInteger GenerateP(BigInteger q, int l)
        {
            if (l % 64 != 0)
            {
                throw new ArgumentException("L value is wrong");
            }
            BigInteger pTemp;
            BigInteger pTemp2;

            do
            {
                pTemp  = BigInteger.genPseudoPrime(l, confidence, rand);
                pTemp2 = pTemp - 1;
                pTemp -= (pTemp2 % q);
            } while (!pTemp.isProbablePrime(confidence) || pTemp.bitCount() != l);
            return(pTemp);
        }
Ejemplo n.º 16
0
        private void CreateKeyPair(int p_key_strength)
        {
            // create the random number generator
            Random x_random_generator = new Random();

            // create the large prime number, P
            o_key_struct.P = BigInteger.genPseudoPrime(p_key_strength,
                                                       16, x_random_generator);

            // create the two random numbers, which are smaller than P
            o_key_struct.X = new BigInteger();
            o_key_struct.X.genRandomBits(p_key_strength - 1, x_random_generator);
            o_key_struct.G = new BigInteger();
            o_key_struct.G.genRandomBits(p_key_strength - 1, x_random_generator);

            // compute Y
            o_key_struct.Y = o_key_struct.G.modPow(o_key_struct.X, o_key_struct.P);
        }
Ejemplo n.º 17
0
        private void CreateKeyPair(int key_strength)
        {
            // create the random number
            Random random_number = new Random();

            current_key.X = new BigInteger();
            current_key.G = new BigInteger();

            // create the large prime number, P  bits/confidence/random
            current_key.P = BigInteger.genPseudoPrime(key_strength, 16, random_number);

            // create the two random numbers, which are smaller than P
            current_key.X.genRandomBits(key_strength - 1, random_number);
            current_key.G.genRandomBits(key_strength - 1, random_number);

            // compute Y modPow(exp, modulo) Y = GexpX modP
            current_key.Y = current_key.G.modPow(current_key.X, current_key.P);
        }
Ejemplo n.º 18
0
        private void InitDH()
        {
            this.PublicKey = 0;
            while (this.PublicKey == 0)
            {
                this.Prime     = BigInteger.genPseudoPrime(BITLENGTH, 10, new Random());
                this.Generator = BigInteger.genPseudoPrime(BITLENGTH, 10, new Random());

                this.PrivateKey = new BigInteger(GenerateRandomHexString(BITLENGTH), 16);

                if (this.Generator > this.Prime)
                {
                    BigInteger temp = this.Prime;
                    this.Prime     = this.Generator;
                    this.Generator = temp;
                }

                this.PublicKey = this.Generator.modPow(this.PrivateKey, this.Prime);
            }
        }
Ejemplo n.º 19
0
        private void CreateKeyPair(int p_key_strength)
        {
            // create the random number generator
            var x_random_generator = new RNGCryptoServiceProvider(); // TODO: switch to cryptographic RNG

            // create the large prime number, P
            o_key_struct.P = BigInteger.genPseudoPrime(p_key_strength,
                                                       16, x_random_generator);

            // create the two random numbers, which are smaller than P
            o_key_struct.X = new BigInteger();
            o_key_struct.X.genRandomBits(p_key_strength - 1, x_random_generator);
            o_key_struct.G = new BigInteger();
            o_key_struct.G.genRandomBits(p_key_strength - 1, x_random_generator);

            // compute Y
            o_key_struct.Y = o_key_struct.G.modPow(o_key_struct.X, o_key_struct.P);

            o_key_struct.Padding = this.Padding;
        }
Ejemplo n.º 20
0
        private void gennr(bool cb)
        {
            Random rn = new Random();

            if (cb == true)
            {
                do
                {
                    q = BigInteger.genPseudoPrime(160, 100, rn);
                    r = BigInteger.genPseudoPrime(864, 100, rn);
                    p = new BigInteger(2 * q * r + 1);
                }while (!p.isProbablePrime(100));
            }
            else
            {
                q = new BigInteger("1357240200991138844597040462790625645217868549141", 10);
                r = new BigInteger("113598119916368178543726680204430381937875824948480914597565387121457881140775963786806669392598432983134451798231342845963966490631191394432315401337959287970864814764728934773343090591391155652090238298766028332871998156437678877229180157399463412668170350113", 10);
                p = new BigInteger("308359870215014078485257030866450886542952487603077718832462434453183859939031089613588944732186396626095766979593995910212976752989132511989881464667656451202440745056276306885148035449349526765128968559736391792793918308470057210553727052388063491635390548699144068380621588468735471047786563470306630805867", 10);
            }
        }
Ejemplo n.º 21
0
        private BigInteger[] genyi(int ns, BigInteger gp)
        {
            Random rn = new Random();

            BigInteger[] yrand = new BigInteger[ns];
            BigInteger   yi    = new BigInteger();
            int          nb;

            for (int i = 0; i < ns; i++)
            {
                do
                {
                    nb = rn.Next(1, 160);
                    yi = BigInteger.genPseudoPrime(nb, 100, rn);
                    //Console.WriteLine("\nY-{0}: {1}", i, yi);
                }while (yi > q);
                yrand[i] = yi;
            }
            return(yrand);
        }
Ejemplo n.º 22
0
        public rsa()
        {
            Random rn = new Random();

            p = BigInteger.genPseudoPrime(520, 100, rn);
            q = BigInteger.genPseudoPrime(520, 100, rn);
            n = new BigInteger(p * q);

            BigInteger o = new BigInteger((p - 1) * (q - 1));

            int eb = 0;

            do
            {
                //eb = rn.Next(2, o.bitCount());
                //e = BigInteger.genPseudoPrime(eb, 100, rn);
                e = new BigInteger(65537);
            }while (e > o || e.gcd(o) != new BigInteger(1));

            d = new BigInteger(e.modInverse(o));
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Generates a request packet.
        /// </summary>
        /// <returns></returns>
        public DiffieHellman GenerateRequest()
        {
            // Generate the parameters.
            prime = BigInteger.genPseudoPrime(bits, 30, random);
            mine  = BigInteger.genPseudoPrime(bits, 30, random);
            g     = BigInteger.genPseudoPrime(bits, 30, random);

            // Gemerate the string.
            PayloadWriter pw = new PayloadWriter();

            pw.WriteBigInteger(prime);
            pw.WriteBigInteger(g);

            // Generate the send BigInt.
            using (BigInteger send = g.modPow(mine, prime))
            {
                pw.WriteBigInteger(send);
            }
            representation = pw.ToByteArray();
            return(this);
        }
Ejemplo n.º 24
0
        // TODO: check again for Miu
        // p_key_strength in normal case is passed in by keysizevalue, which is 1024
        private void CreateKeyPair(int p_key_strength)
        {
            // create the large prime number, p and q
            // p and q are assumed to have the same bit length (512 bit each, so that N is 1024)
            // public static BigInteger genPseudoPrime(int bits, int confidence, RNGCryptoServiceProvider rand)
            using (RNGCryptoServiceProvider x_random_generator = new RNGCryptoServiceProvider())
            {
                var p = BigInteger.genPseudoPrime(p_key_strength / 2, 16, x_random_generator);
                var q = BigInteger.genPseudoPrime(p_key_strength / 2, 16, x_random_generator);

                // compute N
                // n = p*q
                o_key_struct.N = p * q;

                // compute G
                // g is random in Z*(n^2)
                // g = n+1 (simpler variant)
                //o_key_struct.G = o_key_struct.N + 1;
                var temp = new BigInteger();

                temp.genRandomBits(2048, x_random_generator);

                o_key_struct.G = temp % (o_key_struct.N * o_key_struct.N);  //to make sure g is in Z(Nsquare)
                                                                            //TODO: research if this is necessary, see below
                                                                            //o_key_struct.G = o_key_struct.G + 1; // to avoid getting G = 0

                // compute lambda
                // lambda = lcm(p-1, q-1) = (p-1)*(q-1)/gcd(p-1, q-1)
                // or simpler variant, lambda = (p-1)(q-1), since p and q have same length
                //o_key_struct.Lambda = (p - 1) * (q - 1);
                o_key_struct.Lambda = (p - 1) * (q - 1) / (p - 1).gcd(q - 1);

                // Miu =  lambda**-1 (mod n)
                //o_key_struct.Miu = o_key_struct.Lambda.modInverse(o_key_struct.N);
                o_key_struct.Miu = ((o_key_struct.G.modPow(o_key_struct.Lambda, o_key_struct.N * o_key_struct.N) - 1)
                                    / o_key_struct.N).modInverse(o_key_struct.N);

                o_key_struct.Padding = this.Padding;
            }
        }
Ejemplo n.º 25
0
 //TODO_: implement DH key generation methods
 private void GenerateKey(int bitlen, DHKeyGeneration keygen, out BigInteger p, out BigInteger g)
 {
     if (keygen == DHKeyGeneration.Static)
     {
         if (bitlen == 768)
         {
             p = new BigInteger(m_OAKLEY768);
         }
         else if (bitlen == 1024)
         {
             p = new BigInteger(m_OAKLEY1024);
         }
         else if (bitlen == 1536)
         {
             p = new BigInteger(m_OAKLEY1536);
         }
         else
         {
             throw new ArgumentException("Invalid bit size.");
         }
         g = new BigInteger(22); // all OAKLEY keys use 22 as generator
         //} else if (keygen == DHKeyGeneration.SophieGermain) {
         //	throw new NotSupportedException(); //TODO
         //} else if (keygen == DHKeyGeneration.DSA) {
         // 1. Let j = (p - 1)/q.
         // 2. Set h = any integer, where 1 < h < p - 1
         // 3. Set g = h^j mod p
         // 4. If g = 1 go to step 2
         //	BigInteger j = (p - 1) / q;
     }
     else
     {
         // random
         p = BigInteger.genPseudoPrime(bitlen);
         g = new BigInteger(3); // always use 3 as a generator
     }
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Generate a response packet.
        /// </summary>
        /// <param name="request">The string representation of the request.</param>
        /// <returns></returns>
        public DiffieHellman GenerateResponse(PayloadReader pr)
        {
            // Generate the would-be fields.
            using (BigInteger prime = pr.ReadBigInteger())
                using (BigInteger g = pr.ReadBigInteger())
                    using (BigInteger mine = BigInteger.genPseudoPrime(bits, 30, random))
                    {
                        // Generate the key.
                        using (BigInteger given = pr.ReadBigInteger())
                            using (BigInteger key = given.modPow(mine, prime))
                            {
                                this.key = key.getBytes();
                            }
                        // Generate the response.
                        using (BigInteger send = g.modPow(mine, prime))
                        {
                            PayloadWriter pw = new PayloadWriter();
                            pw.WriteBigInteger(send);
                            this.representation = pw.ToByteArray();
                        }
                    }

            return(this);
        }
Ejemplo n.º 27
0
        public static RSAKey Generate(int Exponent, int BitSize)
        {
            BigInteger _e, _n, _d, _p, _q, _dmp1, _dmq1, _iqmp;

            _e = Exponent;

            BigInteger phi, p1, q1;
            int        QS = BitSize >> 1;

            do
            {
                do
                {
                    _p = BigInteger.genPseudoPrime(BitSize - QS, 6, ByteGen);
                }while ((_p - 1).gcd(_e) != 1 && !_p.isProbablePrime(10));

                do
                {
                    _q = BigInteger.genPseudoPrime(QS, 6, ByteGen);
                }while ((_q - 1).gcd(_e) != 1 && !_q.isProbablePrime(10));

                if (_p <= _q)
                {
                    BigInteger tmp_p = _p;
                    _p = _q; _q = tmp_p;
                }
                phi = (p1 = (_p - 1)) * (q1 = (_q - 1));
            }while (phi.gcd(_e) != 1);

            _n    = _p * _q;
            _d    = _e.modInverse(phi);
            _dmp1 = _d % p1;
            _dmq1 = _d % q1;
            _iqmp = _q.modInverse(_p);
            return(new RSAKey(_e, _n, _d, _p, _q, _dmp1, _dmq1, _iqmp));
        }
Ejemplo n.º 28
0
        private static BigInteger[] findRandomStrongPrime(uint primeBits, int orderBits, Rng random)
        {
            BigInteger one = new BigInteger(1);
            BigInteger u, aux, aux2;

            long[]     table_q, table_u, prime_table;
            PrimeSieve sieve = new PrimeSieve(16000);
            uint       table_count = sieve.AvailablePrimes() - 1;
            int        i, j;
            bool       flag;
            BigInteger prime = null, order = null;

            order = BigInteger.genPseudoPrime(orderBits, 20, random);

            prime_table = new long[table_count];
            table_q     = new long[table_count];
            table_u     = new long[table_count];

            i = 0;
            for (int pN = 2; pN != 0; pN = sieve.getNextPrime(pN), i++)
            {
                prime_table[i] = (long)pN;
            }

            for (i = 0; i < table_count; i++)
            {
                table_q[i] =
                    (((order % new BigInteger(prime_table[i])).LongValue()) *
                     (long)2) % prime_table[i];
            }

            while (true)
            {
                u = new BigInteger();
                u.genRandomBits((int)primeBits, random);
                u.setBit(primeBits - 1);
                aux  = order << 1;
                aux2 = u % aux;
                u    = u - aux2;
                u    = u + one;

                if (u.bitCount() <= (primeBits - 1))
                {
                    continue;
                }

                for (j = 0; j < table_count; j++)
                {
                    table_u[j] =
                        (u % new BigInteger(prime_table[j])).LongValue();
                }

                aux2 = order << 1;

                for (i = 0; i < (1 << 24); i++)
                {
                    long cur_p;
                    long value;

                    flag = true;
                    for (j = 1; j < table_count; j++)
                    {
                        cur_p = prime_table[j];
                        value = table_u[j];
                        if (value >= cur_p)
                        {
                            value -= cur_p;
                        }
                        if (value == 0)
                        {
                            flag = false;
                        }
                        table_u[j] = value + table_q[j];
                    }
                    if (!flag)
                    {
                        continue;
                    }

                    aux   = aux2 * new BigInteger(i);
                    prime = u + aux;

                    if (prime.bitCount() > primeBits)
                    {
                        continue;
                    }

                    if (prime.isProbablePrime(20))
                    {
                        break;
                    }
                }

                if (i < (1 << 24))
                {
                    break;
                }
            }

            return(new BigInteger[] { prime, order });
        }
Ejemplo n.º 29
0
        public static void GenKey(int dwKeySize, string exponent)
        {
            string info = "key sizes from 16 bits to 16384 bits in increments of 8 bits.";

            exponent = ConvertTool.RemoveSpace(exponent);

            int count = 0;

            if (dwKeySize % 8 != 0 || dwKeySize < 16)
            {
                throw new ArgumentException(info);
            }

            BigInteger e = 0, n = 0, d = 0, p = 0, q = 0, dp = 0, dq = 0, invq = 0;

label1:
            count++;
            if (count > 20)
            {
                throw new ArgumentException("change E or bits.");
            }
            e = new BigInteger(exponent, 16);

            if (dwKeySize < 384)
            {
                Random rand = new Random();
                p = BigInteger.genPseudoPrime(dwKeySize / 2, 3, rand);
                q = BigInteger.genPseudoPrime(dwKeySize / 2, 3, rand);
                n = p * q;
            }
            else
            {
                //只支持长度从 384 位至 16384 位(增量为 8 位)的密钥
                var           rsa  = new RSACryptoServiceProvider(dwKeySize);
                RSAParameters temp = rsa.ExportParameters(true);
                n = new BigInteger(temp.Modulus);
                p = new BigInteger(temp.P);
                q = new BigInteger(temp.Q);
            }


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

            try
            {
                d  = e.modInverse(oula);
                dp = d % (p - 1);
                dq = d % (q - 1);

                invq = q.modInverse(p);
            }
            catch (Exception ex)
            {
                if (ex.Message != info)
                {
                    goto label1;
                }
            }

            RSA_D    = d.ToHexString();
            RSA_N    = n.ToHexString();
            RSA_E    = e.ToHexString();
            RSA_P    = p.ToHexString();
            RSA_Q    = q.ToHexString();
            RSA_DP   = dp.ToHexString();
            RSA_DQ   = dq.ToHexString();
            RSA_INVQ = invq.ToHexString();
        }
Ejemplo n.º 30
0
        public static RSAKeyPair GenerateNew(int bits, Random rnd)
        {
            BigInteger one = new BigInteger(1);
            BigInteger p   = null;
            BigInteger q   = null;
            BigInteger t   = null;
            BigInteger p_1 = null;
            BigInteger q_1 = null;
            BigInteger phi = null;
            BigInteger G   = null;
            BigInteger F   = null;
            BigInteger e   = null;
            BigInteger d   = null;
            BigInteger u   = null;
            BigInteger n   = null;

            bool finished = false;

            while (!finished)
            {
                p = BigInteger.genPseudoPrime(bits / 2, 64, rnd);
                q = BigInteger.genPseudoPrime(bits - (bits / 2), 64, rnd);

                if (p == 0)
                {
                    continue;
                }
                else if (q < p)
                {
                    t = q;
                    q = p;
                    p = t;
                }

                t = p.gcd(q);
                if (t != one)
                {
                    continue;
                }

                p_1 = p - one;
                q_1 = q - one;
                phi = p_1 * q_1;
                G   = p_1.gcd(q_1);
                F   = phi / G;

                e = one << 5;
                e = e - one;
                do
                {
                    e = e + (one + one);
                    t = e.gcd(phi);
                } while(t != one);

                // !!! d = e.modInverse(F);
                d = e.modInverse(phi);
                n = p * q;
                u = p.modInverse(q);

                finished = true;
            }

            return(new RSAKeyPair(e, d, n, u, p, q));
        }