验四:实现公钥密码算法RSA

RSA公钥加密算法是1977年由Ron RivestAdi ShamirhLenAdleman在(美国麻省理工学院)开发的。RSA取名来自开发他们三者的名字。RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准。RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。

 

一、实验内容

掌进一步掌握大素数分解的一般原理和实现方法。

 

二、实现公钥密码算法RSA的基本原理

2.1 实实现公钥密码算法RSA概述

RSA公钥加密算法是1977年由Ron RivestAdi ShamirhLenAdleman在(美国麻省理工学院)开发的。RSA取名来自开发他们三者的名字。RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准。RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。

RSA公开密钥密码体制。所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制。

    在公开密钥密码体制中,加密密钥(即公开密钥)PK是公开信息,而解密密钥(即秘密密钥)SK是需要保密的。加密算法E和解密算法D也都是公开的。虽然秘密密钥SK是由公开密钥PK决定的,但却不能根据PK计算出SK。正是基于这种理论,1978年出现了著名的RSA算法,它通常是先生成一对RSA 密钥,其中之一是保密密钥,由用户保存;另一个为公开密钥,可对外公开,甚至可在网络服务器中注册。为提高保密强度,RSA密钥至少为500位长,一般推荐使用1024位。这就使加密的计算量很大。为减少计算量,在传送信息时,常采用传统加密方法与公开密钥加密方法相结合的方式,即信息采用改进的DESIDEA对话密钥加密,然后使用RSA密钥加密对话密钥和信息摘要。对方收到信息后,用不同的密钥解密并可核对信息摘要。

  RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。RSA是被研究得最广泛的公钥算法,从提出到现在的三十多年里,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一。

RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密。

  RSA的算法涉及三个参数,ne1e2

  其中,n是两个大质数pq的积,n的二进制表示时所占用的位数,就是所谓的密钥长度。

  e1e2是一对相关的值,e1可以任意取,但要求e1(p-1)*(q-1)互质;再选择e2,要求(e2*e1)mod((p-1)*(q-1))=1

  (ne1),(ne2)就是密钥对。

  RSA加解密的算法完全相同,A为明文,B为密文,则:A=B^e1 mod nB=A^e2 mod n

  e1e2可以互换使用,即:

A=B^e2 mod nB=A^e1 mod n;

2.2 实现公钥密码算法RSA算法流程

 

三、实验结果

 

四、实验小结

本次实验难度较大。开始的时候,我用的是前面的大素数生成算法来加密和解密的,但是在使用过程中我发现,加密和加密的速度比较慢,不仅如此,还只能加密数字。所以我使用的是Jav API提供的公钥和密钥。我为了增加安全性,我给出的公钥个密钥每一次都是不断变化的。

 

源代码:

