RSA算法C++实现--密码学实验

#include
#include
#include
#include
#include
using namespace std;

typedef long long LL;

LL p,q,n,n_Euler,e,d;
LL Plaintext[100];//明文
LL Ciphertext[100];//密文
LL Plaintext_C[100];
LL x,y;

LL mul(LL x,LL y){
	return x*y; 
}

/*用于判断一个数是否为素数*/
bool is_prime(LL x){
	if(x<2)return false;
	for(int i=2;i<=x/i;i++){
		if(x%i==0)return false;
	}
	return true;
} 

/*用于输入两个素数p和q*/ 
void prime_create(){ 
	cout<<"---------------prime_create Start---------------------"<<endl;
	cout<<"请输入两个保密的大素数p和q的值!"<<endl;
	cout<<"请输入p的值:";
	cin>>p;
	while(!is_prime(p)){
		cout<<"p不为素数,请重新输入p的值:";
		cin>>p;
	}
	cout<<"请输入q的值:";
	cin>>q;
	while(!is_prime(q)){
		cout<<"q不为素数,请重新输入p的值:";
		cin>>q;
	}
	cout<<"成功输入p和q的值分别为: p:"<<p<<" q:"<<q<<endl;
	cout<<"---------------prime_create End---------------------"<<endl;	
} 

LL Randomly_e(LL t){
	LL Number=0;
	srand((LL)time(NULL));
	do{
		Number=rand()%(t-2)+1;
	}while(!is_prime(Number));
	cout<<"通过Randomly_e函数,随机生成了一个大于1小于n_Euler的数e:"<<Number<<endl; 
	return Number;
}

LL Cal_d(LL e,LL model,LL* d){
    LL i;
    LL over=e;
    for (i=1;i<model;){
        over=over%model;
        if(over==1){
            *d=i;
            return 1;
        }
        else{
            if(over+e<=model){
            	do{
                    i++;
                    over+=e;
                }while(over+e<=model);
            }
            else{
                i++;
                over+=e;
            }
        }
    }
    return 0;
}
//模n的大数幂乘的快速算法
LL Power_mul(LL a,LL b,LL c,LL n){
	//初始a=x,b=r,c=1
	while(b!=0){
		while(b%2==0){
			b/=2;
			a=(a*a)%n;
		}
		b-=1;
		c=(c*a)%n;
	}
	return c;
}

//明文读入,素数取的比较小,所以直接用了ASCII码来写
void Initialize(){
	char ch[] = "I LOVE NANCHANG UNIVERSITY";
	int counter=0;
	while (ch[counter] != '\0')
	{
		counter++;
	}
	int i,j=0;
	for(i=0;i<100;i++)
	{
		if(j!=counter)
		{
			Plaintext[i]=ch[j];
			j++;
		}
		else
		{
			Plaintext[i]=32;//空格
		}
 
	}
	cout<<"明文如下:";
	for(i=0;i<100;i++)
	{
		printf("%c",Plaintext[i]);
	}
	cout<<endl;
}
/*1.密钥的产生*/ 
void Key_generation(){
	prime_create(); //产生两个大素数p和q 
	
	n=mul(p,q);//计算n=p*q
	n_Euler=mul(p-1,q-1);//计算n_Euler=(p-1)*(q-1)
	cout<<"计算出n为:"<<n<<" n_Euler为:"<<n_Euler<<endl;
	
	e=Randomly_e(n_Euler);
	
	Cal_d(e,n_Euler,&d);
	
	cout<<"通过Cal_d,计算出d的值为:"<<d<<endl;
	
	cout<<"---------------公开钥和私密钥-------------------"<<endl;	
	cout<<"{e,n}为公开钥:"<<"e为:"<<e<<" n为:"<<n<<endl;
	cout<<"{d,n}为秘密钥:"<<"d为:"<<d<<" n为:"<<n<<endl;
	cout<<"---------------密钥产生完毕---------------------"<<endl;
}


void RSA_Encryption(){
	Initialize();
	for(int i=0;i<100;i++)
		Ciphertext[i]=Power_mul(Plaintext[i],e,1,n);
 
	cout<<endl;
	cout<<"用公钥(e, n)加密出的密文如下:";
	for(int i=0;i<100;i++)
		cout<<Ciphertext[i]<<" ";
	cout<<endl;
	cout<<"---------------RSA加密完成!---------------------"<<endl;
}

//RSA解密
void RSA_Decryption()
{
	for(int i=0;i<100;i++)
		Plaintext_C[i] = Power_mul(Ciphertext[i], d, 1, n);
 
	cout<<endl;
	cout<<"用私钥(d, n)解密出的明文如下:" ;
	for(int i=0;i<100;i++)
		printf("%c",Plaintext_C[i]);
	cout<<endl;
	cout<<"---------------RSA解密完成!---------------------"<<endl;
}

int main(){
	/*密钥的产生*/ 
	Key_generation(); 
	/*加密*/
	RSA_Encryption(); 
	/*解密*/
	RSA_Decryption();
} 

你可能感兴趣的:(c++,算法,开发语言,rsa)