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

📄 errortest.cpp

📁 RSA加密算法的实现 2^1024大的素数实现
💻 CPP
字号:
#include"function.h"
#include<fstream>
#define TEST_TIMES 1000
/*------------------------------------------------------
instruction: in this part I offer some code  to help
             testing the correctness of this programe 
--------------------------------------------------------*/
//test operator "+" "-" "*" "/" and "%".
void OperatorTest()
{ 
	ofstream Outf("OperatorTest.txt");
    mathInt a,b;
	long int num1;
	long int num2;
	long int result;
	mathInt sum;
	mathInt diff;
	mathInt multy;
	mathInt quotient;
	mathInt remain;
	bool sign_a[4] = {1,1,0,0};
	bool sign_b[4] = {1,0,1,0};

	srand(time(NULL));  

    //first way
    /*cout<<"-----------------------------------------------------------------"<<endl;
	//test addtion and subtraction;
	for(int j=0;j<=20;j++)
	{
		for(int i=0;i<=3;i++)
		{
			cout<<"----------------CASE:"<<i+1<<"------------------"<<endl;
			a.set_coeff(sign_a[i],rand()%65);
		    b.set_coeff (sign_b[i],rand()%65);
		    cout<<a.length ()<<"---"<<b.length ()<<endl;
			//a.display();
			//b.display();
			sum = a + b;
			if(sum-a==b)
			{
				//cout<<"sum - a == b;"<<" sum is"<<endl;
				//sum.display ();
				cout<<"RIGHT~";
				cout<<endl;
			}
			else
				cout<<"BUG!!! sum - a != b"<<endl;
			diff = a-b;
			if(diff+b==a)
			{
				//cout<<"diff + b == a; "<<"diff is"<<endl;
				//diff.display ();
				cout<<"RIGHT~";
				cout<<endl;
			}
			else
				cout<<"BUG!!! diff + b != a"<<endl;
		}
		cout<<"**************************************************************"<<endl;
		//test multiplication and division
		for(int i=0;i<=3;i++)
		{
			cout<<"----------------CASE:"<<i+1<<"------------------"<<endl;
            a.set_coeff(sign_a[i],rand()%65);
			b.set_coeff (sign_b[i],rand()%64+1);
			cout<<a.length ()<<"---"<<b.length ()<<endl;
			switch(i){
					 case 0:case 1:
							quotient = a/b;
							remain = a%b;
							if(quotient*b+remain==a)
							{
								//cout<<"quitent*b + remain==a"<<endl;
								//diff.display ();
								cout<<"RIGHT~";
								cout<<endl;
							}
							else
								cout<<"BUG!!!quitent*b + remain!=a"<<endl;
							break;
					 case 2:
							quotient = a/b;
							remain = a%b;
							if(a==ZERO)
								break;
							if(quotient*b + remain - b==a)
							{
								//cout<<"negative/positive-->quitent*b + remain - b==a"<<endl;
								//diff.display ();
								cout<<"RIGHT~";
								cout<<endl;
							}
							else
								cout<<"BUG!!!quitent*b + remain!=a"<<endl;
							break;
					 case 3:
							quotient = a/b;
							remain = a%b;
							if(a==ZERO)
								break;
							if(quotient*b +remain - b.abs()==a)
							{
								//cout<<"negative/negative-->quitent*b + remain==a"<<endl;
								//diff.display ();
								cout<<"RIGHT~";
								cout<<endl;
							}
							else
								cout<<"BUG!!!quitent*b + remain!=a"<<endl;
							break;
					 default:
							break;
				   }

		}
	}*/

	//another test
    for(int i=1;i<=TEST_TIMES;i++)
	{
		 num1 = rand();
		 num2 = rand();
		 Outf<<num1<<std::endl;
		 Outf<<num2<<std::endl;
		 result = num1+num2;
		 a = num1;
		 b = num2;

		 sum = a+b;                                   //add 
		 if(sum==result)
		 {
			 cout<<"right"<<endl;
			 Outf<<"ritht"<<std::endl;
		 }
		 else
		 {
			 cout<<"BUG!!!!!!!!!!!!!!!!!!-->add"<<endl;
			 Outf<<"BUG!!!!!!!!!!!!!!!!!!-->add"<<endl;
		 }

		 result = num1 - num2;                       //subtract
		 diff = a - b;
		 if(diff==result)
		 {
			 cout<<"right"<<endl;
			 Outf<<"ritht"<<std::endl;
		 }
		 else
		 {
			 cout<<"BUG!!!!!!!!!!!!!!!-->subtract"<<endl;
			 Outf<<"BUG!!!!!!!!!!!!!!!-->subtract"<<endl;
		 }

		 result = num1*num2;                         //multiply
		 multy = a*b;
		 if(multy==result)
		 {
			 cout<<"right"<<endl;
			 Outf<<"ritht"<<std::endl;
		 }
		 else
		 {
			 cout<<"BUG!!!!!!!!!!!!!-->multiple"<<endl;
			 Outf<<"BUG!!!!!!!!!!!!!-->multiple"<<endl;
		 }


		 result = num1/num2;
		 quotient = a/b;                            //division
		 if(quotient==result)
		 {
			 cout<<"right"<<endl;
			  Outf<<"ritht"<<std::endl;
		 }
		 else
		 {
			 cout<<"BUG!!!!!!!!!!!!!!!-->quotient"<<endl;
			 cout<<"BUG!!!!!!!!!!!!!!!-->quotient"<<endl;
		 }

		 result = num1%num2;
		 remain = a%b;                              //mod
		 if(remain==result)
		 {
			 cout<<"right"<<endl;
			  Outf<<"ritht"<<std::endl;
		 }
		 else
		 {
			 cout<<"BUG!!!!!!!!!!1->remain"<<endl;
			 Outf<<"BUG!!!!!!!!!!1->remain"<<endl;
		 }

	}
}

