RSA算法代码帮助需求 [英] RSA algorithm code help neede

查看:80
本文介绍了RSA算法代码帮助需求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

男生和女生

谁能帮帮我吗.我正处于取证计算BSC的最后一天,对于我们的密码学,我们必须编写一个RSA程序.我不是程序员,并且一直在这门学科上苦苦挣扎(大多数情况下,作为一个成熟的学生,他们都不了解数学).
我写了一些似乎可以运行的代码,但给出的答案不正确,任何人都可以发现我出了错吗?
指令的负载很重,因此我可以跟踪程序的运行情况,因为它一直崩溃.

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>


提前非常感谢Martin


many thanks in advance Martin

推荐答案

您的代码在概念上有很多错误:
1.不需要时不要使用全局变量.
2.使用有意义的变量名.
3.您似乎不知道如何使用for循环:
for(int i=0; i<some_var; i++){...}
4.这是一个比较i==e,这是一个赋值i=e,您应该知道区别.

只是一些错误的地方...
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...


马丁,
我宁愿在Internet上查找RSA实现,因为这里有很多代码需要学习.您可以检查以下内容:
http://code.google.com/p/rsa/ [ http://www.di-mgt.com.au/rsa_alg.html [ ^ ]
http://sourceforge.net/projects/tplockbox/ [
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


在此功能中(根据OP请求进行了审核)
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
}


这篇关于RSA算法代码帮助需求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