1. 程式人生 > >java中RSA加解密的實現

java中RSA加解密的實現

原文:https://blog.csdn.net/draven1122/article/details/55212195

關於加密資料長度和解密資料長度大家可以看一下我前一篇文章內的介紹:

  1. publicstaticvoid main(String[] args) throws Exception {  
  2.         // TODO Auto-generated method stub
  3.         HashMap<String, Object> map = RSAUtils.getKeys();  
  4.         //生成公鑰和私鑰
  5.         RSAPublicKey publicKey = (RSAPublicKey) map.get("public"
    );  
  6.         RSAPrivateKey privateKey = (RSAPrivateKey) map.get("private");  
  7.         //模
  8.         String modulus = publicKey.getModulus().toString();  
  9.         //公鑰指數
  10.         String public_exponent = publicKey.getPublicExponent().toString();  
  11.         //私鑰指數
  12.         String private_exponent = privateKey.getPrivateExponent().toString();  
  13.         //明文
  14.         String ming = "123456789";  
  15.         //使用模和指數生成公鑰和私鑰
  16.         RSAPublicKey pubKey = RSAUtils.getPublicKey(modulus, public_exponent);  
  17.         RSAPrivateKey priKey = RSAUtils.getPrivateKey(modulus, private_exponent);  
  18.         //加密後的密文
  19.         String mi = RSAUtils.encryptByPublicKey(ming, pubKey);  
  20.         System.err.println(mi);  
  21.         //解密後的明文
  22.         ming = RSAUtils.decryptByPrivateKey(mi, priKey);  
  23.         System.err.println(ming);  
  24.     }  


RSAUtils.java

  1. package yyy.test.rsa;  
  2. import java.math.BigInteger;  
  3. import java.security.KeyFactory;  
  4. import java.security.KeyPair;  
  5. import java.security.KeyPairGenerator;  
  6. import java.security.NoSuchAlgorithmException;  
  7. import java.security.interfaces.RSAPrivateKey;  
  8. import java.security.interfaces.RSAPublicKey;  
  9. import java.security.spec.RSAPrivateKeySpec;  
  10. import java.security.spec.RSAPublicKeySpec;  
  11. import java.util.HashMap;  
  12. import javax.crypto.Cipher;  
  13. publicclass RSAUtils {  
  14.     /** 
  15.      * 生成公鑰和私鑰 
  16.      * @throws NoSuchAlgorithmException  
  17.      * 
  18.      */
  19.     publicstatic HashMap<String, Object> getKeys() throws NoSuchAlgorithmException{  
  20.         HashMap<String, Object> map = new HashMap<String, Object>();  
  21.         KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");  
  22.         keyPairGen.initialize(1024);  
  23.         KeyPair keyPair = keyPairGen.generateKeyPair();  
  24.         RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  
  25.         RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();  
  26.         map.put("public", publicKey);  
  27.         map.put("private", privateKey);  
  28.         return map;  
  29.     }  
  30.     /** 
  31.      * 使用模和指數生成RSA公鑰 
  32.      * 注意:【此程式碼用了預設補位方式,為RSA/None/PKCS1Padding,不同JDK預設的補位方式可能不同,如Android預設是RSA 
  33.      * /None/NoPadding】 
  34.      *  
  35.      * @param modulus 
  36.      *            模 
  37.      * @param exponent 
  38.      *            指數 
  39.      * @return 
  40.      */
  41.     publicstatic RSAPublicKey getPublicKey(String modulus, String exponent) {  
  42.         try {  
  43.             BigInteger b1 = new BigInteger(modulus);  
  44.             BigInteger b2 = new BigInteger(exponent);  
  45.             KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
  46.             RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);  
  47.             return (RSAPublicKey) keyFactory.generatePublic(keySpec);  
  48.         } catch (Exception e) {  
  49.             e.printStackTrace();  
  50.             returnnull;  
  51.         }  
  52.     }  
  53.     /** 
  54.      * 使用模和指數生成RSA私鑰 
  55.      * 注意:【此程式碼用了預設補位方式,為RSA/None/PKCS1Padding,不同JDK預設的補位方式可能不同,如Android預設是RSA 
  56.      * /None/NoPadding】 
  57.      *  
  58.      * @param modulus 
  59.      *            模 
  60.      * @param exponent 
  61.      *            指數 
  62.      * @return 
  63.      */
  64.     publicstatic RSAPrivateKey getPrivateKey(String modulus, String exponent) {  
  65.         try {  
  66.             BigInteger b1 = new BigInteger(modulus);  
  67.             BigInteger b2 = new BigInteger(exponent);  
  68.             KeyFactory keyFactory = KeyFactory.getInstance("RSA");  
  69.             RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(b1, b2);  
  70.             return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);  
  71.         } catch (Exception e) {  
  72.             e.printStackTrace();  
  73.             returnnull;  
  74.         }  
  75.     }  
  76.     /** 
  77.      * 公鑰加密 
  78.      *  
  79.      * @param data 
  80.      * @param publicKey 
  81.      * @return 
  82.      * @throws Exception 
  83.      */
  84.     publicstatic String encryptByPublicKey(String data, RSAPublicKey publicKey)  
  85.             throws Exception {  
  86.         Cipher cipher = Cipher.getInstance("RSA");  
  87.         cipher.init(Cipher.ENCRYPT_MODE, publicKey);  
  88.         // 模長
  89.         int key_len = publicKey.getModulus().bitLength() / 8;  
  90.         // 加密資料長度 <= 模長-11
  91.         String[] datas = splitString(data, key_len - 11);  
  92.         String mi = "";  
  93.         //如果明文長度大於模