Ejemplo n.º 1
0
        // Generate a number of primes and write to file
        private static void GeneratePrimes(int n)
        {
            int i = 1, j = 3;

            StringBuilder sb = new StringBuilder();

            sb.Append(2);
            sb.Append(' ');

            while (i < n)
            {
                if (PrimalityTests.MillerRabin(j))
                {
                    if (i == n - 1)
                    {
                        sb.Append(j);
                        System.Console.WriteLine("help");
                        i++;
                    }
                    else
                    {
                        sb.Append(j);
                        sb.Append(' ');
                        i++;
                    }
                }
                j++;
            }

            System.IO.File.WriteAllText("./Generated/primes.txt", sb.ToString());
        }
Ejemplo n.º 2
0
        // GenerateStrongPrime(int n) - where n is number of bits in a generated prime number
        public static Integer GenerateStrongPrime(int bitLength)
        {
            Integer rand;
            int     i = 1;

            do
            {
                rand = 2 * i * GeneratePrime(bitLength) + 1;
                i++;
            } while(!PrimalityTests.MillerRabin(rand));

            return(rand);
        }
Ejemplo n.º 3
0
        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //



        // GeneratePrime(int n) - where n is number of bits in a generated prime number
        public static Integer GeneratePrime(int bitLength)
        {
            var generator = new LehmerHigh((uint)(int)DateTime.Now.Ticks);

            var rand = Tools.ToInteger(generator.RandomBits(bitLength));

            rand |= 1;

            var size = rand * 2 - 2;

            do
            {
                rand += 2;
            }while(!(PrimalityTests.MillerRabin(rand) && rand != size));

            return(rand);
        }
Ejemplo n.º 4
0
        public static Integer GenerateBlumPrime(int bitLength)
        {
            Integer rand;
            var     generator = new LehmerHigh((uint)(int)DateTime.Now.Ticks);

            do
            {
                do
                {
                    rand  = Tools.ToInteger(generator.RandomBits(bitLength));
                    rand |= 1;
                } while ((rand - 3) % 4 != 0);

                while (rand != 2 * rand - 4)
                {
                    rand += 4;
                    if (PrimalityTests.MillerRabin(rand))
                    {
                        return(rand);
                    }
                }
            }while (true);
        }