示例#1
0
        public static long[] generatePrimes(byte count, int maxValue = ushort.MaxValue)
        {
            var  rnds  = new System.Collections.Generic.List <long>();
            var  p     = new long[count];
            byte found = 0;

            if (maxValue / count < System.Math.Log(maxValue))
            {
                throw new System.ArgumentOutOfRangeException();
            }

            while (found < count)
            {
                rnds.AddRange(Utils.PseudoRandoms(100, maxValue));
                foreach (var num in rnds)
                {
                    if (found < count && PrimeTests.MillerRabinTests(num))
                    {
                        p[found] = num;
                        found++;
                    }
                }
                rnds.Clear();
            }
            return(p);
        }
示例#2
0
        public static RsaParams generateRSAPair()
        {
            var rnds = new System.Collections.Generic.List <long>();

            var  ps = PrimeTests.generatePrimes(2, short.MaxValue);
            long p = ps[0], q = ps[1];

            long n    = p * q;
            long phiN = (p - 1) * (q - 1);

            long e = 2;

            while (BigInteger.GreatestCommonDivisor(e, phiN) != 1)
            {
                e = Utils.PseudoRandoms(1, phiN, 2)[0];
            }

            long k = 1;

            while (((k * phiN + 1) % e) != 0) // || ((k * phiN + 1) / e) == e)
            {
                k++;
            }

            long d = (k * phiN + 1) / e;

            return(new RsaParams()
            {
                n = n, d = d, e = e
            });
        }