java rsa加密算法java实现

来源:互联网 发布:小米授权淘宝网店 编辑:程序博客网 时间:2024/06/06 08:24
Java代码  收藏代码
  1. 简单完整的代码,通过这个代码你将对RSA加密算法在Java中的实现方法有一个初步的了解,这个类,你可以直接使用,水平高的,就自己修改完善下代码。  
  2. package security;  
  3. import java.security.*;  
  4. import java.security.spec.*;  
  5. import java.security.interfaces.*;  
  6. import javax.crypto.spec.*;  
  7. import javax.crypto.interfaces.*;  
  8. import java.io.*;  
  9. import java.math.*;  
  10. public class RSADemo {  
  11.     public RSADemo() {  
  12.     }  
  13.     public static void generateKey() {  
  14.         try {  
  15.             KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");  
  16.             kpg.initialize(1024);  
  17.             KeyPair kp = kpg.genKeyPair();  
  18.             PublicKey pbkey = kp.getPublic();  
  19.             PrivateKey prkey = kp.getPrivate();  
  20.             // 保存公钥  (官网:www.fhadmin.org)
  21.             FileOutputStream f1 = new FileOutputStream("pubkey.dat");  
  22.             ObjectOutputStream b1 = new ObjectOutputStream(f1);  
  23.             b1.writeObject(pbkey);  
  24.             // 保存私钥  
  25.             FileOutputStream f2 = new FileOutputStream("privatekey.dat");  
  26.             ObjectOutputStream b2 = new ObjectOutputStream(f2);  
  27.             b2.writeObject(prkey);  
  28.         } catch (Exception e) {  
  29.         }  
  30.     }  
  31.     public static void encrypt() throws Exception {  
  32.         String s = "Hello World!";  
  33.         // 获取公钥及参数e,n  (官网:www.fhadmin.org)
  34.         FileInputStream f = new FileInputStream("pubkey.dat");  
  35.         ObjectInputStream b = new ObjectInputStream(f);  
  36.         RSAPublicKey pbk = (RSAPublicKey) b.readObject();  
  37.         BigInteger e = pbk.getPublicExponent();  
  38.         BigInteger n = pbk.getModulus();  
  39.         System.out.println("e= " + e);  
  40.         System.out.println("n= " + n);  
  41.         // 获取明文m  
  42.         byte ptext[] = s.getBytes("UTF-8");  
  43.         BigInteger m = new BigInteger(ptext);  
  44.         // 计算密文c  
  45.         BigInteger c = m.modPow(e, n);  
  46.         System.out.println("c= " + c);  
  47.         // 保存密文  
  48.         String cs = c.toString();  
  49.         BufferedWriter out =  
  50.             new BufferedWriter(  
  51.                 new OutputStreamWriter(new FileOutputStream("encrypt.dat")));  
  52.         out.write(cs, 0, cs.length());  
  53.         out.close();  
  54.     }  
  55.     public static void decrypt() throws Exception {  
  56.         // 读取密文  
  57.         BufferedReader in =  
  58.             new BufferedReader(  
  59.                 new InputStreamReader(new FileInputStream("encrypt.dat")));  
  60.         String ctext = in.readLine();  
  61.         BigInteger c = new BigInteger(ctext);  
  62.         // 读取私钥  
  63.         FileInputStream f = new FileInputStream("privatekey.dat");  
  64.         ObjectInputStream b = new ObjectInputStream(f);  
  65.         RSAPrivateKey prk = (RSAPrivateKey) b.readObject();  
  66.         BigInteger d = prk.getPrivateExponent();  
  67.         // 获取私钥参数及解密  
  68.         BigInteger n = prk.getModulus();  
  69.         System.out.println("d= " + d);  
  70.         System.out.println("n= " + n);  
  71.         BigInteger m = c.modPow(d, n);  
  72.         // 显示解密结果  (官网:www.fhadmin.org)
  73.         System.out.println("m= " + m);  
  74.         byte[] mt = m.toByteArray();  
  75.         System.out.println("PlainText is ");  
  76.         for (int i = 0; i < mt.length; i++) {  
  77.             System.out.print((char) mt[i]);  
  78.         }  
  79.     }  
  80.     public static void main(String args[]) {  
  81.         try {  
  82.             generateKey();  
  83.             encrypt();  
  84.             decrypt();  
  85.         } catch (Exception e) {  
  86.             System.out.println(e.toString());  
  87.         }  
  88.     }  
  89. }