examc_003.cpp

来自「RSA源码。实现数字签名」· C++ 代码 · 共 94 行

CPP
94
字号
// examC_003.cpp : Defines the entry point for the console application.
//

/****RSA****/
#include "stdafx.h"
#include "iostream.h"
int candp(int x,int c,int n)
{ 
	long z=1;
	int cc[16];
	for(int j=15;j>=0;j--)
	{
		cc[j]=c%2;
		c=c/2;
	}
	for(j=0;j<16;j++)
	{
		cout<<cc[j];
	}
	cout<<endl;
	for(int i=0;i<16;i++)
	{
		if(cc[i]==1)
		{
			z=z*z;
			while(z>n)
				z-=n;
			z=z*x;
			while(z>n)
				z-=n;
		}
		else
		{
			z=z*z;
			while(z>n)
				z-=n;
		}
	}
	return z;
}

void main()
{
	int p,q,e,d,m,n,t,c,r;
	char s;
	do{
		cout<<"input the p = ";
		cin>>p;
		cout<<"input the q = ";
		cin>>q;
		n=p*q;
		cout<<"so , the n  = "<<n<<endl;
		t=(p-1)*(q-1);
		cout<<"so , the t  = "<<t<<endl;
		cout<<"please intput the e = ";
		cin>>e;
		if(e<1||e>t)
		{
			cout<<"e is error,please input again";
			cin>>e;
		}
		d=1;
		while  (((e*d)%t)!=1)   
			d++;
		cout<<"then caculate out that the d = "<<d<<endl;
		cout<<"if you want to konw the cipher please input 1"<<endl;
		cout<<"if you want to konw the plain  please input 2"<<endl;
		cin>>r;
		if(r==1)
		{
			cout<<"input the m :";/*输入要加密的明文数字*/
			cin>>m;
			cout<<"the binary of e: ";
			c=candp(m,e,n);
			cout<<"so ,the cipher is "<<c<<endl;
			cout<<"do you want to use this program:Yes or No"<<endl;
			s=getchar();
		}
		if(r==2)
		{
			cout<<"input the c :";/*输入要解密的密文数字*/
			cin>>c;
			cout<<"the binary of d: ";
			m=candp(c,d,n);
			cout<<"so ,the cipher is "<<m<<endl; 
			cout<<"do you want to use this program:Yes or No"<<endl;
			s=getchar();
		}
	}while(s=='Y' || s=='y');
}



⌨️ 快捷键说明

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