ts.cpp

来自「RSA算法」· C++ 代码 · 共 54 行

CPP
54
字号
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <iostream.h>
#include "Bignum.cpp"

CBigNum GenerateBigRandomNumber(unsigned short nBytes)
{
   CBigNum Result=0U; // 初始化大数
   int i;
   clock_t ctStart;
   unsigned long ctr=0;
   // 设置时间间隔
   clock_t ctInterval = CLOCKS_PER_SEC / 50 + 1; 
   for (i=0; i<nBytes*2; i++)
   {
      ctStart = clock();
	  // 等到大于时间间隔再开始
      while (clock() - ctStart < ctInterval)
         ctr++;
      ctr = (ctr % 33) & 0xF;
      Result <<= 4U; // 大数左移4位
      Result |= ctr; // 做或运算
   }
   putchar('\n');
   return Result; // 返回大数
}

void main()
{
	int  nBytes=32;
	long   end,start=clock();
	double time;

	CBigNum x,e,n,c;
	x=GenerateBigRandomNumber( nBytes);// 生成随机大数
	e=GenerateBigRandomNumber( nBytes);
	n=GenerateBigRandomNumber( nBytes);
    
    c=x.PowMod(e,n);	//作x的e次方mod n的运算

	cout<<"x="<<x<<'\n'<<'\n';
	cout<<"e="<<e<<'\n'<<'\n';
	cout<<"n="<<n<<'\n'<<'\n';
	cout<<"c="<<c<<'\n'<<'\n'<<endl;

	end=clock(); //计时结束
    time=(double)(end-start)/CLOCKS_PER_SEC;
	printf("The time of this operation is:%.3f s",time);
	printf("\n");
}


⌨️ 快捷键说明

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