Click here to Skip to main content
15,913,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys and girls

Can anyone please help me. I am in the last day of my Forensic Computing BSC and for our cryptography we have to write a RSA program. I am not a programmer and have struggled all along with this subject (mostly not understanding the maths as a mature student).
I have written some code which seems to run, but gives the incorrect answer can anyone spot where I went wrong please?
the loads of couts are so I could follow the progress of the program as it kept crashing.

#include <iostream>
#include <ctime>
using namespace std;
int p,q,z,ans,answer,k,i,a,l,n,e,d,check,M,C,number2, mod, leftover;

int GCD(int e, int z) {   
    // No negatives   
    e = abs(e);   
    z = abs(z);     
   
    // Main loop of algorithm   
    int temp;   
    while (z > 0) {   
        temp = z;   
        z = e % z;   
        e = temp; 
    } // end while
	{ 	return e;  } 
} // end GCD()


unsigned long int gcd (unsigned long int a, unsigned long int b)
{
  unsigned long int r = a, oldR = 0;
  while (r != 0)
    {
      oldR = r;
      r = a % b;
      a = b;
      b = r;
    }
  return oldR;
}
      
unsigned long int inverse (unsigned long int number, unsigned long int mod)
{
  long int quotient = 0, x = 0, y = 1,
    oldx = 1, oldy = 0, newx = 0, newy = 0;
 
  if ( gcd(number, mod) != 1)
    {
      return 0;
    }
  /* Algorithm:    */
  while ( (x * number) + (y * mod) != 1 )
    {
      quotient = ((oldx * number) + (oldy * mod)) / ((x * number) + (y * mod));
      newx = oldx - (x * quotient);
      newy = oldy - (y * quotient);
      oldx = x;
      oldy = y;
      x = newx;
      y = newy;
    }
  if (x < 0)
    {
      x += mod;
    }
  return x;
}

// check whether d is a prime number 
 int checkprime(int number)
{
check = 0;
leftover = 2;
for (number2 = number; number2 >= 1; number2 = number2 - 2)
{
mod = number%number2;
if (mod == 0)
{
leftover = leftover -1;
}
}
if (leftover < 0)
{
check = 0;
}
else
{
check = 1;
}
return (check);
}

// generate prime number 
int genprime ()
	{
int number, check;
do
{
number = 2*(rand()%10)+11;
check = checkprime (number);
}
while(check == 0); 
return (number);
}
// check d is a prime number
int checkd(int d, int e, int z)
{
int modzd, checkprimed, check;
modzd = z%d;
checkprimed = checkprime(d);
if ((modzd == 0)&&(e == d)&&(checkprimed == 0))
{
check = 0;
}
else
{
check = 1;
}
return (check);
}

int gcd(int p, int q)
{int k;
	 //Function gcd();
if (q == 0) 
{return (p);}
else 
	
	{k = p / q;
	 gcd(p,p-k*q);
return (p,q);
    }
}
// generate prime number e
int eprime()
	{ /* Choose e such that gcd (e,z) == 1 and choose e < n  */
   {
    
cout<<" e is  "<<e<<endl;
   // do
	{ srand(time(0));
   e=  2*(rand()%((z-2)/2))+1;
	 cout<<" e is now 1 "<<e<<endl; 
   	}
 if  (GCD(e,z)!=1 )
	{ 
	   cout<<" e is now "<<e<<endl;
      e=eprime();
   }
else
 { return (e);
   }  }}

//check modulus
 int  modulus;
 int checkdmodulus(int d, int e, int z)
{
modulus = (d*e)%z;
if (modulus == 1)
{
return (check);
}
else
{
check = 0;
}
{return (check);
}}
 //get variable d
 int   ret,dx,fin;
int getd ()
	{(i=0, i=e, i++);
{ 
	d=(e*i)%z; 
  if (d=1)
{	return (d); }
}}

	void main()
	 // create random numbers 
	
	{
		 p= genprime();
		 cout<<"p/genprime = "<<p<<endl;
		srand((unsigned)time(0));
	 q= genprime ();
	cout<<"q = "<<q<<endl;
	if (p==q)
	{
		 		 q = genprime();
				 
	}
	else

	if (p<q)>
		{k = p;
		p = q;
		q = k;
		cout <<" p<q">	}
		
		{n = p*q;
	cout<<" n= "<<n<<endl;
	z = (p-1)*(q-1); 
cout<<" z= "<<z<<endl;
	e= eprime(); 
	d= getd(); 
		cout<< "p = "<< p <<endl;
cout<< "q = "<< q <<endl;
cout<< "d = "<<d <<endl;
cout<< "e = "<< e <<endl;
cout<< "z = "<< z <<endl;
cout<< "n = "<< n <<endl;
			}
				{C=0;
		cout<<"enter message two digits "<<endl;
		cin>>M;
		
			(i=0, i=e, i++);
			l=(M*i)%n;
				C=C+l;
				}
		cout<<"c = "<< C<<endl;
		{M=0;
		
		
			(i=0, i=d, i++);
			l=(C*i)%n;
				M=M+l;
				}
		cout<<"M = "<< M <<endl;
	}
	
</ctime></iostream>

many thanks in advance Martin
Posted
Updated 8-Jun-11 8:38am
v2

There's a lot conceptually wrong with your code:
1. Don't use globals when you don't need to.
2. Use meaningful variable names.
3. You don't seem to know how to use for loops:
for(int i=0; i<some_var; i++){...}
4. This is a comparison i==e, this is an assignment i=e, you should know the difference.

