刚学习,用整数编写了一个RSA加密算法,加深一下理解

来源:互联网 发布:淘宝联盟结算规则 编辑:程序博客网 时间:2024/05/16 04:40

#include<iostream>#include<cstdlib>#include<ctime>#define MAX 100using namespace std;

void Decimal_to_binary(int n,int str[],int * length)//十进制转化为二进制,二进制存放在str数组中,length存放转化后二进制的位数{ int i=0; while(n) {  str[i++]=n%2;  n/=2; } *length=i-1;}//Decimal_to_binary

bool WITNESS(int n)//素数的判定{ int str[MAX]; int length,a,x,d=1; Decimal_to_binary(n-1,str,&length); while(1) {  a=rand()%n+1;  if(a<n)break; } for(int i=length;i>=0;i--) {  x=d;  d=(d*d)%n;  if(d==1&&x!=1&&x!=n-1)   return false;  if(str[i]==1)   d=(d*a)%n; } if(d!=1)return false; return true;}//WITNESS

bool Test_prime(int n)//用WITNESS算法测试五次,看这个数是不是素数{ for(int i=0;i<5;i++)  if(WITNESS(n)==false)return false; return true;}//test_prime

bool Extended_Eucild(int f,int d,int *back)//扩展的欧几里得算法,在d在模f的条件下的逆元{ int X1,X2,X3,Y1,Y2,Y3,Q,T1,T2,T3; //if(f<d)return Extended_Eucild(d,f,back); X1=1;X2=0;X3=f;Y1=0;Y2=1;Y3=d; while(Y3!=1&&Y3!=0) {  Q=X3/Y3;  T1=X1-Q*Y1;T2=X2-Q*Y2;T3=X3-Q*Y3;  X1=Y1;X2=Y2;X3=Y3;  Y1=T1;Y2=T2;Y3=T3; } if(Y3==0) {  *back=X3;  return false; } else {  Y2=(Y2+f)%f;  *back=Y2;  return true; }}//Extended_Eucild

int Fast_index(int a,int index,int n)//快速指数算法,计算a的index次方在模n下的余数{ int str[MAX]; int length; Decimal_to_binary(index,str,&length); int c=0,d=1; for(int i=length;i>=0;i--) {  c=2*c;  d=(d*d)%n;  if(str[i]==1)  {   c=c+1;   d=(d*a)%n;  } } return d;}//Fast_index

void Produce_key(int *ee,int *d,int *nn)//产生密钥{ int p,q,n,fn,e,back; cout<<"input two primes p and q:"; cin>>p>>q;  while (!(Test_prime(p)&&Test_prime(q))) {  cout<<"wrong input,please make sure two number are both primes!"<<endl;  cout<<"input two primes p and q:";  cin>>p>>q; } n=p*q; fn=(p-1)*(q-1); cout<<"n="<<n<<",fn="<<fn<<endl; cout<<"please input e:"; cin>>e; while(e>=fn||e<=1) {  cout<<"wrong input,please make sure 1<e<"<<fn<<endl<<"Please re-enter e :";  cin>>e; } while(Extended_Eucild(fn,e,&back)==false) {  cout<<"wrong input,please make sure e and "<<fn<<" is coprime"<<endl;  cout<<"Please re-enter e:";  cin>>e; } cout<<"Public key is {"<<e<<","<<n<<"}"<<endl; cout<<"Private key is {"<<back<<","<<n<<"}"<<endl; *ee=e;*d=back;*nn=n;}//Produce_key/*void Produce_key(int *ee,int *d,int *nn)//产生密钥{ int p,q,n,fn,e,back; //srand((unsigned)time(NULL)); p=rand(); q=rand(); while (!(Test_prime(p)&&Test_prime(q))) { p=rand(); q=rand(); } cout<<"p="<<p<<"q="<<q<<endl; n=p*q; fn=(p-1)*(q-1); cout<<"n="<<n<<",fn="<<fn<<endl; cout<<"please input e:"; cin>>e; while(e>=fn||e<=1) {  cout<<"wrong input,please make sure 1<e<"<<fn<<endl<<"Please re-enter e "<<endl;  cin>>e; } while(Extended_Eucild(fn,e,&back)==false) {  cout<<"wrong input,please make sure e and "<<fn<<" is coprime"<<endl;  cout<<"Please re-enter e"<<endl;  cin>>e; } cout<<"Public key is {"<<e<<","<<n<<"}"<<endl; cout<<"Private key is {"<<back<<","<<n<<"}"<<endl; *ee=e;*d=back;*nn=n;}//Produce_key*/int Encrypt(int e,int n)//加密{ int c,m; cout<<"please input plaintext:"; cin>>m; c=Fast_index(m,e,n); cout<<"Ciphertext is "<<c<<endl; return c;}//Encrypt

void Decrypt(int d,int n)//解密{ int m,c; cout<<"please input ciphertext:"; cin>>c; m=Fast_index(c,d,n); cout<<"Plaintext is "<<m<<endl;}//Decrypt

void Print()//打印主界面{ cout<<"*********************************************"<<endl; cout<<"***       Welcome to use RSA encoder      ***"<<endl; cout<<"***               p Produce key           ***"<<endl; cout<<"***               e Encrypt               ***"<<endl; cout<<"***               d Decrypt               ***"<<endl; cout<<"***               q Quit                  ***"<<endl; cout<<"*********************************************"<<endl; cout<<"please input your choice:"<<endl;}//Print

int main(){ char ch; int e,d,n,c; Print(); while((ch=getchar())!='q') {  if(ch=='p')   Produce_key(&e,&d,&n);  else if(ch=='e')   c=Encrypt(e,n);  else if(ch=='d')   Decrypt(d,n); } system("pause"); return 0;}

原创粉丝点击