RSA实现

一、运算基础(大数相乘、取模、乘方、素数判定等)

为了方便,封装在一个DLL文件中(代码如下:)

1.头文件

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>

extern "C" __declspec(dllexport)
void Add (char *tempbuf1,   char *tempbuf2, char *result);  // 大数相加 result = tempbuf1 + tempbuf2

extern "C" __declspec(dllexport)
void Sub(char *tempbuf1, char *tempbuf2, char *result);     // 大数相减 result = tempbuf1 - tempbuf2 (默认前者大)

extern "C" __declspec(dllexport)
int Compare(char *tempbuf1,char *tempbuf2);  // 比较两数的大小

extern "C" __declspec(dllexport) 
void Chen (char *tempbuf1,   char *tempbuf2, char *result); // 大数相乘 result = tempbuf1 * tempbuf2

void Trans (char *str_num1,   char *str_num2, char *tempbuf1, char *tempbuf2); // 大数转化为等长,且最前面添加 “0”


extern "C" __declspec(dllexport)                                             // 大数除法 div_result = tempbuf1 / tempbuf2
void  Mod_Div(char *tempbuf1,   char *tempbuf2, char *mod_result  , char *div_result );    // 大数求余 result = tempbuf1 % tempbuf2  


extern "C" __declspec(dllexport)
void  ChenFang (char *tempbuf1,   int num, char *result);    // 大数乘方   result = tempbuf1 ^ num

extern "C" __declspec(dllexport)
void  ThrowAway_0 (char *tempbuf );  // 去除结果前面的 连续的无意义的 "0"

extern "C" __declspec(dllexport)
bool NiYuan(char *str1 , char *str2 , char *result) ;  // str1^(-1) mod str2 = result

extern "C" __declspec(dllexport)
void ToHex(char *num , char *Hex);  //大数转换成16进制

extern "C" __declspec(dllexport)
void ReverseStr(char *str , char *result); // 字符串str 逆序输出到 result

extern "C" __declspec(dllexport)
bool Is_Prime_Number(char *num);   //判定素数

2.主要代码:

//=============================================================================================================
#include "Big__CALC.h"


void Trans(char *str_num1, char *str_num2, char *tempbuf1, char *tempbuf2)
{
	int len_num1=0;
	int len_num2=0;
	int i=0;
	while(str_num1[i]!='\0')
	{
		len_num1++;
		i++;
	}	
	//	printf("字符串1的长度: length1=%d\n",len_num1);
	i=0;
	while(str_num2[i]!='\0')
	{
		len_num2++;
		i++;
	}	
	//	printf("字符串2的长度: length2=%d\n\n",len_num2);
	
	tempbuf1[0]='0';
	tempbuf2[0]='0';
	
	//=======================================================================
	if(len_num2>=len_num1)                                   //补成相同长度
	{
		for(i=1;i<=(len_num2-len_num1);i++)
		{
			tempbuf1[i]='0';
		}
		for(i=len_num2-len_num1+1;i<=len_num2;i++)
		{
			tempbuf1[i]=str_num1[i-(len_num2-len_num1+1)];
		}
		for(i=1;i<=len_num2;i++)
		{
			tempbuf2[i]=str_num2[i-1];
		}
	}
	//------------------------------------------
	else if(len_num2<len_num1)
	{
		for(i=1;i<=(len_num1-len_num2);i++)
		{
			tempbuf2[i]='0';
		}
		for(i=len_num1-len_num2+1;i<=len_num1;i++)
		{
			tempbuf2[i]=str_num2[i-(len_num1-len_num2+1)];
		}
		for(i=1;i<=len_num1;i++)
		{
			tempbuf1[i]=str_num1[i-1];
		}
	}
}