Just a few things that are wrong...
 
Share this answer
 
Comments
martinpaton 8-Jun-11 15:10pm    
Thanks Albert
1. Sorry not sure what you mean here............literally been at this for 8 weeks 1 hour a week.
2. yes obviously even at my stage I should know that, panic and numerous rewrites lost all that. Although most are in line with the assignment.
3. yes idiot I learnt that and deleted all the working loops
4. I dont see where I got that wrong, but I am sure I did!


Loops work now but the program only gets to working out e
Albert Holguin 8-Jun-11 15:24pm    
1.All these are declared as global variables:
int p,q,z,ans,answer,k,i,a,l,n,e,d,check,M,C,number2, mod, leftover;
...Meaning all functions have direct access to all these, typically not desirable and prone to bugs (specially when paired with variable names that are one letter).
2. Its hard to read and interpret your code with all these letters as variables names.
3. ...
4. Towards bottom of your main() function, this error is in two places within what I'm guessing should be two for() loops.
martinpaton 8-Jun-11 15:58pm    
Albert again thanks
1. i will come back to once i have it basically running
2. apolagies will fix if i have time
3.
4. sorted
can you please look at the section getd and see if I have been stupid there too?
Albert Holguin 8-Jun-11 18:23pm    
I just continued on another solution...
Sergey Alexandrovich Kryukov 8-Jun-11 17:28pm    
Agree, my 5.
--SA
Hi Martin,
I would rather look in the internet for the RSA implementation, as there is a lot of code to study here. You might check this:
http://code.google.com/p/rsa/[^]
http://www.di-mgt.com.au/rsa_alg.html[^]
http://sourceforge.net/projects/tplockbox/[^]
Regards
 
Share this answer
 
Comments
martinpaton 8-Jun-11 16:00pm    
Ciumac,
Thanks looked at them, I have been working on this for so long and checking out alsorts of sites, but have gotten myself in such a panic I am not learning from them.
Albert Holguin 8-Jun-11 19:26pm    
good links ciumac, i may use those sources! :D
In this function (reviewed as per OPs request)
int getd (){
  (i=0, i=e, i++); //Incomplete for() loop, be careful with i=e (you're assigning)
 {
  d=(e*i)%z; 
  if (d=1) //This statement is saying, assign 1 to d, not comparing
   {   return (d); }
 }

 //This wouldn't compile since the only return is within the conditional statement, you must return an integer
}
 
Share this answer
 

Well it looks like crap, you cant follow it, but it now works!
Many thanks for the help Albert

#include <iostream>
#include <ctime>
using namespace std;
int p,q,z,k,i,a,l,n,e,d,ld,check,M,C,mod,number2, leftover;

int encrypt()
{
l=(M * i)%n;
C=C+l;
return (C);
}
int decrypt()
{
M=0;
mod=0;

l=(C*i)%n;

M=M+l;
return M;
}

int GCD(int e, int z) {
// No negatives
e = abs(e);
z = abs(z);

// Main loop of algorithm
int temp;
while (z > 0) {
temp = z;
z = e % z;
e = temp;
} // end while
{
return e; }
} // end GCD()


//

// check number is a prime number
int checkprime(int number)
{
check = 0;
leftover = 2;
for (number2 = number; number2 >= 1; number2 = number2 - 2)
{
mod = number%number2;
if (mod == 0)
{
leftover = leftover -1;
}
}
if (leftover < 0)
{
check = 0;
}
else
{
check = 1;
}
return (check);
}


// generate prime number
int genprime ()
{
int number, check;
do
{
number = 2*(rand()%10)+11;
check = checkprime (number);
}
while(check == 0);
return (number);
}


int gcd(int p, int q)
{int k;
//Function gcd();
if (q == 0)
{return (p);}
else

{k = p / q;
gcd(p,p-k*q);
return (p,q);
}
}
// generate prime number e
int eprime()
{ /* Choose e such that gcd (e,z) == 1 and choose e < n */
{

{ srand(time(0));
e= 2*(rand()%((z-2)/2))+1;

}
if (GCD(e,z)!=1 )
{
e=eprime();
}
else
{ return (e);
}
}}



//check modulus
int modulus;
int checkdmodulus(int i)
{
modulus = (i*e)%z;
if (modulus == 1)
{
d=i;
return (d);
}
}


void main()
// create random numbers

{
p= genprime();
srand((unsigned)time(0));
q= genprime ();
if (p==q)
{
q = genprime();

}
else

if (p<q)>
{k = p;
p = q;
q = k;

}

{n = p*q;

z = (p-1)*(q-1);
e= eprime();

for (i=0; i<e;> d = checkdmodulus(i);


cout<< "p = "<< p <<endl;
cout<< "q = "<< q <<endl;
cout<< "d = "<<d <<endl;
cout<< "e = "<< e <<endl;
cout<< "z = "<< z <<endl;
cout<< "n = "<< n <<endl;

}

C=0;
cout<<"enter message two digits "<<endl;
cin>>M;
for (i=1; i<e;> {int messageencrypt = encrypt();}
cout<<"Encrypted Message = "<< C <<endl;
for (i=1; i<0; i++)
{M = decrypt();}
cout<<"Decrypted Message = "<< M <<endl;
}
 
Share this answer
 
Comments
Albert Holguin 9-Jun-11 17:02pm    
lol, be sure to accept one of my solutions ;) ... and glad it works, even if it doesn't look good...

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