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

📄 rsa.cpp

📁 rsa加密算法很好的例子
💻 CPP
字号:
#include <iostream.h>
#include <stdlib.h>
#include <string.h>

#include "CRSA.h"

void checkparams(bool&,long&,long&,long&,char*,char*,char**,int,char**);
void printUsage();

void main(int argc, char* argv[])
{

	bool enc = false;  // whether to run for encryption or decryption
	long e; // the encryption exponent
	long p; // the first prime number
	long q; // the second prime number
	char* ifilename = NULL; // file to read input from
	char* ofilename = NULL; // file to write output to
	char* data = NULL; // data string to encrypt/decrypt
	char* cipher = NULL; // the encrypted data
	char* dec = NULL; // the decrypted data

	checkparams(enc,e,p,q,ifilename,ofilename,&data,argc,argv);

	CRSA crsa;

	if (!crsa.verify(e,p,q))
	{
		cout << "Error: e, p and q must be prime and ";
		cout << endl << "e, n must be relatively prime." << endl;

		exit(1);
	}
	else if (p < 40 || q < 40 || abs(p-q)<15)
	{
		cout << "Error! Too small p and q";
		cout << endl << "must be at least 40 and p-q > 14.";
		exit(1);
	}

	if (enc)
	{ // encrypt the data

		cipher = crsa.Encrypt(data,e,p,q);
		cout << endl << "RSA Cipher: " << cipher << endl;

	}
	else
	{ //decrypt the data

		dec = crsa.Decrypt(data,e,p,q);
		cout << endl << "Decrypted: " << dec << endl;
	}
}

//funtion to check the validity of input parameters
void checkparams(bool& enc,long& e,long& p,long& q,char* ifilename,char* ofilename,char** data,int argc,char* argv[])
{

	if (argc < 10)
	{ // invalid number of arguments
		printUsage();
	}

	for(int i = 1 ; i < argc ; i++)
	{

		if (strcmp(argv[i],"enc") == 0)
		{ // run for RSA encryption
			enc = true;
		}
		else if (strcmp(argv[i],"dec") == 0)
		{ // run for RSA decryption
			enc = false;
		}
		else if (strcmp(argv[i],"-e") == 0)
		{ // the encryption exponent
			if (++i != argc)
				e = atol(argv[i]);
			else
				printUsage();
		}
		else if (strcmp(argv[i],"-p") == 0)
		{ // first prime number
			if (++i != argc)
				p = atol(argv[i]);
			else
				printUsage();
		}
		else if (strcmp(argv[i],"-q") == 0)
		{ // second prime number
			if (++i != argc)
				q = atol(argv[i]);
			else
				printUsage();
		}
		else if (strcmp(argv[i],"-if") == 0)
		{ // read from a file
			if (++i != argc)
				ifilename = argv[i];
			else
				printUsage();
		}
		else if (strcmp(argv[i],"-of") == 0)
		{ // write output to a file
			if (++i != argc)
				ofilename = argv[i];
			else
				printUsage();
		}
		else if (strcmp(argv[i],"-d") == 0)
		{ // this is the data to encode/decode
			if (++i != argc)
				*data = argv[i];
			else
				printUsage();
		}
		else
		{ // invalid input parameter
			printUsage();
		}
	}
}

//function to display help and exit
void printUsage()
{

	cout << endl << "RSA Encryption and Decryption" << endl;

	cout << endl << "rsa [enc|dec] -e E -p P -q Q [-if file|-d data] [-of]" << endl;
	cout << endl << "enc   - Run the program for encryption";
	cout << endl << "dec   - Run the program for decryption";
	cout << endl << "-e    - The next argument is the encryption exponent";
	cout << endl << "-p    - The next argument is the first prime number";
	cout << endl << "-q    - The next argument is the second prime number";
	cout << endl << "-if   - Read data to enc/dec from the given input file";
	cout << endl << "-of   - Write the output to the given file";
	cout << endl << "-d    - Encrypt/decrypt the given data";

	cout << endl << endl;

	exit(1);
}

⌨️ 快捷键说明

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