//====================================================================================================
extern "C" __declspec(dllexport)
void  Chen(char *tempbuf1,   char *tempbuf2 , char *result)
{
	char buf1[200]={0};
	char buf2[200]={0};
	Trans(tempbuf1,tempbuf2,buf1,buf2);
	strcpy(tempbuf1,buf1);
	strcpy(tempbuf2,buf2);
	
	int len=0;
	int i=0;
	int j=0;
	int n=0;
	int jinwei=0;
	
	while(tempbuf1[i]!='\0')  // 字符串长度
	{
		len++;
		i++;
	}
	
	int temp[200]={0};        // 该数组用来存储各次方的系数 10为底
	
	int max=2*len;
	for(i=0;i<=max;i++)
	{
		if(i<len-1)
		{
			for(j=0;(j<=len-1)&&(j>=i+1-len)&&(j<=i);j++)
			{			
				temp[i]+=((int)tempbuf1[len-1-j]-48 )*((int)tempbuf2[len-1-i+j]-48);  			
			}
			temp[i]+=jinwei;
			
			if (temp[i]>=10)  //&&temp[i]<100
			{
				jinwei  = temp[i]/10;
				temp[i] = temp[i]%10;
			}
			else jinwei=0;
		}
		else if (i>=len-1)
		{
			for(j=i-len+2;(j<=len-1)&&(j>=i+1-len)&&(j<=i);j++)
			{			
				temp[i]+=((int)tempbuf1[len-1-j]-48 )*((int)tempbuf2[len-1-i+j]-48);  			
			}
			temp[i]+=jinwei;
			
			if (temp[i]>=10)  //&&temp[i]<100
			{
				jinwei  = temp[i]/10;
				temp[i] = temp[i]%10;
			}
			else jinwei=0;
		}
		
		
	}
	
	i=max;
	while(i>=0)
	{
		if(temp[i]!=0)
			break;
		
		else i--;				
	}
	//	printf("str1 * str2= ");
	int num=i; // 科学计数法次数
			   /*	for (j=num;j>=0;j--)
			   {
			   
				 printf("%d",temp[j]);
				 
				   }	
				   printf("\t%d\n",num);
	*/
	//===========================================
	memset(result,0,200);
	for(i=num;i>=0;i--)
	{
		result[num-i]=(char)(temp[i]+48);
		
	}
	//	printf("result = %s\t%d\n",result,strlen(result)-1);
	ThrowAway_0 (result);
	
}


//=====================================================================

extern "C" __declspec(dllexport) 
void  ChenFang(char *tempbuf1,   int num, char *result)    // 大数乘方
{
	static int count=0;
	if (count==0)
	{
		char c[200]={'1',0,0};
		memset(result,0,200);
		Chen( tempbuf1 , c , result);
		
	}
	count++;
	
	if(num==1)
	{
		return ;
	}
	
    else if(num>1)
	{
		Chen( tempbuf1 , result , result);
		if (strlen(result)>100)
		{
			printf("超过100位!\n");
			printf("%d!\n\n",num);
			return ;
		}
		num--;
        ChenFang(tempbuf1,num,result);
	}
	
	ThrowAway_0 (result);
	
}


//=================================================

