RSA算法实验报告.docx
- 文档编号:28365575
- 上传时间:2023-07-10
- 格式:DOCX
- 页数:25
- 大小:31.51KB
RSA算法实验报告.docx
《RSA算法实验报告.docx》由会员分享,可在线阅读,更多相关《RSA算法实验报告.docx(25页珍藏版)》请在冰豆网上搜索。
RSA算法实验报告
实验二非对称密码算法RSA
一、实验目的
通过实际编程了解非对称密码算法RSA勺加密和解密过程,加深对非对称密码算法的认识。
二、实验环境
运行Windows或Linux操作系统的PC机,具有版本的Java语言编译环境。
三、实验内容和步骤
1、对RSA算法的理解
RSA算法(公开密钥算法)的原理:
(1)•选择两个大的素数p和q(典型情况下为1024位)
(2).计算n=p*q和z=(p-1)*(q-1).
(3).选择一个与z互素的数,将它称为d
⑷.找到e,使其满足e*d=1modz
提前计算出这些参数以后,我们就可以开始执行加密了。
首先将明文分成块,使得每个明文消息P落在间隔0*P 为了做到这一点,只要将明文划分成k 位的块即可,这里k是满足2Ak 为了加密一个消息P,只要计算C=PAe(modn)即可。 为了解密C,只要计算P=CAd(modn)即可。 可以证明,对于指定范围内的所有P,加密盒解密互为反函数。 为了执行加密,你需要e和n;为了执行解密,你需要d和n。 因此,公钥是有(e,n)对组成,而私钥是有(d,n)对组成。 实例: 根据已知参数: p=3,q=11,M=2计算公私钥,并对明文进行加密,然后对密文进行解密。 由题意知: n=p*q=33,z=(p-1)*(q-1)=20,选d=7, 计算得e=3,所以 C=MAe(modn)=8 M=CAd(modn)=2 2、RSA算法与DES算法的比较: 运行附件的RSATool,输入一大段文字,记录运行时间。 再使用DES算法加密相同的文字,记录运行时间,对比这两个时间发现,RSA算法比DES算法慢很多,因为RSA算法进行的是大数运算,所以程序运行的速度比DES慢很多。 因此RSA算法只适合于少量数据加密,不适合于大量数据加密。 3、算法设计 主要的方法: (1)、publicstaticvoidGetPrime() 方法名称: 产生大数的方法。 说明: 利用Java语言的中的类的方法中随机产生大数。 (2)、publicstaticbooleanMillerRobin(BigIntegernum)方法名称: 判断是否是素数的方法。 参数说明: num是由GetPrime方法产生的大数。 说明: 这个方法判断GetPrime方法传过来的是否是一个素数,是就返回true,否就返回false。 3)、publicstaticBigIntegerpowmod(BigIntegera,BigIntegert,BigIntegernum)方法名称: 大数的幂运算方法。 说明: 这个方法对传入的大数进行幂运算。 4)、publicstaticBigIntegerinvmod(BigIntegera,BigIntegerb)方法名称: 大数的取模运算方法。 说明: 这个方法对大数进行取模运算。 5)、publicstaticStringEncode(StringinStr,BigIntegerPrimeP,BigIntegerPrimeQ,BigIntegern,intnLen,intm,JTextFieldd)方法名称: 加密算法。 参数说明: inStr是从界面输入的明文。 PrimeP和PrimeQ是由GetPrime方法产生的两个大素数。 n是由PrimeP和PrimeQ得到的值。 nLen为n的长度。 d为公钥。 6)、publicstaticStringDecode(StringinStr,BigIntegerPrimeP,BigIntegerPrimeQ,BigIntegern,intnLen,intm,JTextFielde)方法名称: 解密算法。 参数说明: inStr是从界面输入的明文。 PrimeP和PrimeQ是由GetPrime方法产生的两个大素数。 n是由PrimeP和PrimeQ得到的值。 nLen为n的长度。 e为私钥。 4、源程序: (文件)import.*; importimport.*; import.*; import.*; import.*;publicclassRSA1{ publicstaticvoidmain(String[]args){ MyFrameframe=newMyFrame(); MyPanel_fbuttonpanel_fbutton=newMyPanel_fbutton(frame,,,,;FlowLayoutfl=newFlowLayout,0,0); (fl);(panel_fbutton); (150,100,500,480); J (true); } } classMyFrameextendsJFrame { publicMyFrame() {setTitle("RSA算法"); add(wel); MyPanel_ppanel_p=newMyPanel_p(P);add(panel_p); MyPanel_qpanel_q=newMyPanel_q(Q);add(panel_q); MyPanel_dpanel_d=newMyPanel_d(d);add(panel_d); MyPanel_epanel_e=newMyPanel_e(e);add(panel_e); MyPanel_inpanel_in=newMyPanel_in(input);add(panel_in); MyPanel_outpanel_out=newMyPanel_out(output);add(panel_out); MyPanel_out1panel_out1=newMyPanel_out1(output1);add(panel_out1); MyPanel_buttonpanel_button=newMyPanel_button(P,Q,d,e,input,output,output1);add(panel_button); } privateJLabelwel=newJLabel("RSA算法演示"); protectedJTextFieldP=newJTextField(35); protectedJTextFieldQ=newJTextField(35); protectedJTextFieldd=newJTextField(35); protectedJTextFielde=newJTextField(35); protectedJTextAreainput=newJTextArea(4,35); protectedJTextAreaoutput=newJTextArea(4,35); protectedJTextAreaoutput1=newJTextArea(4,35); } classMyPanel_fbuttonextendsJPanel { ad, publicMyPanel_fbutton(Frameaframe,JTextFieldaP,JTextFieldaQ,JTextFieldJTextFieldae) { frame=aframe; P=aP; Q=aQ; e=ae; d=ad; } privateFrameframe; privateJTextFieldP; privateJTextFieldQ; privateJTextFieldd; privateJTextFielde; } classMyPanel_pextendsJPanel { publicMyPanel_p(JTextFieldaP) { P=aP; add(newJLabel("质数P: ")); add(P); } privateJTextFieldP; } classMyPanel_qextendsJPanel { publicMyPanel_q(JTextFieldaQ) { Q=aQ; add(newJLabel("质数Q: ")); add(Q); } privateJTextFieldQ; } classMyPanel_dextendsJPanel publicMyPanel_d(JTextFieldad) d=ad; add(newJLabel(" 公钥: ")); add(d); } privateJTextFieldd; } classMyPanel_eextendsJPanel { publicMyPanel_e(JTextFieldae) { e=ae; add(newJLabel("私钥: ")); add(e); } privateJTextFielde; } classMyPanel_inextendsJPanel { publicMyPanel_in(JTextAreaainput) { input=ainput; add(newJLabel("输入明文: ")); JScrollPanejsp1=newJScrollPane(input,v,h);add(jsp1); } privateJTextAreainput; intv=; inth=; } classMyPanel_outextendsJPanel { publicMyPanel_out(JTextAreaaoutput) { output=aoutput; add(newJLabel("生成的密文: ")); JScrollPanejsp=newJScrollPane(output,v,h);add(jsp); } privateJTextAreaoutput; intv=; inth=; } classMyPanel_out1extendsJPanel publicMyPanel_out1(JTextAreaaoutput1) output1=aoutput1; add(newJLabel("解密后的明文: ")); JScrollPanejsp=newJScrollPane(output1,v,h);add(jsp); } privateJTextAreaoutput1; intv=; inth=; } classMyPanel_buttonextendsJPanel { publicMyPanel_button(JTextFieldaP,JTextFieldaQ,JTextFieldad,JTextFieldae,JTextAreaainput,JTextAreaaoutput,JTextAreaaoutput1){ P=aP; Q=aQ; e=ae; d=ad; input=ainput; output=aoutput;output1=aoutput1; (newRandListener(P,Q));add(randProduce); (newRandDListener(P,Q,d,e));add(randD); (newEncodeListener(P,Q,d,e,input,output));add(encode); (newDecodeListener(P,Q,d,e,output,output1));add(decode); } privateJTextFieldP; privateJTextFieldQ; privateJTextFieldd; privateJTextFielde; privateJTextAreainput; privateJTextAreaoutput; privateJTextAreaoutput1; privateJButtonrandProduce=newJButton("生成质数P和Q"); privateJButtonrandD=newJButton("生成公钥和私钥"); privateJButtonencode=newJButton("加密"); privateJButtondecode=newJButton("解密"); } classFileEncodeListenerimplementsActionListener { publicFileEncodeListener(Framef,JTextFieldap,JTextFieldaq,JTextFieldae,JTextFieldad){ P=ap; Q=aq; E=ae; D=ad; fr=f; } publicvoidactionPerformed(ActionEventee) { FileDialogfd=newFileDialog(fr); (true); StringinfileName=()+(); StringinStr=newString(); inStr=(infileName); BigIntegerPrimeP=newBigInteger()); BigIntegerPrimeQ=newBigInteger()); BigIntegern=(PrimeQ); intnLen=(); intm=(int)((double)(nLen)/); nLen=(nLen-1)/16; StringoutStr=newString(); outStr=(inStr,PrimeP,PrimeQ,n,nLen,m,D);for(i=()-1;i>=0;--i) { if(i)=='.')break; } StringoutfileName=(0,i); outfileName=outfileName+newString(".EncodeRsa")+(i,());(outfileName,outStr); } privateJTextFieldP; privateJTextFieldQ; privateJTextFieldE; privateJTextFieldD; privateFramefr; inti; } classFileDecodeListenerimplementsActionListener { publicFileDecodeListener(Framef,JTextFieldap,JTextFieldaq,JTextFieldae,JTextFieldad){ P=ap; Q=aq; E=ae;D=ad;fr=f; }publicvoidactionPerformed(ActionEventee) { FileDialogfd=newFileDialog(fr); (true); StringinfileName=()+(); StringinStr=newString();inStr=(infileName); BigIntegerPrimeP=newBigInteger()); BigIntegerPrimeQ=newBigInteger());BigIntegern=(PrimeQ);intnLen=(); intm=(int)((double)(nLen)/);nLen=(nLen-1)/16;StringoutStr=newString();outStr=(inStr,PrimeP,PrimeQ,n,nLen,m,E);for(i=()-1;i>=0;--i) {if(i)=='.')break; } StringoutfileName=(0,i);outfileName=outfileName+newString(".DecodeRsa")+(i,());(outfileName,outStr); } privateJTextFieldP; privateJTextFieldQ; privateJTextFieldE; privateJTextFieldD; privateFramefr; inti; } classRandListenerimplementsActionListener { publicRandListener(JTextFieldaP,JTextFieldaQ){ P=aP; Q=aQ; } publicvoidactionPerformed(ActionEvente){ (P);(Q); } privateJTextFieldP; privateJTextFieldQ; } classRandDListenerimplementsActionListener { publicRandDListener(JTextFieldaP,JTextFieldaQ,JTextFieldad,JTextFieldae){ P=aP; Q=aQ; d=ad; e=ae; } publicvoidactionPerformed(ActionEventee) { BigIntegerPP=newBigInteger()); BigIntegerQQ=newBigInteger()); BigIntegertemp=(newBigInteger("1"))).multiply(newBigInteger("1"))); BigIntegertemp1; do { temp1=newBigInteger(100,newRandom()).mod(temp); } while(temp1)==false); ()); (temp1,temp).toString()); } privateJTextFieldP; privateJTextFieldQ; privateJTextFieldd; privateJTextFielde; } classEncodeListenerimplementsActionListener { publicEncodeListener(JTextFieldaP,JTextFieldaQ,JTextFieldad,JTextFieldae,JTextAreain,JTextAreaout) P=aP; Q=aQ; d=ad; e=ae; input=in; output=out; } publicvoidactionPerformed(ActionEventee) { BigIntegerPrimeP=newBigInteger()); BigIntegerPrimeQ=newBigInteger()); BigIntegern=(PrimeQ); intnLen=(); intm=(int)((double)(nLen)/); nLen=(nLen-1)/16; StringinStr=();(inStr,PrimeP,PrimeQ,n,nLen,m,e)); } privateJTextFieldP; privateJTextFieldQ; privateJTextFieldd; privateJTextFielde; privateJTextAreainput; privateJTextAreaoutput; } classDecodeListenerimplementsActionListener { publicDecodeListener(JTextFieldaP,JTextFieldaQ,JTextFieldad,JTextFieldae,JTextAreaout,JTextAreaout1) { P=aP; Q=aQ; d=ad; e=ae; output=out;output1=out1; } publicvoidactionPerformed(ActionEventee) { BigIntegerPrimeP=newBigInteger()); BigIntegerPrimeQ=newBigInteger()); BigIntegern=(PrimeQ); intnLen=(); intm=(int)((double)(nLen)/); nLen=(nLen-1)/16; StringinStr=();(inStr,PrimeP,PrimeQ,n,nLen,m,d)); } privateJTextFieldP; privateJTextFieldQ; privateJTextFieldd; privateJTextFielde; privateJTextAreaoutput; privateJTextAreaoutput1; } classPublicMethod { publicstaticvoidGetPrime(JTextFieldprime) { BigIntegernum=newBigInteger("0"); Randomrand=newRandom(); do { intlength=(int)()*20+100); num=newBigInteger(length,5,rand); ()); }while(MillerRobin(num)==false); } publicstaticbooleanMillerRobin(BigIntegernum) { inttime=1000; BigIntegermod=(new
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- RSA 算法 实验 报告