




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上实验二 非对称密码算法RSA一、实验目的通过实际编程了解非对称密码算法RSA的加密和解密过程,加深对非对称密码算法的认识。二、实验环境运行Windows或Linux操作系统的PC机,具有JDK1.6版本的Java语言编译环境。三、实验内容和步骤1. 对RSA算法的理解RSA算法(公开密钥算法)的原理:(1) 选择两个大的素数p和q(典型情况下为1024位)(2) 计算n = p * q 和 z =(p-1)*(q-1).(3) 选择一个与z互素的数,将它称为d(4) 找到e,使其满足e*d = 1 mod z提前计算出这些参数以后,我们就可以开始执行加密了。首先将明文
2、分成块,使得每个明文消息P落在间隔0*P<n中。为了做到这一点,只要将明文划分成k位的块即可,这里k是满足2k<n的最大整数。为了加密一个消息P,只要计算C=Pe(mod n) 即可。为了解密C,只要计算P=Cd(mod n)即可。可以证明,对于指定范围内的所有P,加密盒解密互为反函数。为了执行加密,你需要e和n;为了执行解密,你需要d和n。因此,公钥是有(e,n)对组成,而私钥是有(d,n)对组成。实例:根据已知参数:p=3,q=11,M=2,计算公私钥,并对明文进行加密,然后对密文进行解密。由题意知:n = p * q33,z =(p-1)*(q-1)20,选d7,计算得e=3
3、,所以C=Me(mod n)8M=Cd(mod n)22、RSA算法与DES算法的比较:运行附件的RSATool,输入一大段文字,记录运行时间。再使用DES算法加密相同的文字,记录运行时间,对比这两个时间发现,RSA算法比DES算法慢很多,因为RSA算法进行的是大数运算,所以程序运行的速度比DES慢很多。因此RSA算法只适合于少量数据加密,不适合于大量数据加密。3、算法设计主要的方法:(1)、public static void GetPrime()方法名称:产生大数的方法。说明:利用Java语言的中的java.math.BigInteger类的方法中随机产生大数。(2)、public sta
4、tic boolean MillerRobin(BigInteger num)方法名称:判断是否是素数的方法。参数说明:num是由GetPrime方法产生的大数。说明:这个方法判断GetPrime方法传过来的是否是一个素数,是就返回true,否就返回false。(3)、public static BigInteger powmod( BigInteger a, BigInteger t, BigInteger num )方法名称:大数的幂运算方法。说明:这个方法对传入的大数进行幂运算。(4)、public static BigInteger invmod(BigInteger a, BigIn
5、teger b)方法名称:大数的取模运算方法。说明:这个方法对大数进行取模运算。(5)、public static String Encode(String inStr,BigInteger PrimeP,BigInteger PrimeQ, BigInteger n,int nLen,int m,JTextField d)方法名称:加密算法。参数说明:inStr是从界面输入的明文。PrimeP和PrimeQ是由GetPrime方法产生的两个大素数。n是由PrimeP和PrimeQ得到的值。nLen为n的长度。d为公钥。(6)、public static String Decode(Strin
6、g inStr,BigInteger PrimeP,BigInteger PrimeQ, BigInteger n,int nLen,int m,JTextField e)方法名称:解密算法。参数说明:inStr是从界面输入的明文。PrimeP和PrimeQ是由GetPrime方法产生的两个大素数。n是由PrimeP和PrimeQ得到的值。nLen为n的长度。e为私钥。4、源程序:(RSA1.java文件)import javax.swing.*; import java.awt.event.*; import java.math.*; import java.util.*; import j
7、ava.awt.*; import java.io.*; public class RSA1 public static void main(String args) MyFrame frame = new MyFrame(); MyPanel_fbutton panel_fbutton = new MyPanel_fbutton(frame,frame.P,frame.Q,frame.d,frame.e); FlowLayout fl = new FlowLayout (FlowLayout.CENTER,0,0); frame.setLayout(fl); frame.add(panel_
8、fbutton); frame.setBounds( 150, 100, 500, 480 ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); class MyFrame extends JFrame public MyFrame() setTitle("RSA算法"); add(wel); MyPanel_p panel_p = new MyPanel_p(P); add(panel_p); MyPanel_q panel_q = new MyPanel_q(Q)
9、; add(panel_q); MyPanel_d panel_d = new MyPanel_d(d); add(panel_d); MyPanel_e panel_e = new MyPanel_e(e); add(panel_e); MyPanel_in panel_in = new MyPanel_in(input); add(panel_in); MyPanel_out panel_out = new MyPanel_out(output); add(panel_out); MyPanel_out1 panel_out1 = new MyPanel_out1(output1); ad
10、d(panel_out1); MyPanel_button panel_button = new MyPanel_button(P,Q,d,e,input,output,output1); add(panel_button); private JLabel wel = new JLabel(" RSA算法演示 "); protected JTextField P = new JTextField(35); protected JTextField Q = new JTextField(35); protected JTextField d = new JTextField(
11、35); protected JTextField e = new JTextField(35); protected JTextArea input = new JTextArea(4,35); protected JTextArea output = new JTextArea(4,35); protected JTextArea output1 = new JTextArea(4,35); class MyPanel_fbutton extends JPanel public MyPanel_fbutton(Frame aframe,JTextField aP, JTextField a
12、Q, JTextField ad, JTextField ae) frame = aframe; P = aP; Q = aQ; e = ae; d = ad; private Frame frame; private JTextField P; private JTextField Q; private JTextField d; private JTextField e; class MyPanel_p extends JPanel public MyPanel_p(JTextField aP) P=aP; add(new JLabel(" 质数 P:"); add(P
13、); private JTextField P; class MyPanel_q extends JPanel public MyPanel_q(JTextField aQ) Q=aQ; add(new JLabel(" 质数 Q:"); add(Q); private JTextField Q; class MyPanel_d extends JPanel public MyPanel_d(JTextField ad) d=ad; add(new JLabel(" 公 钥:"); add(d); private JTextField d; class
14、MyPanel_e extends JPanel public MyPanel_e(JTextField ae) e=ae; add(new JLabel(" 私 钥:"); add(e); private JTextField e; class MyPanel_in extends JPanel public MyPanel_in(JTextArea ainput) input = ainput; add(new JLabel(" 输入明文:"); JScrollPane jsp1 = new JScrollPane (input,v,h); add(
15、jsp1); private JTextArea input; int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED; int h=JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED; class MyPanel_out extends JPanel public MyPanel_out(JTextArea aoutput) output = aoutput; add(new JLabel(" 生成的密文:"); JScrollPane jsp = new JScrollPane (output,v,
16、h); add(jsp); private JTextArea output; int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED; int h=JScrollPane.HORIZONTAL_SCROLLBAR_NEVER; class MyPanel_out1 extends JPanel public MyPanel_out1(JTextArea aoutput1) output1 = aoutput1; add(new JLabel("解密后的明文:"); JScrollPane jsp = new JScrollPane (
17、output1,v,h); add(jsp); private JTextArea output1; int v=JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED; int h=JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED; class MyPanel_button extends JPanel public MyPanel_button(JTextField aP,JTextField aQ,JTextField ad,JTextField ae, JTextArea ainput,JTextArea aoutput,J
18、TextArea aoutput1) P = aP; Q = aQ; e = ae; d = ad; input = ainput ; output = aoutput ; output1 = aoutput1 ; randProduce.addActionListener(new RandListener( P, Q ); add(randProduce); randD.addActionListener(new RandDListener( P, Q, d, e ); add(randD); encode.addActionListener(new EncodeListener( P, Q
19、, d, e, input, output ); add(encode); decode.addActionListener(new DecodeListener( P, Q, d, e, output, output1 ); add(decode); private JTextField P; private JTextField Q; private JTextField d; private JTextField e; private JTextArea input; private JTextArea output; private JTextArea output1; private
20、 JButton randProduce = new JButton("生成质数P和Q"); private JButton randD = new JButton("生成公钥和私钥"); private JButton encode = new JButton("加密"); private JButton decode = new JButton("解密"); class FileEncodeListener implements ActionListener public FileEncodeListener(
21、Frame f,JTextField ap, JTextField aq,JTextField ae,JTextField ad) P = ap; Q = aq; E = ae; D = ad; fr = f; public void actionPerformed(ActionEvent ee) FileDialog fd = new FileDialog(fr); fd.setVisible(true); String infileName =fd.getDirectory()+fd.getFile(); String inStr = new String(); inStr = Publi
22、cMethod.read(infileName); BigInteger PrimeP = new BigInteger(P.getText(); BigInteger PrimeQ = new BigInteger(Q.getText(); BigInteger n =PrimeP.multiply(PrimeQ); int nLen = n.bitLength(); int m=(int)(Math.ceil(double)(nLen)/16.0); nLen = (nLen-1) / 16; String outStr = new String(); outStr = PublicMet
23、hod.Encode(inStr,PrimeP,PrimeQ,n,nLen,m,D); for(i=infileName.length()-1;i>=0;-i) if(infileName.charAt(i)='.') break; String outfileName = infileName.substring(0,i); outfileName = outfileName + new String(".EncodeRsa") + infileName.substring(i,infileName.length(); PublicMethod.ou
24、tput(outfileName,outStr); private JTextField P; private JTextField Q; private JTextField E; private JTextField D; private Frame fr; int i; class FileDecodeListener implements ActionListener public FileDecodeListener(Frame f,JTextField ap, JTextField aq,JTextField ae,JTextField ad) P = ap; Q = aq; E
25、= ae; D = ad; fr = f; public void actionPerformed(ActionEvent ee) FileDialog fd = new FileDialog(fr); fd.setVisible(true); String infileName =fd.getDirectory()+fd.getFile(); String inStr = new String(); inStr = PublicMethod.input(infileName); System.out.println(inStr); BigInteger PrimeP = new BigInt
26、eger(P.getText(); BigInteger PrimeQ = new BigInteger(Q.getText(); BigInteger n =PrimeP.multiply(PrimeQ); int nLen = n.bitLength(); int m=(int)(Math.ceil(double)(nLen)/16.0); nLen = (nLen-1) / 16; String outStr = new String(); outStr = PublicMethod.Decode(inStr,PrimeP,PrimeQ,n,nLen,m,E); for(i=infile
27、Name.length()-1;i>=0;-i) if(infileName.charAt(i)='.') break; String outfileName = infileName.substring(0,i); outfileName = outfileName + new String(".DecodeRsa") + infileName.substring(i,infileName.length(); PublicMethod.write(outfileName,outStr); private JTextField P; private J
28、TextField Q; private JTextField E; private JTextField D; private Frame fr; int i; class RandListener implements ActionListener public RandListener(JTextField aP, JTextField aQ) P = aP; Q = aQ; public void actionPerformed(ActionEvent e) PublicMethod.GetPrime( P ); PublicMethod.GetPrime( Q ); private
29、JTextField P; private JTextField Q; class RandDListener implements ActionListener public RandDListener(JTextField aP, JTextField aQ, JTextField ad, JTextField ae) P = aP; Q = aQ; d = ad; e = ae; public void actionPerformed(ActionEvent ee) BigInteger PP = new BigInteger(P.getText(); BigInteger QQ = n
30、ew BigInteger(Q.getText(); BigInteger temp = (PP.subtract(new BigInteger("1").multiply(QQ.subtract(new BigInteger("1"); BigInteger temp1; do temp1=new BigInteger(100, new Random().mod(temp); while(PublicMethod.MillerRobin(temp1)=false); d.setText(temp1.toString(); e.setText(Publi
31、cMethod.invmod(temp1, temp).toString(); private JTextField P; private JTextField Q; private JTextField d; private JTextField e; class EncodeListener implements ActionListener public EncodeListener(JTextField aP, JTextField aQ, JTextField ad, JTextField ae, JTextArea in, JTextArea out) P = aP; Q = aQ
32、; d = ad; e = ae; input = in; output = out; public void actionPerformed(ActionEvent ee) BigInteger PrimeP = new BigInteger(P.getText(); BigInteger PrimeQ = new BigInteger(Q.getText(); BigInteger n =PrimeP.multiply(PrimeQ); int nLen = n.bitLength(); int m=(int)(Math.ceil(double)(nLen)/16.0); nLen = (
33、nLen-1) / 16; String inStr = input.getText(); output.setText(PublicMethod.Encode(inStr,PrimeP,PrimeQ,n,nLen,m,e); private JTextField P; private JTextField Q; private JTextField d; private JTextField e; private JTextArea input; private JTextArea output; class DecodeListener implements ActionListener
34、public DecodeListener(JTextField aP, JTextField aQ, JTextField ad, JTextField ae, JTextArea out, JTextArea out1) P = aP; Q = aQ; d = ad; e = ae; output = out; output1 = out1; public void actionPerformed(ActionEvent ee) BigInteger PrimeP = new BigInteger(P.getText(); BigInteger PrimeQ = new BigIntege
35、r(Q.getText(); BigInteger n =PrimeP.multiply(PrimeQ); int nLen = n.bitLength(); int m=(int)(Math.ceil(double)(nLen)/16.0); nLen = (nLen-1) / 16; String inStr = output.getText(); output1.setText(PublicMethod.Decode(inStr,PrimeP,PrimeQ,n,nLen,m,d); private JTextField P; private JTextField Q; private J
36、TextField d; private JTextField e; private JTextArea output; private JTextArea output1; class PublicMethod public static void GetPrime( JTextField prime ) BigInteger num = new BigInteger("0"); Random rand = new Random(); do int length = (int)(Math.random()*20+100); System.out.println(lengt
37、h); num = new BigInteger( length, 5 , rand ); prime.setText(num.toString(); while(MillerRobin(num)=false); public static boolean MillerRobin(BigInteger num) int time = 1000; BigInteger mod = num.mod(new BigInteger("2"); if(mod.equals(new BigInteger("0") return false; int s = 0, j
38、=0; BigInteger t=num.subtract(new BigInteger("1"); while( t.mod(new BigInteger("2").equals("0") ) t.divide(new BigInteger("2"); +s; for(int i=0; i<time; +i) BigInteger a = new BigInteger(100, new Random().mod(num.subtract(new BigInteger("3").add(n
39、ew BigInteger("2"); BigInteger y = powmod(a, t, num); if(y.equals(new BigInteger("1")=false && y.equals(num.subtract(new BigInteger("1")=false) j=1; while(j=s&&y.equals(num.subtract(new BigInteger("1")=false) y = y.multiply(y).mod(num); if(y.eq
40、uals(new BigInteger("1") return false; +j; if(y.equals(num.subtract(new BigInteger("1")=false) return false; return true; public static BigInteger powmod( BigInteger a, BigInteger t, BigInteger num ) BigInteger A = new BigInteger("1"); while(t.equals(new BigInteger(&quo
41、t;0")=false) if(t.mod(new BigInteger("2").equals(new BigInteger("1") A = A.multiply(a).mod(num); a = a.multiply(a).mod(num); t=t.divide(new BigInteger("2"); return A; public static BigInteger invmod(BigInteger a, BigInteger b) System.out.println(a+" "+b);
42、 BigInteger s0=new BigInteger("1"), s1=new BigInteger("0"), s2, q, t, b0=b; while(b.equals(new BigInteger("0")=false) q=a.divide(b); s2=s0.subtract( q.multiply(s1) ); if(pareTo(new BigInteger("0")!=-1) s2=s2.mod(b0); else s2=b0.subtract( s2.multiply(new BigInte
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 四年级数学上册五倍数和因数5.4.1认识因数质数合数教学设计冀教版
- 建设芳香腈新材料及氯氰基苯技改转型升级项目可行性研究报告模板-立项备案
- 自动拉模压瓦机企业ESG实践与创新战略研究报告
- 中型车企业ESG实践与创新战略研究报告
- 内燃机及配件企业数字化转型与智慧升级战略研究报告
- 碳纳米管企业数字化转型与智慧升级战略研究报告
- 空调、冰箱驱动控制器企业ESG实践与创新战略研究报告
- 电力铜母线企业数字化转型与智慧升级战略研究报告
- 透明质酸(玻尿酸)企业数字化转型与智慧升级战略研究报告
- 2025年涂镀添加剂合作协议书
- 基于多源异构数据的地质知识图谱构建与应用
- 2024年战略规划咨询服务合同
- 2024年领导干部任前廉政知识考试测试题库及答案
- 《公共资源交易主体信用评价实施指南》编制说明
- 2024年度山东省国家电网招聘之管理类测试卷(含答案)
- 煤矿防突专项设计(样本)
- 绍兴银行社会招聘真题
- 《储水式电热水器的安全使用年限》
- DB1303T375-2024起重机械使用管理制度编制指南
- 路灯安装工程项目实施重点、难点和解决方案
- 山西省云时代技术有限公司笔试题库
评论
0/150
提交评论