extern "C" __declspec(dllexport) 
void  Mod_Div(char *tempbuf1,   char *tempbuf2, char *mod_result , char *div_result )    // 大数求余
{
	strcpy(tempbuf1,tempbuf1);
	strcpy(tempbuf2,tempbuf2);

	memset(mod_result,0,200);
	ThrowAway_0 (tempbuf1);
	ThrowAway_0 (tempbuf2);
	if (tempbuf2[0]=='0')
	{
		printf("除数不能为0!\n");
		return ;
	}
	int  max1 = strlen(tempbuf1);
	int  max2 = strlen(tempbuf2);
	int  temp=0;
	int  ret=1;
	int  count=0;
	char div[200]={0};
	char buf[200]={0};
	char buf1[200]={0}; //存放temp2 * buf
	char AfterSub[200]={0};
	
	ret=Compare(tempbuf1,tempbuf2); 
	if(ret==0)  //tempbuf1 =tempbuf2,取模就是 0 ,商就是1
	{
		strcpy (mod_result,"0");
		memset(div_result,0,sizeof(div_result));
		div_result[0]='1';
		return ;
	}
	if(ret==-1)  // tempbuf1 < tempbuf2 取模就是 tempbuf1 ,商就是0
	{   
		strcpy (mod_result,tempbuf1);
		ThrowAway_0 (mod_result);
		memset(div_result,0,200);
		div_result[0]='0';
		return ;
	}
	if(ret==1) // tempbuf1 > tempbuf2
	{
		ThrowAway_0(tempbuf1);
		ThrowAway_0(tempbuf2);
		buf[0]=(char)( ((int)tempbuf1[0]-48) / ((int)tempbuf2[0]-48+1) +48);
		for (int i=1;i<=max1-max2;i++)
		{
			buf[i] = '0';
		}
		Chen(tempbuf2 ,buf , buf1);			
		Sub (tempbuf1 ,buf1,AfterSub); 
		memset(tempbuf1,0,200);
		strcpy(tempbuf1,AfterSub);
		
		ret=Compare(tempbuf1,tempbuf2);
		while(ret==1||ret==0)   // tempbuf1 > tempbuf2 ,继续减
		{			    			
			Sub (tempbuf1,tempbuf2,AfterSub);  
			memset(tempbuf1,0,200);
			strcpy (tempbuf1,AfterSub);	
			ret=Compare(tempbuf1,tempbuf2);
			ThrowAway_0 (tempbuf1);
			ThrowAway_0 (tempbuf2); 
			count++;
			if(count>=1000)
			{
				itoa(count,div,10);
				Add(div_result,div,div_result);
				count=0;
			}
			
		}
		
		strcpy (mod_result,tempbuf1);
		ThrowAway_0 (mod_result);
		itoa(count,div,10);
		Add(div_result,div,div_result);
		Add(div_result ,buf , div_result);	
	}
}



//========================================================

extern "C" __declspec(dllexport) 
void Add(char *tempbuf1,   char *tempbuf2, char *result) // 大数相加 result = tempbuf1 + tempbuf2
{
	char buf1[200]={0};
	char buf2[200]={0};

	Trans(tempbuf1,tempbuf2,buf1,buf2);
	strcpy(tempbuf1,buf1);
	strcpy(tempbuf2,buf2);
	
	int i=0;
	int temp=0;
	int jinwei=0;
	int len=0;
	while(tempbuf1[i]!='\0')
	{
		len++;
		i++;
	}
	for(i=len-1;i>=0;i--)
	{   
		temp=(int)(tempbuf1[i]+tempbuf2[i]+jinwei-96);
		if(temp>=10)
		{
			temp=temp-10;
			jinwei=1;
		}
		else jinwei=0;
		result[i]=(char)(temp+48);	
	}
	ThrowAway_0 (result);
	
}

//================================================
extern "C" __declspec(dllexport) 
void  ThrowAway_0 (char *tempbuf )  // 去除结果前面的 连续的无意义的 "0"
{
	char buf[200]={0};
	
	int n = strlen(tempbuf)-1;
	int i=0;
	while(i<n)
	{
		if (tempbuf[i]!='0')
		{
			break;
		}
		else 
		{
			i++;
		}
	}
	int Throw = i;
	for (i=0;i<=n-Throw;i++)
	{
		buf[i]=tempbuf[i+Throw];
	}
	
	strcpy(tempbuf,buf);
	
}

//=======================================================

