当前位置:文档之家› RSA算法的实现实验报告

RSA算法的实现实验报告

RSA算法的实现实验报告
RSA算法的实现实验报告

RSA 算法的实现

一、实验目的

1. 熟悉公钥密码体制;

2. 掌握产生密钥对的程序设计方法;

3. 掌握产生加密/ 解密的程序设计方法。

二、实验内容和要求

1. 进行RSA加密/解密算法的设计;

2. 对RSA程序进行编译和调试;

3. 使用编写的程序进行加密和解密。

三、实验环境

运行Windows操作系统的PC机,可以利用具有VC+语言环境;如果所运用的语言是JAVA那么也可以利用JAVA语言环境来实现RSA算法的加密和解密。

四、实验步骤

1. 采用C++语言进行本次实验的编写,实验的代码如下:

#include

#include

int candp(int a,int b,int c)

{ int r=1;

b=b+1;

while(b!=1)

{

r=r*a;

r=r%c;

b--;

}

printf("%d\n",r);

return r;

}

void main()

{

int p,q,e,d,m,n,t,c,r;

char s;

printf("please input the p,q: "); scanf("%d%d",&p,&q);

n=p*q;

printf("the n is %3d\n",n);

t=(p-1)*(q-1);

printf("the t is %3d\n",t); printf("please input the e: "); scanf("%d",&e);

if(e<1||e>t)

{

printf("e is error,please input again: "); scanf("%d",&e);

}

d=1;

while(((e*d)%t)!=1) d++;

printf("then caculate out that the d is %d\n", printf("the cipher please input 1\n"); printf("the plain please input 2\n"); scanf("%d",&r);

switch(r)

{

case 1: printf("input the m: "); /*

scanf("%d",&m); c=candp(m,e,n);

printf("the cipher is %d\n",c);break; case 2: printf("input the c: "); /*

scanf("%d",&c); 输入要加密的明文数字*/输入要解密的密文数字*/

m=ca ndp(c,d ,n);

prin tf("the cipher is %d\n ",m);break;

getch();

}

2、代码的思想:首先随意输入两个素数p和q,然后利用算法计算出p*q 即n,再算出(p-1)*(q-1)即t,并且同时输出计算的结果n和t,接下来输入e, 经过算法可以计算出d,由此可以知道RSA算法的公钥和私钥;接下来可以有两个选择:一选择输入明文,有明文经过算法可以计算出密文;二输入密文,有密

文经过算法可以计算出明文。

3、运行以上代码就可以得到实验的结果。

五、实验结果

实验结果如下图所示:

C; XProgriaM FilesXVlcrosoft Visual Studlo\]lyProjec-ts\sliiyan.lXIi|ebiiG:\sti? .?pleat;e Input; tlie p# 电上U 31

t;ha n is 403

L]|j& t is:3&0

pled古甘丄mpuit tlie e - 7

Chen cacuilate ouit that the d Is 103

the cipher please input 1

the plain please input 2

2

input the c: 8795935

-156

the £ iphei* is —156

六、实验心得:

通过这次的实验,了解了非对称密码算法RSA会运用一些现成的算法

进行编程,对一些比较复杂的算法开始基本认识并深刻的掌握。在以后所涉及这方面的知识将会有全新的了解和掌握。

实验报告

姓名:刘新平

业:

互联网

级:10-03 班

号:

541012010313

相关主题
文本预览
相关文档 最新文档