图 4.1 RSA 算法运行结果
(1)输入初始明文,八位及以上。
(2)输入素数 p、q,回车产生其公钥和私钥。
(3) 进入 RSA 算法交互页面,输入 1 加密,输出密文与花费时间;输入 2,输
出明文与花费时间。
(4)使用 RSA 和 DES 算法加密同一段明文,发现时间差距并不大,0.021 秒和
0.022 秒,经过反思,可能是因为数据太小,所以效果并不明显。可以将
程序从文件读入,输入数据量比较多的明文再查看结果。
附:源代码
#include
#include
#include
#include
#include
char s[100],*c;
int n,e,d,i,C,j,k=0,len;
int str[100],b[30];
unsigned gcd(unsigned a, unsigned b )
{
}
if(a%b==0)
return b;
else
return gcd(b,a%b);
void Egcd(int a, int b,int &x, int &y)
{
//ax-by=1
if(b==0||a==0)
{
x=1;
y=0;
return ;
}
if(a
}
}
//printf("e=%d\n",e);
Egcd(e,N,d,Y);
// printf("d=%d Y=%d\n",d,Y);
printf("公钥 PU={e=%d,n=%d}\n",e,n);
printf("私钥 PR={d=%d,n=%d}\n",d,n);
}
void encrypt()
{
//加密函数
len=strlen(s);
//hgprintf("len=%d\n",len);
for(i=0;i122)
{
b[k]=i;
k++;
for(j=i;j