extern "C" __declspec(dllexport) 
void Sub(char *tempbuf1, char *tempbuf2, char *result)     // 大数相减 result = tempbuf1 - tempbuf2
{
	ThrowAway_0 (tempbuf1);
	ThrowAway_0 (tempbuf2);
	memset(result,0,200);
	char buf1[200]={0};
	char buf2[200]={0};

	Trans(tempbuf1,tempbuf2,buf1,buf2);
	
	strcpy(tempbuf1,buf1);
	strcpy(tempbuf2,buf2);
	
	int i=0;
	int temp=0;
	int jiewei=0;
	int len=0;
	int ret=1;
	
	while(tempbuf1[i]!='\0')    // tempbuf1 和 tempbuf2 的长度相等
	{
		len++;
		i++;
	}
	
	ret = Compare(tempbuf1,tempbuf2);
	if(ret==1)
	{
		for(i=len-1;i>=0;i--)
		{  
			temp = (int)tempbuf1[i] - (int)tempbuf2[i] - jiewei;
			if (temp>=0)
			{
				result[i]=(char)(temp+48);
				jiewei=0;
			}
			else if (temp<0)
			{
				result[i]=(char)(temp+10+48);
				jiewei=1;
			}
		}
		ThrowAway_0 (result);
	}
	else if(ret==0) 
	{
		memset(result,0,200);
		result[0]='0';
	}
	else if(ret==-1) 
	{
		for(i=len-1;i>=0;i--)
		{  
			temp = (int)tempbuf2[i] - (int)tempbuf1[i] - jiewei;
			if (temp>=0)
			{
				result[i]=(char)(temp+48);
				jiewei=0;
			}
			else if (temp<0)
			{
				result[i]=(char)(temp+10+48);
				jiewei=1;
			}
		}
		ThrowAway_0 (result);
		memset(buf1,0,200);
		sprintf(buf1,"-%s",result);	
		strcpy(result,buf1);
	}
	
}

//======================================================================

//===================================================================================
extern "C" __declspec(dllexport) 
int Compare(char *tempbuf1,char *tempbuf2)
{
	ThrowAway_0 (tempbuf1);
	ThrowAway_0 (tempbuf2);
	char buf1[200]={0};
	char buf2[200]={0};

	Trans(tempbuf1,tempbuf2,buf1,buf2);
	memset(tempbuf1,0,200);
	memset(tempbuf2,0,200);
	strcpy(tempbuf1,buf1);
	strcpy(tempbuf2,buf2);
	
	
	int ret=1;
	int count=0;	
	while(count<200)
	{
		
		int m=(int)tempbuf1[count]-48;
		int n=(int)tempbuf2[count]-48;
		if(m==n)
		{
			count++;
			ret=0;
		}
		else if(m>n)
		{
			//	printf("tempbuf1>tempbuf2\n");
			ret=1;
			break;
			
		}
		else if(m<n)
		{
			//	printf("tempbuf1<tempbuf2\n");
			ret=-1;
			break;
		}
	}
	return ret;
}