图形界面源代码:

 

 
  
  1. import java.awt.*; 
  2. import javax.swing.*; 
  3. import java.awt.event.*; 
  4. import java.io.*; 
  5. import java.util.*; 
  6. import java.security.KeyPair; 
  7. import java.security.KeyPairGenerator; 
  8. import java.security.interfaces.RSAPrivateKey; 
  9. import java.security.interfaces.RSAPublicKey; 
  10. import javax.crypto.Cipher; 
  11. public class RSA0414 extends WindowAdapter implements ActionListener,ItemListener{ 
  12.             String puk=null;//公钥 
  13.             String bts=null;//加密后的数据 
  14.             String prk=null;//私钥 
  15.             String btt=null;//解密后的数据 
  16.             Label l1=new Label("输入:"),l2=new Label("加密:"),l3=new Label("解密:"),l4=new Label("私钥:"),l5=new Label("公钥:");     
  17.             JFrame f; 
  18.             TextField t1=new TextField(60),t2=new TextField(60),t3=new TextField(60); 
  19.             TextArea t4=new TextArea(" ",5,60,TextArea.SCROLLBARS_BOTH),t5=new TextArea(" ",5,60,TextArea.SCROLLBARS_BOTH); 
  20.             Button b1=new Button("加密"),b2=new Button("解密"),b3=new Button("清屏"); 
  21.             Choice c1; 
  22.             Panel p1,p2,p3,p4,p5,p6,p7,p8,p9; 
  23.             public void display(){ 
  24.             f=new JFrame("中国矿业大学密码学课程设计 学号:08093739 欧二强 "); 
  25.             f.setSize(540,400); 
  26.             f.setLocation(200,140); 
  27.             f.setBackground(Color.lightGray); 
  28.             f.setLayout(new BorderLayout()); 
  29.             tiajiazujian(); 
  30.             f.setVisible(true); 
  31.     } 
  32. /*添加界面组件以及监听器*/ 
  33.     public void tiajiazujian(){              
  34.             c1=new Choice(); 
  35.             c1.add("实现公钥密码算法RSA"); 
  36.             c1.addItemListener(this); 
  37.             f.add(c1,"North"); 
  38.             p1=new Panel(); 
  39.             p3=new Panel(); 
  40.             f.add(p3,"Center"); 
  41.             p3.setLayout(new FlowLayout()); 
  42.             p3.add(l1);p3.add(t1);p3.add(l2);p3.add(t2);p3.add(l3);p3.add(t3); 
  43.             p2=new Panel(); 
  44.             f.add(p2,"South"); 
  45.             //p2.setLayout(new FlowLayout());//GridLayout(2,2)); 
  46.             p4=new Panel(); 
  47.             //f.add(p4,"Center"); 
  48.             p3.add(l4);p3.add(t4);p2.add(b1);p2.add(b2);p2.add(b3);p3.add(l5);p3.add(t5); 
  49.             b1.addActionListener(this); 
  50.             b2.addActionListener(this); 
  51.             b3.addActionListener(this); 
  52.     } 
  53. /*对应不同加密算法的按钮点击事件*/ 
  54.     public void actionPerformed(ActionEvent e){                      
  55.             if (e.getSource()==b3&&c1.getSelectedIndex()==0){ 
  56.             t1.setText(" ");t2.setText(" ");t3.setText(" "); 
  57.             } 
  58.             if (e.getSource()==b1&&c1.getSelectedIndex()==0) 
  59.             { 
  60.                 //ywjiami(); 
  61.                 rsaJiami(); 
  62.             } 
  63.              
  64.             if(e.getSource()==b2&&c1.getSelectedIndex()==0){ 
  65.                 //ywjiemi(); 
  66.                 rsaJiemi(); 
  67.             } 
  68.              
  69.      
  70.     } 
  71.     public void itemStateChanged(ItemEvent e){ 
  72.      
  73.     } 
  74.  
  75. /*实现公钥密码算法RSA**/ 
  76. //RSA加密算法 
  77.     public void rsaJiami(){ 
  78.         try { 
  79.             RSAEncrypt encrypt = new RSAEncrypt(); 
  80.             //Scanner read =new Scanner(System.in); 
  81.             //System.out.println("请输入您要加密的数据:"); 
  82.             String s1; 
  83.             String s2; 
  84.  
  85.             s1=t1.getText();//输入解密数据 
  86.  
  87.             String encryptText = s1
  88.             KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); 
  89.             keyPairGen.initialize(1024); 
  90.             KeyPair keyPair = keyPairGen.generateKeyPair(); 
  91.              
  92.             RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); 
  93.             RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); 
  94.             //System.out.println("请输入您要加密的数据:"); 
  95.             byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes()); 
  96.             byte[] de = encrypt.decrypt(privateKey,e); 
  97.             //System.out.println("加密的数据:"); 
  98.             //System.out.println(encrypt.bytesToString(e)); 
  99.             //System.out.println("解密的数据:"); 
  100.              
  101.             //System.out.println(encrypt.bytesToString(de)); 
  102.             //System.out.println("公钥:"+publicKey); 
  103.             //System.out.println("密钥:"+privateKey); 
  104.             //puk=publicKey.toString();//公钥 
  105.             bts=encrypt.bytesToString(e);//加密后的数据 
  106.             //prk=privateKey.toString();//私钥 
  107.             //btt=encrypt.bytesToString(de);//解密后的数据 
  108.             t2.setText(bts);//加密结果 
  109.             t3.setText(btt);//解密结果 
  110.             //t4.replaceRange(prk,0,10); 
  111.             //t5.replaceRange(puk,0,10); 
  112.             } catch (Exception e) { 
  113.             e.printStackTrace(); 
  114.             } 
  115.     } 
  116.  
  117.     //RSA解密算法 
  118.     public void rsaJiemi(){ 
  119.         try { 
  120.             RSAEncrypt encrypt = new RSAEncrypt(); 
  121.             //Scanner read =new Scanner(System.in); 
  122.             //System.out.println("请输入您要加密的数据:"); 
  123.             String s1; 
  124.             String s2; 
  125.  
  126.             s1=t1.getText();//输入解密数据 
  127.  
  128.             String encryptText = s1
  129.             KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); 
  130.             keyPairGen.initialize(1024); 
  131.             KeyPair keyPair = keyPairGen.generateKeyPair(); 
  132.              
  133.             RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); 
  134.             RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); 
  135.             //System.out.println("请输入您要加密的数据:"); 
  136.             byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes()); 
  137.             byte[] de = encrypt.decrypt(privateKey,e); 
  138.             //System.out.println("加密的数据:"); 
  139.             //System.out.println(encrypt.bytesToString(e)); 
  140.             //System.out.println("解密的数据:"); 
  141.              
  142.             //System.out.println(encrypt.bytesToString(de)); 
  143.             //System.out.println("公钥:"+publicKey); 
  144.             //System.out.println("密钥:"+privateKey); 
  145.             puk=publicKey.toString();//公钥 
  146.             //bts=encrypt.bytesToString(e);//加密后的数据 
  147.             prk=privateKey.toString();//私钥 
  148.             btt=encrypt.bytesToString(de);//解密后的数据 
  149.             //t2.setText(bts);//加密结果 
  150.             t3.setText(btt);//解密结果 
  151.             t4.replaceRange(prk,0,10); 
  152.             t5.replaceRange(puk,0,10); 
  153.             } catch (Exception e) { 
  154.             e.printStackTrace(); 
  155.             } 
  156.     } 
  157. //主方法 
  158. public static void main(String arg[]) 
  159.     { 
  160.     RSA0414 ob=new RSA0414(); 
  161.     ob.display(); 
  162.     } 
  163. }  



