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

📄 rsa.cpp

📁 RSA加密程序
💻 CPP
字号:
// RSA.cpp : Defines the entry point for the console application.
//
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "stdafx.h"
#include "RSA.h"
#include "BigInt.h"	//大数类头文件

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define DEFAULT 65536

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

int GetPrime(int maxnum);
void CopyRight();
void ShowMenu();
void GenPubKey();
void GenPriKey();
void Encrypt();
void Decrypt();
void DetectError();

CBigInt bigp,bigq,bign,bigfai_n;
CBigInt bige,bigd;
CBigInt bigmsg, bigencrypted, bigdecrypted;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		CopyRight();
		ShowMenu();
	}

	return nRetCode;
}

void CopyRight()
{
	cout<<"==============================================================="<<endl;
	cout<<"            Welcome to Zhu Lingchen's RSA Application"<<endl;
	cout<<"               CopyRight 2007, All rights reserved."<<endl;
	cout<<"                         Menu Options"<<endl;
	cout<<"==============================================================="<<endl;
}

void ShowMenu()
{
	int choice;

	cout<<"1. Generate Public-Key"<<endl;
	cout<<"2. Generate Private-Key"<<endl;
	cout<<"3. Encrypt"<<endl;
	cout<<"4. Decrypt"<<endl;
	cout<<"0. Exit"<<endl<<endl;
	cout<<"Choice: ";
	cin >> choice;
	if (choice < 0 || choice > 4)
	{
		cout<<"Invalid Choice!"<<endl;
		return;
	}
	else if (!choice)
		return;
	else
	{
		switch (choice)
		{
		case 1:
			GenPubKey();
			break;
		case 2:
			GenPriKey();
			break;
		case 3:
			Encrypt();
			break;
		case 4:
			Decrypt();
			break;
		}
	}
}

//产生小于maxnum的某个质数
int GetPrime(int maxnum)
{
	if (maxnum >= 50000)		//防止在计算质数上消耗太多系统资源
		maxnum = 50000;
	int temp;
	int prime[DEFAULT]={0};
	int count=0;
	for( int i=2; i < maxnum; i++)
	{
		if( i>2 && i%2==0 )//大于2的偶数都不是是质数
			continue;

		int j=3;//判断3,5,7,9……i/2是否有i的因子
		while( j <= i/2 && i%j )
			j += 2;

		//若上述数都不是i的因子,则i是质数

		if(j > i/2)
		{
			prime[count] = i;
			count++;
		}
	}
	srand( (unsigned)time( NULL ) );
	do
	{
		temp = rand() % count;
	}while( temp == 0 );

	return prime[temp];
}

void GenPubKey()
{
	CString str, str1;

	CBigInt bigptemp,bigqtemp;

	long fai_n;
	long p,q;
	//输入质数对p和q
	cout << "Input the PRIME NUMBER p and q: "<<endl;
	cin >> p >> q;
	cout << " p = " << p << "\t" << " q = " << q <<endl;
	//转化为大数表达方式
	bigp.Mov( p );
	bigq.Mov( q );
	//大数运算: n=p*q
	bign.Mov( bigp.Mul( bigq ) ); 
	bign.OutPutToStr(str, 10);
	cout <<" n = p * q = "<<atoi(str) <<endl;

	//大数运算: 求fai(n)
	bigptemp.Mov( bigp.Sub(1) );
	bigqtemp.Mov( bigq.Sub(1) );
	bigfai_n.Mov( bigptemp.Mul( bigqtemp ) );
	bigfai_n.OutPutToStr(str, 10);
	fai_n = atoi( str );
	cout <<" fai(n) = (p-1) * (q-1) = "<< fai_n <<endl;

	bige.Mov( GetPrime(fai_n) ); //求e,e是一个不大于fai_n的质数
	bige.OutPutToStr(str, 10);
	cout <<" e = "<<atoi( str ) <<endl;

	bign.OutPutToStr(str, 10);
	bige.OutPutToStr(str1, 10);

	cout <<" Public-Key is ("<<atoi(str)<<","<<atoi(str1)<<")"<<endl;


//-------------------------------
	system("pause");
	cout << endl;
	ShowMenu();
}

void GenPriKey()
{
	DetectError();
	CString str,str1;
	bigd = bige.Euc( bigfai_n ); //d*e (mod fai_n) = 1,返回d
	bigd.OutPutToStr(str, 10);
	cout <<" d = "<<atoi( str ) <<endl;
	bign.OutPutToStr(str1, 10);

	cout <<" Private-Key is ("<<atoi(str1)<<","<<atoi(str)<<")"<<endl;
	//-------------------------------
	system("pause");
	cout << endl;
	ShowMenu();
}

void Encrypt()
{
	DetectError();
	long msg;
	CString str;
	
	cout<<"Input the message to be Encrypted: ";
	cin >> msg;
	cout <<"Message m = "<<msg<<endl;
	bigmsg.Mov( msg );
	bigencrypted = bigmsg.Mon( bige, bign);
	bigencrypted.OutPutToStr(str, 10);

	cout << "The ENCRYPTED code is: "<<atoi(str)<<endl;
	//-------------------------------
	system("pause");
	cout << endl;
	ShowMenu();
}

void Decrypt()
{
	DetectError();
	CString str,str1;
	bigencrypted.OutPutToStr(str, 10);
	cout << "The ENCRYPTED code is: "<<atoi(str)<<endl;

	bigdecrypted = bigencrypted.Mon( bigd, bign);
	bigdecrypted.OutPutToStr(str1, 10);
	cout << "The DECRYPTED code is: "<<atoi(str1)<<endl;
	//-------------------------------
	system("pause");
	cout << endl;
	ShowMenu();
}

void DetectError()
{
	CString str,str1;
	bigp.OutPutToStr(str, 10);
	bigq.OutPutToStr(str1, 10);
	if ( !atoi(str) || !atoi(str1) )
	{
		cout << "Error! The keys have not been generated!" << endl;
		return;
	}
}

⌨️ 快捷键说明

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