Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I wrote the following C++ code for generating keys for RSA :

C#
void keygen(long long int &e_key,long long int &d_key,long long int phival)
    {
        //Code to find the encryption key.

        e_key = genrndnum(2,(phival));

        while(gcd(e_key,phival)!=1)
        {
            e_key = genrndnum(2,(phival-1));
        }

        //Code to find decryption key.

    long long int k =0;
        d_key = (1+(k*phival))/e_key;

        while(((1+(k*phival))%e_key)!=0)
        {
            ++k;
            d_key = (1+(k*phival))/e_key;
        }

        return;
    }


[Update]

Code for genrndnum().

C#
long genrndnum(long lower_limit,long upper_limit)
{
    srand(time(NULL));
    long rdnum =0;

    rdnum = lower_limit + (rand() % (int)(upper_limit - lower_limit + 1));

    return rdnum;
}


[End Update]

[Update 2]

Keygen is a function to generate private and public keys.
Here e_key is Public key and d_key the private key. Both are passed by reference.
Then phival is the Euler totient function (ie., phi(x)). (This was calculated sometime earlier , not shown).

I got all these information from wikipedia.


[End Update 2]

[Update 3]

Code for finding gcd().

long long int gcd(long long int num1 , long long int num2)
{
		/*
	
		Returns the Greatest Common Divisor (GCD) of two given numbers.
		
	*/
	
	
	if(num1 ==0 && num2==0)
	{
		return 0;
	}
	
	if(num1==0)
	{
		return num2;
	}
	
	if(num2 ==0)
	{
		return num1;
	}
	long long int remainder = 0;
	
	do
	{	
	    remainder = num1%num2;
		num1 = num2;	
		num2 = remainder;
				
	}while(remainder!=0);
	
	return num1;
}


Input Values for keygen().

The only input value for keygen() is :
phival = 43392.



[End Update 3]

[Update 4]

Output values : [Randomly generated]


VB
p =337
 q = 149
 phi = 49728
Encryption key = 8893
k = 7
Private key = 39 (There is another problem here : Encryption and Private keys should be the same. No where in the code have i changed e_key during decryption.)

Public key = 39
Modulus = 50213




[End Update 4]


But i get the same value for Public key and private key. What is the mistake in the code ?
Posted
Updated 18-Jun-13 15:38pm
v6
Comments
Richard MacCutchan 18-Jun-13 11:00am    
What is the mistake in the code?
You need to provide more information about what results you get against what you expect.
compuknow 18-Jun-13 11:20am    
For example , (results I get after running the code) Public key = 5615 , Private key = 5615(which is wrong) , Modulus = 16279. Private and Public keys must be different.
Richard MacCutchan 18-Jun-13 12:06pm    
What different values are you using in your functions? As it stands you have not provided nearly enough information.
compuknow 18-Jun-13 12:11pm    
Sorry , different values means ? Are you asking the p and q values (ie., the prime numbers).
Richard MacCutchan 18-Jun-13 12:25pm    
Well, you have shown us some (but not all) code, and shown a couple of results. We cannot deduce from that what the full code does or what its input values are, so it's impossible to guess what may be going wrong.

1 solution

Quote:
long genrndnum(long lower_limit,long upper_limit)
{
srand(time(NULL)); //<================== mistake here
long rdnum =0;

rdnum = lower_limit + (rand() % (int)(upper_limit - lower_limit + 1));

return rdnum;
}


You should not call srand every time you need a new random number. You should call it once, before calling rand for the very first time.
 
Share this answer
 
v2
Comments
compuknow 18-Jun-13 12:55pm    
Where should I put srand(time(NULL)) ? Is it outside the function ?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900