RSA算法代码:

 

 
  
  1. import java.util.*; 
  2. import java.security.KeyPair; 
  3. import java.security.KeyPairGenerator; 
  4. import java.security.interfaces.RSAPrivateKey; 
  5. import java.security.interfaces.RSAPublicKey; 
  6. import javax.crypto.Cipher; 
  7.  
  8. /** *//** 
  9. * RSAEncrypt 
  10. *  
  11.  
  12.  
  13. * @author maqujun 
  14. * @see 
  15. */ 
  16. public class RSAEncrypt { 
  17.  
  18. /** *//** 
  19. * Main method for RSAEncrypt. 
  20. * @param args 
  21. */ 
  22. public static void main(String[] args) { 
  23.         try { 
  24.             RSAEncrypt encrypt = new RSAEncrypt(); 
  25.             Scanner read =new Scanner(System.in); 
  26.             System.out.println("请输入您要加密的数据:"); 
  27.             String encryptText = read.next(); 
  28.             KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); 
  29.             keyPairGen.initialize(1024); 
  30.             KeyPair keyPair = keyPairGen.generateKeyPair(); 
  31.              
  32.             RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); 
  33.             RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); 
  34.             //System.out.println("请输入您要加密的数据:"); 
  35.             byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes()); 
  36.             byte[] de = encrypt.decrypt(privateKey,e); 
  37.             System.out.println("加密的数据:"); 
  38.             System.out.println(encrypt.bytesToString(e)); 
  39.             System.out.println("解密的数据:"); 
  40.              
  41.             System.out.println(encrypt.bytesToString(de)); 
  42.             System.out.println("公钥:"+publicKey); 
  43.             System.out.println("密钥:"+privateKey); 
  44.             } catch (Exception e) { 
  45.             e.printStackTrace(); 
  46.             } 
  47.          
  48. }    
  49.  
  50. /** *//** 
  51. * Change byte array to String. 
  52. * @return byte[] 
  53. */ 
  54. protected String bytesToString(byte[] encrytpByte) { 
  55. String result = ""
  56. for (Byte bytes : encrytpByte) { 
  57. result += (char) bytes.intValue(); 
  58. return result; 
  59.  
  60. /** *//** 
  61. * Encrypt String. 
  62. * @return byte[] 
  63. */ 
  64. protected byte[] encrypt(RSAPublicKey publicKey, byte[] obj)  { 
  65. if (publicKey != null) { 
  66. try { 
  67. Cipher cipher = Cipher.getInstance("RSA"); 
  68. cipher.init(Cipher.ENCRYPT_MODE, publicKey); 
  69. return cipher.doFinal(obj); 
  70. } catch (Exception e) { 
  71. e.printStackTrace(); 
  72. return null; 
  73.  
  74. /** *//** 
  75. * Basic decrypt method 
  76. * @return byte[] 
  77. */ 
  78. protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) { 
  79. if (privateKey != null) { 
  80. try { 
  81. Cipher cipher = Cipher.getInstance("RSA"); 
  82. cipher.init(Cipher.DECRYPT_MODE, privateKey); 
  83. return cipher.doFinal(obj); 
  84. } catch (Exception e) { 
  85. e.printStackTrace(); 
  86.  
  87. return null;