//=====================================================
extern "C" __declspec(dllexport) 
bool NiYuan(char *str1 , char *str2 , char *result)  // str1^(-1) mod str2 = result
{
	
	char To_Div[200]={0};   //保存每次的   除数
	char To_Div_ed[200]={0}; //保存每次的  被除数
	char Mod[200]={0};      //保存每次的   余数
	char Div[200][200]={0}; //保存每次的   商
	char X[200][200]={0};   //保存每次     X系数
	char Y[200][200]={0};   //保存每次     Y系数
	char ZERO[200]={'0'};
	int i=0;
	
	memset(result,0,sizeof(result));
	strcpy(To_Div_ed, str2);
	strcpy(To_Div   , str1);
	
	while (1) //得到各循环时的商
	{
		Mod_Div(To_Div_ed , To_Div , Mod , Div[i]);
		ThrowAway_0(Mod);
		if ( Mod[0]=='0' )
		{
			break;
		}
		ThrowAway_0(To_Div);
		strcpy(To_Div_ed , To_Div);
		strcpy(To_Div , Mod);
		i++;
	}
	
	int n=i;
	i=0;
	int t=1;
	X[n-1][0]='1';
	X[n][0]='0';
	for (i=0;i<n-1;i++)
	{
		Chen(Div[n-1-i] , X[n-1-i] , Y[n-1-i]);	
		Add (Y[n-1-i] , X[n-i] ,  Y[n-1-i]);
		ThrowAway_0(Y[n-1-i]);
		strcpy( X[n-2-i] , Y[n-1-i]);
		
	}
	Chen(Div[0] , X[0] , Y[0]);
	Add (Y[0]   , X[1] , Y[0]);
	ThrowAway_0(Y[0]);
	strcpy( result, Y[0]);
	return true;

//以下两种方式效率低下

/*	char  YI[200]={'1',0};
    char buf[200]={'1',0}; // 存放每次递增的测试值
	char buf_mod[200]={0};
	char buf_div[200]={0};
	char temp[200]={0};

	while (1)
	{
		Chen(str2,buf,temp);
		Add(temp,YI,temp);
		Mod_Div(temp,str1,buf_mod,buf_div);
		ThrowAway_0 (buf_mod);
		if(strcmp(buf_mod,"0")==0)
		{
			strcpy(result,buf_div);
			ThrowAway_0 (result);
			return true;
		}
		Add(buf,YI,buf);
	}


	return true;
*/
/*	Mod_Div(str2,str1,buf,buf);
	while(1)
	{
		Chen(str1,buf,result);
		ThrowAway_0 (buf);
		ThrowAway_0 (str1);
		Mod_Div(result,str2,buf_mod,buf_div);
		ThrowAway_0 (buf_mod);
		if(strcmp(buf_mod,"1")==0)
		{
			strcpy(result,buf);
			ThrowAway_0 (result);
			return true;
		}
		
		Add(buf,YI,buf);
		
		if(strlen(result)>50)
		{
			printf("找不到逆元!\n");
			return false;
		}
	}
*/	
}
//==============================================
extern "C" __declspec(dllexport) 
void ToHex(char *num , char *Hex)
{
	static int count = 0;
	char ToDiv[200] = "16";
	//char Yi[200] = "1";
	char str_mod[200]  = {0};    //每次的余数
	char str_div[200]  = {0};    //每次的商
	static char mod[200]  = {0}; //每次的余数的16进制
	char _num[200]  = {0};
	if (count==0)
	{
		strcpy(_num,num);
	}
	
	Mod_Div(_num,ToDiv,str_mod,str_div);
	ThrowAway_0 (str_mod);
	ThrowAway_0 (str_div);
	int n_num = atoi(str_mod); 
	switch(n_num)
	{
	case 10:
		mod[count]='A';
		break;
	case 11:
		mod[count]='B';
		break;
	case 12:
		mod[count]='C';
		break;
	case 13:
		mod[count]='D';
		break;
	case 14:
		mod[count]='E';
		break;
	case 15:
		mod[count]='F';
		break;
	default:
		mod[count]=str_mod[0];
		break;
	}
	
	if( -1== Compare(str_div,ToDiv)) //结束的条件
	{
		count++;
		ThrowAway_0 (str_div);
		n_num = atoi(str_div); 
		switch(n_num)
		{
		case 10:
			mod[count]='A';
			break;
		case 11:
			mod[count]='B';
			break;
		case 12:
			mod[count]='C';
			break;
		case 13:
			mod[count]='D';
			break;
		case 14:
			mod[count]='E';
			break;
		case 15:
			mod[count]='F';
			break;
		default:
			mod[count]=str_div[0];
			break;
		}	
		strcpy(Hex , mod);
		memset(str_div,0,sizeof(str_div));
		ReverseStr(Hex,str_div);
		memset(Hex,0,sizeof(Hex));
		strcpy(Hex,str_div);
		ThrowAway_0 (Hex);
		return ;
	}
	
	count++;
	memset(_num,0,200);
	strcpy(_num,str_div);
	ToHex(_num,Hex);
}

//==========================================
extern "C" __declspec(dllexport) 
void ReverseStr(char *str, char *result)
{
	int count = strlen(str);
	int i = 0;
	for (i=0;i<count;i++)
	{
		result[i] = str[count-1-i];
		
	}
}


