Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
public BigInteger GenerateNumber(int length)
        {
            RNGCryptoServiceProvider createNum = new RNGCryptoServiceProvider();
            byte[] number = new byte[length];
            createNum.GetBytes(number);
            BigInteger numToRet = new BigInteger(number);
            return numToRet;
        }
 
        public bool isPrime(BigInteger N, int iterations)
        {
            BigInteger r = new BigInteger((N - 1).ToByteArray());
            BigInteger a;
            BigInteger y;
            byte[] number = N.ToByteArray();
 
            if (N == 2 || N == 3)
                return true;
            if (N % 2 == 0)
                return false;
 
            while (r % 2 == 0)
            {
                r = r / 2;                
            }
 
            for (int i = 0; i < iterations; i++)
            {
                do
                {
                    a = GenerateNumber(number.Length);
                
                } while (a < 2 || a > N - 2);
                
                y = BigInteger.ModPow(a, r, N);
                BigInteger temp = r;
                
                while (temp != N - 1 && y != 1 && y != N - 1)
                {
                    y = BigInteger.ModPow(y, 2, N);
                    temp = BigInteger.Multiply(temp,2);
                }
                
                if (y != N - 1 && BigInteger.ModPow(temp,1,2) == 0)
                    return false;
                
            }
 
            return true;
        }
 
when I run this program with small numbers it works great, when I try to run it with numbers like 150 digits long (for RSA encryption), using the GenerateNumber(int length) method, the program fails and does not continue to operate.
do you know about a way to make it work ?
thanks a lot!
Posted 26-Feb-13 2:34am
Edited 26-Feb-13 2:38am
RyanDev129.8K
v2
Comments
boogac at 26-Feb-13 9:05am
   
150 digits and int length ??
 
I am not sure, it can be a memory problem..After seen your post,I m curious about Big O notation of this algorithm..if its something like n2 or worse it can cause trouble with big numbers naturally
Member 9552449 at 26-Feb-13 9:20am
   
how do you think I can fix that?
do you know a better version of this algorithm?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
According to Codeplex the BigInteger should handle large number, but by default it is set uint32. to handle large numbers you have to tweak their code. See instruction here
http://biginteger.codeplex.com/[^]
 

Regards
Jegan
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 190
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 Afzaal Ahmad Zeeshan 120
4 DamithSL 115
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100