//Test function RabbinMillerTest 
void RabbinCorrTest()
{
	ofstream outf("RabbinMiller.txt");
	
    int prime[] = {113,   
	  193,   241,   257,   337,   353,   401,   433,   449,   577,   593,   641,   
	  673,   769,   881,   929,   977,   1009,   1153,   1201,   1217,   1249,   
	  1297,   1361,   1409,   1489,   1553,   1601,   1697,   1777,   1873,   
	  1889,   2017,   2081,   2113,   2129,   2161,   2273,   2417,   2593,   
	  2609,   2657,   2689,   2753,   2801,   2833,   2897,   3041,   3089,   
	  3121,   3137,   3169,   3217,   3313,   3329,   3361,   3457,   3617,   
	  3697,   3761,   3793,   3889,   4001,   4049,   4129,   4177,   4241,   
	  4273,   4289,   4337,   4481,   4513,   4561,   4657,   4673,   4721,   
	  4801,   4817,   4993,   5009,   5153,   5233,   5281,   5297,   5393,   
	  5441,   5521,   5569,   5857,   5953,   6113,   6257,   6337,   6353,   
	  6449,   6481,   6529,   6577,   6673,   6689,   6737,   6833,   6961,   
	  6977,   7057,   7121,   7297,   7393,   7457,   7489,   7537,   7649,   
	  7681,   7793,   7841,   7873,   7937,   8017,   8081,   8161,   8209,   
	  8273,   8353,   8369,   8513,   8609,   8641,   8689,   8737,   8753,   
	  8849,   8929,   9041,   9137,   9281,   9377,   9473,   9521,   9601,   
	  9649,   9697,   9857   
	  };  //the prime list;

	for(int i=0;i<=140;i++)
	{
       mathInt p = prime[i];
	   if(RabbinMillerTest(p))
	   {
		   cout<<"OK"<<endl;
	      p.display ();
	   }
	   else
	   {
		   cout<<"BUG!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
		   outf<<prime[i];
	   }
	}
	cout<<endl;
  
}

//Test encrypt and decode a mathInt
void EncryptTest()
{
	GenerateKey();
	ifstream PubBase;
    vector<int> v(VSIZE);
	int i=0;
	bool sign;
	int length;
    
	PubBase.open("public_base.txt");//如果open失败刚cerr<<!!!!!待补充完整;
	PubBase>>sign;
	PubBase>>length;
	while(!PubBase.eof()&&i<=length-1)//get the public base from a public txt file;
	{
		PubBase>>v[i];
		i++;
	}
	mathInt n(sign,v);

	mathInt m;
	m.set_coeff(1,2*PRI_LEN-2);//m是要加密的数;0<m<n;
	cout<<"m is:"<<endl;
	m.display();
	m.OutToFile("original_m.txt");

	mathInt c = Montgomery(m,3,n);//c is the encrypted code,we use e = 3 as the public key;
	cout<<"c is:"<<endl;
	c.display();
	c.OutToFile("encrypted_c.txt");

	ifstream PriKey;
	PriKey.open("private_key.txt");
	i = 0;
	v.resize(VSIZE,0);//所有的都置零v.resize(VSIZE,0);
	PriKey>>sign;
	PriKey>>length;
	while(!PriKey.eof()&&i<=length-1)
	{
		PriKey>>v[i];
		i++;
	}
	mathInt my_key(sign,v);//get private key;

	mathInt decoded_m = Montgomery(c,my_key,n);
	cout<<"decoded m is:"<<endl;
	decoded_m.display();
	decoded_m.OutToFile("decoded_m.txt");
   
}

⌨️ 快捷键说明

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