//====================================
extern "C" __declspec(dllexport) 
bool Is_Prime_Number(char *num)
{
	int  count=2;
	int  n_mod=0;
	int  n_temp=0;
	char YI[200]="1";
	char SHIWAN[200]="10000";
	char temp_num[200]={0};
	char buf[200]="2";
	char str_div[200]={0};
	char str_mod[200]={0};
	strcpy(temp_num,num); //暂存num
	int ret = -1;
	printf("----------------\n"); //测试++++++++++++++++++++++++++++++++++++++++++++++
	if (Compare(SHIWAN,temp_num)==1)
	{
		ThrowAway_0 (temp_num);
		n_temp=atoi(temp_num);
		while(count<n_temp)
		{
			n_mod=n_temp%count;
			if (n_mod==0)
			{
				return false;
			}
			count++;
		}
		return true;
	}
	
    else 
	{
		while(ret==-1)
		{
		memset(str_div,0,200);
		memset(str_mod,0,200);

		Mod_Div(temp_num,buf,str_mod,str_div);
		ThrowAway_0 (str_mod);
		if(str_mod[0]=='0')       // (strcmp(str_mod,"0") == 0) 
		{
			return false;
		}
		memset(temp_num,0,200);
		strcpy(temp_num,num);
		
		Add(buf,YI,buf);printf("%s\t%s\t%s\t\n",temp_num,buf,str_mod);//测试++++++++++++++++++++++++++++++++++++++++++++++
		ThrowAway_0(buf);
		ThrowAway_0(YI);
		ret=Compare(buf,temp_num);
		}
	    printf("buf is: %s ----------------\n",buf);
        return true;
	}
}

//WITNESS算法--判定大素数	
/*	while(ret==-1)
	{
		Chen(buf,buf,temp);
		Mod_Div(temp,temp_num,str_mod,str_mod); //不关心商
		if (strcmp(str_mod,"1") == 0) 
		{
			return false;
		}
		Add(buf,YI,buf);
		ret=Compare(buf,temp_num);
	}
	return true;
*/

二、RSA加密解密部分
基本代码:
void CRSADlg::OnEncrypt() // 加密文件内容
{
	CString strPath="";
	char    buf[200]={0};
	char    EncryptedFile[200]={0};
	char    chenfang[200]={0};
	char    PubKey[1024]={0};
	char    PubKey_N[200]={0};
	char    PubKey_D[200]={0};
	CFileDialog FileDlg(TRUE , ".txt" , NULL , OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		                "dat file(*.dat)|*.dat|txt file(*.txt)|*.txt|all files(*.*)|*.*||",NULL);	
	if( FileDlg.DoModal()==IDOK)
	{
		strPath=FileDlg.GetPathName();
	}

	CFile PubKeyFile("PubKey.dat",CFile::modeRead);  // 解析并读出密钥
	PubKeyFile.Read(PubKey,1024);
	PubKeyFile.Close();
	CString str=PubKey;  
	int index1=str.Find('D');
	int index2=str.Find('N');
	CString temp1=str.Left(index2);                              
    CString temp2=temp1.Mid(index1+3,index2-index1-1);	
	strcpy(PubKey_D,temp2.GetBuffer(0));          //MessageBox(PubKey_E);	
	temp1=str.Right(str.GetLength()-index2-3);	  
	strcpy(PubKey_N,temp1.GetBuffer(0));		  //MessageBox(PubKey_N);

	int ret=3;
	CFile PlainFile(strPath,CFile::modeRead);
	CFile File("Encrypted.dat",CFile::modeCreate |CFile::modeReadWrite );

//	while(ret==3)
	{	
		ret = PlainFile.Read(buf,3); //读出需要加密的文件内容
		MessageBox(buf);		
		//ChenFang(buf,atoi(PubKey_E),chenfang);             
		//Mod_Div(chenfang,PubKey_N,EncryptedFile,EncryptedFile); //此处效率极低,换成下面这张种,实现在DLL中

		ChengFang_Mod(buf,PubKey_D,PubKey_N,EncryptedFile);
		//MessageBox(EncryptedFile);

		File.Write(EncryptedFile,strlen(EncryptedFile)+1);	
	}
	PlainFile.Close();
	File.Close();

	MessageBox("Over !");
}

