⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rsa.cpp

📁 算法不是很难
💻 CPP
字号:
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

class  RSA
{ 

public:
	 
	 void  getTP();
     void  displayPUK(); 
	 void  displayPRK();
	 unsigned int  gcd(unsigned int a, unsigned int b);
	  long  extend( long , long );
     unsigned int  encrypted(unsigned int m,unsigned  int e);
	 unsigned int  decrypted(unsigned int c,unsigned  int d);

     unsigned int p,q,e,d;

};

void RSA::displayPUK()
{
	 cout<<"Public Key "<<endl;
	 cout<<"the value of e and n  are : "<<endl;
	 while(1)
	 {   
		 
		 e=rand();    /* 确保e 和 (p-1)*(q-1)互素 */
		 if( e<(p-1)*(q-1) &&  1==gcd(e,(p-1)*(q-1)) )  break;
	 }
	 cout<<e<<"  "<<p*q<<endl;
}

void  RSA::displayPRK()
{
	  cout<<"Private Key "<<endl;
	  cout<<"the value of d and  n  are : "<<endl;
	  d=extend( (p-1)*(q-1),e);
	  cout<<d<<"  "<<p*q<<endl;
}

void RSA::getTP()
{
cout<<"Input tow Primes"<<endl;
	   cin>>p>>q;
}

unsigned int  RSA::gcd(unsigned int a,unsigned int b)
{       	unsigned int temp;
	     if(a<b)
		{
		
			temp=a;
			a=b;
			b=temp;
		}   
		if(b==0)
			return a;
		else return gcd(b,a%b);

}

  long  RSA::extend(long a,long e)
{
	long x[4],y[4],t[4],q,i;

	x[1]=1;
	x[2]=0;	
	
	x[3]=a;

    y[1]=0;
	y[2]=1;	
	
	y[3]=e;	
	
	while(y[3]!=1){

		if(y[3]==0){
			cout<<"密钥不存在!"<<endl;
			break;
		}
		
		q=x[3]/y[3];

		for(i=1;i<4;i++){
			t[i]=x[i]-q*y[i];
			x[i]=y[i];
			y[i]=t[i];
		}
	}
	if(y[3]==1){
		
             return y[2];
	}
               
	}

unsigned int RSA::encrypted(unsigned int m,unsigned int e)
{
	unsigned int t=1,temp=m;
	while(e!=0)
	{
	   if(e%2!=0)
	   {
           t*=temp;
		   t%=p*q;
	   }
	   e=e>>1;
	   temp=(temp%(p*q))*(temp%(p*q))%(p*q);

	}
	return t%(p*q);

	
}

unsigned int RSA::decrypted(unsigned int c,unsigned int d)
{
     unsigned int t=1,temp=c;
	while(d!=0)
	{
	   if(d%2!=0)
	   {
           t*=temp;
		   t%=p*q;
	   } 
	   d=d>>1;
	   temp=(temp%(p*q))*(temp%(p*q))%(p*q);

	}
	return t%(p*q);

}

/*一定要注意明文的值要比n小  */

int main()
{       
	    unsigned int now,rnow;
	    //char cc[100]="plain.txt",ch;
          char cc[100],ch;
	    RSA rsa;
        
	   cout<<"**********************************"<<endl;
	    rsa.getTP();

	   cout<<"**********************************"<<endl;
	    rsa.displayPUK();

	   cout<<"**********************************"<<endl;
	   rsa.displayPRK();

	   cout<<"**********************************"<<endl;
       cout<<"Input PlainTextFile "<<endl;
	      getchar();
	 	 cin.getline(cc,100,'\n');      
	  
	  
	   cout<<"***********************************"<<endl;

	   cout<<"encrypted ........."<<endl;
       ifstream infile1(cc);
	   ofstream outfile1("encrypted.txt");
       ofstream outfile2("decrypted.txt");
	   while(infile1.get(ch))
	   {      
		     cout<<(unsigned int)ch<<"  ";
               now=rsa.encrypted((unsigned int )ch,rsa.e);        cout<<" now "<<now<<" ";
               rnow=rsa.decrypted(now,rsa.d);   cout<<" rnow "<<rnow<<endl;
               outfile1<<(char)now;   //可是这样话c的值有可能超过了256
			   outfile2<<(char)rnow;
	   }
       infile1.close();
	   outfile1.close();
	   outfile2.close();

	  cout<<"***********************************"<<endl;
	   cout<<"decrypted..........";
     
	    return 0;
       
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -