RSA算法程序中的分段错误



请检查以下代码。。这是我试图实现的RSA算法,p和q声明为17和11…程序在提示用户输入纯文本m后,将分段错误(核心转储)作为错误。这意味着什么,为什么会出现这个错误??。。。任何帮助都将不胜感激。:)

#include<iostream>     

#include<math.h>
using namespace std;
class RSA         
{    
public:    
long M,phi,d,e,n,c;        
int p,q;         
RSA();            
void calculate();            
long relprime();        
long gcd(long,long);          
void encrypt();         
void decrypt();         
};

RSA::RSA()        
{           
cout<<"enter the plain text M";         
cin>>M;         
p=17;      
q=11;    
}    
void RSA::calculate()        
{           
n=p*q;             
phi=(q-1)*(p-1);             
e=(long)relprime();              
cout<<e;  

d=0;           
while(d==0)            
{              
for(int k=1;;k++)                 
{                 
if((phi*k+1)%e==0)              
d=(phi*k+1)/e;             
}            
}       

cout<<d; 

}          

long RSA::relprime()              
{                  
for(int i=2;i<phi;i++)      

{               
if(gcd(i,phi)==1)           
return (long)i;              
}        
}             
long RSA::gcd(long a,long b)              
{              
if(a<b)        

{                   
if(a%b==0)                    
return a;              
else gcd(b-a,a);            
}             
else gcd(b,a);              
}              

void RSA::encrypt()              
{            
c=(long)pow(M,e);             
c=c%n;                
cout<<"encrypted c="<<c;  

}          
void RSA::decrypt()            
{             
M=(long)pow(c,d);          
M=M%n;            
cout<<"plain text="<<M;  

}           
int main()          
{            
RSA r;            
r.calculate();             
r.encrypt();        
r.decrypt();             
return 0;         
}         

由于似乎没有任何指针处理,下一个最好的想法可能是堆栈溢出。我注意到你的gcd函数是递归实现的,如果用相等的参数(a==b)调用它,它永远不会中止递归。也许线路if(a<b)应该是if(a>=b)

编辑:这似乎也不对。。。您可能需要将该方法作为一个整体进行检查:)

最新更新