void CRSADlg::OnDecrypt() // 解密文件内容
{
	CString strPath="";
	char    buf[200]={0};
	char    DecryptedFile[200]={0};
	char    chenfang[200]={0};
	char    PrivateKey[1024]={0};
	char    PrivateKey_N[200]={0};
	char    PrivateKey_E[200]={0};
	CFileDialog FileDlg(TRUE , ".txt" , NULL , OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
	                    "dat file(*.dat)|*.dat|txt file(*.txt)|*.txt|all files(*.*)|*.*||",NULL);	
	if( FileDlg.DoModal()==IDOK)
	{
		strPath=FileDlg.GetPathName();
	}
	
	CFile PrivateKeyFile("PrivateKey.dat",CFile::modeRead); //读出密钥
	PrivateKeyFile.Read(PrivateKey,1024);
	PrivateKeyFile.Close();
	CString str=PrivateKey;  
	int index1=str.Find('D');
	int index2=str.Find('N');
	CString temp1=str.Left(index2);                              
    CString temp2=temp1.Mid(index1+3,index2-index1-1);	
	strcpy(PrivateKey_E,temp2.GetBuffer(0));          //MessageBox(PubKey_D);	
	temp1=str.Right(str.GetLength()-index2-3);	  
	strcpy(PrivateKey_N,temp1.GetBuffer(0));		  //MessageBox(PubKey_N);
	
	CFile PlainFile(strPath,CFile::modeRead);
	CFile File("Encrypted.dat",CFile::modeCreate |CFile::modeReadWrite );
	
	int ret=4;
	while(ret==4)
	{	
		ret = PlainFile.Read(buf,4); //读出需要加密的文件内容
		//MessageBox(buf);
		
		//enFang(buf,atoi(PrivateKey_D),chenfang);
		//d_Div(chenfang,PrivateKey_N,DecryptedFile,EncryptedFile);//此处效率极低,换成下面这张种,实现在DLL中
		ChengFang_Mod(buf,PrivateKey_E,PrivateKey_N,DecryptedFile);
				
		File.Write(DecryptedFile,strlen(DecryptedFile)+1);	
	}	
	PlainFile.Close();
	File.Close();
	
}

void CRSADlg::OnSignatul()  // 进行数字签名
{
	
}

void CRSADlg::OnRenzheng() // 验证数字签名
{
	
}

void CRSADlg::OnQuit() // 退出程序
{
	PostQuitMessage(1);	
}


void CRSAInit::OnCreateP() //生成大素数 P
{
	//SetDlgItemText(IDC_EDIT_P,"正在生成....请等待");
	//UpdateData(FALSE);
	//Invalidate();
	char ch_rand[200]={0};
	char buf[256]={0};
	while(1)
	{
		srand( (unsigned)time( NULL ) );
		for (int i=0;i<KEYLENGTH;i++)
		{
			ch_rand[i]=(char)(rand()%10+48);
		}
		if ((int)(ch_rand[KEYLENGTH-1]-48)%2==0)
		{
			continue;
		}
		ThrowAway_0(ch_rand);
		bool ret=Is_Prime_Number(ch_rand);
		if (ret==true)
		{
			//sprintf(buf,"%s \n是素数!",ch_rand);
			//MessageBox(buf);
			SetDlgItemText(IDC_EDIT_P,ch_rand);
			memset(m_P,0,200);
			strcpy(m_P,ch_rand);
			return ;
		}
	//	else sprintf(buf,"%s \n不是素数!",ch_rand);
	}

}

void CRSAInit::OnCreateQ() //生成大素数 Q
{
	//SetDlgItemText(IDC_EDIT_Q,"正在生成....请等待");
	//UpdateData(FALSE);
	char ch_rand[200]={0};
	char buf[256]={0};
	memset(m_Q,0,200);
	while(1)
	{
		srand( (unsigned)time( NULL ) );
		for (int i=0;i<KEYLENGTH;i++)
		{
			ch_rand[i]=(char)(rand()%10+48);
		}
		if ((int)(ch_rand[KEYLENGTH-1]-48)%2==0)
		{
			continue;
		}
		ThrowAway_0(ch_rand);
		if (strlen(ch_rand)<KEYLENGTH)
		{
			memset(ch_rand,0,200);
			continue;
		}
		bool ret=Is_Prime_Number(ch_rand);
		if (ret==true)
		{
			//sprintf(buf,"%s \n是素数!",ch_rand);
			//MessageBox(buf);
			SetDlgItemText(IDC_EDIT_Q,ch_rand);
			strcpy(m_Q,ch_rand);
			return ;
		}
	//	else sprintf(buf,"%s \n不是素数!",ch_rand);
	}	
}

void CRSAInit::OnCreateN() //生成 N=P*Q
{
	if (strlen(m_P)<1)
	{
		MessageBox("请先生成P");
	}
	if (strlen(m_Q)<1)
	{
		MessageBox("请先生成Q");
	}
	Chen(m_P , m_Q , m_N);	
	SetDlgItemText(IDC_EDIT_N,m_N);
	UpdateData(FALSE);
}

void CRSAInit::OnCreateFi() // 计算 FI(n)=(P-1)*(Q-1);                                                        
{
	char temp1[200]={0};
	char temp2[200]={0};
	char YI[200]="1";
	Sub(m_P , YI , temp1);      
	Sub(m_Q , YI , temp2);		
	Chen(temp1 , temp2 , m_FI);
	SetDlgItemText(IDC_EDIT_FI,m_FI);
	UpdateData(FALSE);
}

void CRSAInit::OnCreateE() //生成私钥 E
{
	char ch_rand[200]={0};
	char buf[256]={0};
	memset(m_E,0,200);
	while(1)
	{
		srand( (unsigned)time( NULL ) );
		for (int i=0;i<KEYLENGTH-1;i++)
		{
			ch_rand[i]=(char)(rand()%10+48);
		}
		if ((int)(ch_rand[KEYLENGTH-2]-48)%2==0)
		{
			continue;
		}
		ThrowAway_0(ch_rand);
		bool ret=Is_Prime_Number(ch_rand);
		if (ret==true)
		{
			//sprintf(buf,"%s \n是素数!",ch_rand);
			//MessageBox(buf);
			SetDlgItemText(IDC_EDIT_E,ch_rand);
			strcpy(m_E,ch_rand);
			return ;
		}		
	}
}

void CRSAInit::OnCreateD() //生成公钥的 D
{
	memset(m_D,0,200);
	NiYuan(m_E , m_FI , m_D);
	SetDlgItemText(IDC_EDIT_D,m_D);
	UpdateData(FALSE);
	
}

void CRSAInit::OnSavepubkey()    // 保存公钥
{
	CFile file("PubKey.dat",CFile::modeCreate |CFile::modeReadWrite );
	char  buf[1000]={0};
	sprintf(buf , "publickey:\r\nD: %s\r\nN: %s",m_D,m_N);
	file.Write(buf,strlen(buf)+1);
	file.Close();
}

void CRSAInit::OnSaveprivatekey() //保存私钥
{
	CFile file("PrivateKey.dat",CFile::modeCreate |CFile::modeReadWrite );
	char  buf[1000]={0};
	sprintf(buf , "privatekey:\r\nE: %s\r\nN: %s",m_E,m_N);
	file.Write(buf,strlen(buf)+1);
	file.Close();	
}

void CRSAInit::OnOver()  //生成密钥完成
{
	CDialog::OnOK();		
}

void CRSAInit::OnCancel() 
{
	CDialog::OnCancel();	
}


部分代码待完善

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值