RSA加密與解密


  1. package utils;  
[html] view plaincopy
  1. import java.io.ByteArrayOutputStream;  
  2. import java.security.Key;  
  3. import java.security.KeyFactory;  
  4. import java.security.KeyPair;  
  5. import java.security.KeyPairGenerator;  
  6. import java.security.PrivateKey;  
  7. import java.security.PublicKey;  
  8. import java.security.Signature;  
  9. import java.security.interfaces.RSAPrivateKey;  
  10. import java.security.interfaces.RSAPublicKey;  
  11. import java.security.spec.PKCS8EncodedKeySpec;  
  12. import java.security.spec.X509EncodedKeySpec;  
  13. import java.util.HashMap;  
  14. import java.util.Map;  
  15.   
  16. import javax.crypto.Cipher;  
  17.   
  18. /**  
  19.  * <p>  
  20.  * RSA公鑰/私鑰/簽名工具包  
  21.  * </p>  
  22.  * <p>  
  23.  * 羅納德·李維斯特(Ron [R]ivest)、阿迪·薩莫爾(Adi [S]hamir)和倫納德·阿德曼(Leonard [A]dleman)  
  24.  * </p>  
  25.  * <p>  
  26.  * 字符串格式的密鑰在未在特殊說明情況下都爲BASE64編碼格式<br/>  
  27.  * 由於非對稱加密速度極其緩慢,一般文件不使用它來加密而是使用對稱加密,<br/>  
  28.  * 非對稱加密算法可以用來對對稱加密的密鑰加密,這樣保證密鑰的安全也就保證了數據的安全  
  29.  * </p>  
  30.  *   
  31.  * @author IceWee  
  32.  * @date 2012-4-26  
  33.  * @version 1.0  
  34.  */  
  35. public class RSAUtils {  
  36.   
  37.     /**  
  38.      * 加密算法RSA  
  39.      */  
  40.     public static final String KEY_ALGORITHM = "RSA";  
  41.   
  42.     /**  
  43.      * 簽名算法  
  44.      */  
  45.     public static final String SIGNATURE_ALGORITHM = "MD5withRSA";  
  46.   
  47.     /**  
  48.      * 獲取公鑰的key  
  49.      */  
  50.     private static final String PUBLIC_KEY = "RSAPublicKey";  
  51.   
  52.     /**  
  53.      * 獲取私鑰的key  
  54.      */  
  55.     private static final String PRIVATE_KEY = "RSAPrivateKey";  
  56.   
  57.     /**  
  58.      * RSA最大加密明文大小  
  59.      */  
  60.     private static final int MAX_ENCRYPT_BLOCK = 117;  
  61.   
  62.     /**  
  63.      * RSA最大解密密文大小  
  64.      */  
  65.     private static final int MAX_DECRYPT_BLOCK = 128;  
  66.   
  67.     /**  
  68.      * <p>  
  69.      * 生成密鑰對(公鑰和私鑰)  
  70.      * </p>  
  71.      *   
  72.      * @return  
  73.      * @throws Exception  
  74.      */  
  75.     public static Map<String, Object> genKeyPair() throws Exception {  
  76.         KeyPairGenerator keyPairGen = KeyPairGenerator  
  77.                 .getInstance(KEY_ALGORITHM);  
  78.         keyPairGen.initialize(1024);  
  79.         KeyPair keyPair = keyPairGen.generateKeyPair();  
  80.         RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  
  81.         RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();  
  82.         Map<String, Object> keyMap = new HashMap<String, Object>(2);  
  83.         keyMap.put(PUBLIC_KEY, publicKey);  
  84.         keyMap.put(PRIVATE_KEY, privateKey);  
  85.         return keyMap;  
  86.     }  
  87.   
  88.     /**  
  89.      * <p>  
  90.      * 用私鑰對信息生成數字簽名  
  91.      * </p>  
  92.      *   
  93.      * @param data  
  94.      *            已加密數據  
  95.      * @param privateKey  
  96.      *            私鑰(BASE64編碼)  
  97.      *   
  98.      * @return  
  99.      * @throws Exception  
  100.      */  
  101.     public static String sign(byte[] data, String privateKey) throws Exception {  
  102.         byte[] keyBytes = Base64Utils.decode(privateKey);  
  103.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);  
  104.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  105.         PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);  
  106.         Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);  
  107.         signature.initSign(privateK);  
  108.         signature.update(data);  
  109.         return Base64Utils.encode(signature.sign());  
  110.     }  
  111.   
  112.     /**  
  113.      * <p>  
  114.      * 校驗數字簽名  
  115.      * </p>  
  116.      *   
  117.      * @param data  
  118.      *            已加密數據  
  119.      * @param publicKey  
  120.      *            公鑰(BASE64編碼)  
  121.      * @param sign  
  122.      *            數字簽名  
  123.      *   
  124.      * @return  
  125.      * @throws Exception  
  126.      *   
  127.      */  
  128.     public static boolean verify(byte[] data, String publicKey, String sign)  
  129.             throws Exception {  
  130.         byte[] keyBytes = Base64Utils.decode(publicKey);  
  131.         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);  
  132.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  133.         PublicKey publicK = keyFactory.generatePublic(keySpec);  
  134.         Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);  
  135.         signature.initVerify(publicK);  
  136.         signature.update(data);  
  137.         return signature.verify(Base64Utils.decode(sign));  
  138.     }  
  139.   
  140.     /**  
  141.      * <P>  
  142.      * 私鑰解密  
  143.      * </p>  
  144.      *   
  145.      * @param encryptedData  
  146.      *            已加密數據  
  147.      * @param privateKey  
  148.      *            私鑰(BASE64編碼)  
  149.      * @return  
  150.      * @throws Exception  
  151.      */  
  152.     public static byte[] decryptByPrivateKey(byte[] encryptedData,  
  153.             String privateKey) throws Exception {  
  154.         byte[] keyBytes = Base64Utils.decode(privateKey);  
  155.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);  
  156.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  157.         Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);  
  158.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
  159.         cipher.init(Cipher.DECRYPT_MODE, privateK);  
  160.         int inputLen = encryptedData.length;  
  161.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  162.         int offSet = 0;  
  163.         byte[] cache;  
  164.         int i = 0;  
  165.         // 對數據分段解密  
  166.         while (inputLen - offSet > 0) {  
  167.             if (inputLen - offSet > MAX_DECRYPT_BLOCK) {  
  168.                 cache = cipher  
  169.                         .doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);  
  170.             } else {  
  171.                 cache = cipher  
  172.                         .doFinal(encryptedData, offSet, inputLen - offSet);  
  173.             }  
  174.             out.write(cache, 0, cache.length);  
  175.             i++;  
  176.             offSet = i * MAX_DECRYPT_BLOCK;  
  177.         }  
  178.         byte[] decryptedData = out.toByteArray();  
  179.         out.close();  
  180.         return decryptedData;  
  181.     }  
  182.   
  183.     /**  
  184.      * <p>  
  185.      * 公鑰解密  
  186.      * </p>  
  187.      *   
  188.      * @param encryptedData  
  189.      *            已加密數據  
  190.      * @param publicKey  
  191.      *            公鑰(BASE64編碼)  
  192.      * @return  
  193.      * @throws Exception  
  194.      */  
  195.     public static byte[] decryptByPublicKey(byte[] encryptedData,  
  196.             String publicKey) throws Exception {  
  197.         byte[] keyBytes = Base64Utils.decode(publicKey);  
  198.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);  
  199.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  200.         Key publicK = keyFactory.generatePublic(x509KeySpec);  
  201.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
  202.         cipher.init(Cipher.DECRYPT_MODE, publicK);  
  203.         int inputLen = encryptedData.length;  
  204.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  205.         int offSet = 0;  
  206.         byte[] cache;  
  207.         int i = 0;  
  208.         // 對數據分段解密  
  209.         while (inputLen - offSet > 0) {  
  210.             if (inputLen - offSet > MAX_DECRYPT_BLOCK) {  
  211.                 cache = cipher  
  212.                         .doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);  
  213.             } else {  
  214.                 cache = cipher  
  215.                         .doFinal(encryptedData, offSet, inputLen - offSet);  
  216.             }  
  217.             out.write(cache, 0, cache.length);  
  218.             i++;  
  219.             offSet = i * MAX_DECRYPT_BLOCK;  
  220.         }  
  221.         byte[] decryptedData = out.toByteArray();  
  222.         out.close();  
  223.         return decryptedData;  
  224.     }  
  225.   
  226.     /**  
  227.      * <p>  
  228.      * 公鑰加密  
  229.      * </p>  
  230.      *   
  231.      * @param data  
  232.      *            源數據  
  233.      * @param publicKey  
  234.      *            公鑰(BASE64編碼)  
  235.      * @return  
  236.      * @throws Exception  
  237.      */  
  238.     public static byte[] encryptByPublicKey(byte[] data, String publicKey)  
  239.             throws Exception {  
  240.         byte[] keyBytes = Base64Utils.decode(publicKey);  
  241.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);  
  242.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  243.         Key publicK = keyFactory.generatePublic(x509KeySpec);  
  244.         // 對數據加密  
  245.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
  246.         cipher.init(Cipher.ENCRYPT_MODE, publicK);  
  247.         int inputLen = data.length;  
  248.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  249.         int offSet = 0;  
  250.         byte[] cache;  
  251.         int i = 0;  
  252.         // 對數據分段加密  
  253.         while (inputLen - offSet > 0) {  
  254.             if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {  
  255.                 cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);  
  256.             } else {  
  257.                 cache = cipher.doFinal(data, offSet, inputLen - offSet);  
  258.             }  
  259.             out.write(cache, 0, cache.length);  
  260.             i++;  
  261.             offSet = i * MAX_ENCRYPT_BLOCK;  
  262.         }  
  263.         byte[] encryptedData = out.toByteArray();  
  264.         out.close();  
  265.         return encryptedData;  
  266.     }  
  267.   
  268.     /**  
  269.      * <p>  
  270.      * 私鑰加密  
  271.      * </p>  
  272.      *   
  273.      * @param data  
  274.      *            源數據  
  275.      * @param privateKey  
  276.      *            私鑰(BASE64編碼)  
  277.      * @return  
  278.      * @throws Exception  
  279.      */  
  280.     public static byte[] encryptByPrivateKey(byte[] data, String privateKey)  
  281.             throws Exception {  
  282.         byte[] keyBytes = Base64Utils.decode(privateKey);  
  283.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);  
  284.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  285.         Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);  
  286.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
  287.         cipher.init(Cipher.ENCRYPT_MODE, privateK);  
  288.         int inputLen = data.length;  
  289.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  290.         int offSet = 0;  
  291.         byte[] cache;  
  292.         int i = 0;  
  293.         // 對數據分段加密  
  294.         while (inputLen - offSet > 0) {  
  295.             if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {  
  296.                 cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);  
  297.             } else {  
  298.                 cache = cipher.doFinal(data, offSet, inputLen - offSet);  
  299.             }  
  300.             out.write(cache, 0, cache.length);  
  301.             i++;  
  302.             offSet = i * MAX_ENCRYPT_BLOCK;  
  303.         }  
  304.         byte[] encryptedData = out.toByteArray();  
  305.         out.close();  
  306.         return encryptedData;  
  307.     }  
  308.   
  309.     /**  
  310.      * <p>  
  311.      * 獲取私鑰  
  312.      * </p>  
  313.      *   
  314.      * @param keyMap  
  315.      *            密鑰對  
  316.      * @return  
  317.      * @throws Exception  
  318.      */  
  319.     public static String getPrivateKey(Map<String, Object> keyMap)  
  320.             throws Exception {  
  321.         Key key = (Key) keyMap.get(PRIVATE_KEY);  
  322.         return Base64Utils.encode(key.getEncoded());  
  323.     }  
  324.   
  325.     /**  
  326.      * <p>  
  327.      * 獲取公鑰  
  328.      * </p>  
  329.      *   
  330.      * @param keyMap  
  331.      *            密鑰對  
  332.      * @return  
  333.      * @throws Exception  
  334.      */  
  335.     public static String getPublicKey(Map<String, Object> keyMap)  
  336.             throws Exception {  
  337.         Key key = (Key) keyMap.get(PUBLIC_KEY);  
  338.         return Base64Utils.encode(key.getEncoded());  
  339.     }  
  340.   
  341. }  

Base64Utils文件

[html] view plaincopy
  1. package utils;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.InputStream;  
  9. import java.io.OutputStream;  
  10. import com.sun.org.apache.xml.internal.security.utils.Base64;    
  11. /**  
  12.  * <p>  
  13.  * BASE64編碼解碼工具包  
  14.  * </p>  
  15.  * <p>  
  16.  * 依賴javabase64-1.3.1.jar  
  17.  * </p>  
  18.  *   
  19.  * @author IceWee  
  20.  * @date 2012-5-19  
  21.  * @version 1.0  
  22.  */  
  23. public class Base64Utils {  
  24.   
  25.     /**  
  26.      * 文件讀取緩衝區大小  
  27.      */  
  28.     private static final int CACHE_SIZE = 1024;  
  29.       
  30.     /**  
  31.      * <p>  
  32.      * BASE64字符串解碼爲二進制數據  
  33.      * </p>  
  34.      *   
  35.      * @param base64  
  36.      * @return  
  37.      * @throws Exception  
  38.      */  
  39.     public static byte[] decode(String base64) throws Exception {  
  40.         return Base64.decode(base64.getBytes());  
  41.     }  
  42.       
  43.     /**  
  44.      * <p>  
  45.      * 二進制數據編碼爲BASE64字符串  
  46.      * </p>  
  47.      *   
  48.      * @param bytes  
  49.      * @return  
  50.      * @throws Exception  
  51.      */  
  52.     public static String encode(byte[] bytes) throws Exception {  
  53.         return new String(Base64.encode(bytes));  
  54.     }  
  55.       
  56.     /**  
  57.      * <p>  
  58.      * 將文件編碼爲BASE64字符串  
  59.      * </p>  
  60.      * <p>  
  61.      * 大文件慎用,可能會導致內存溢出  
  62.      * </p>  
  63.      *   
  64.      * @param filePath 文件絕對路徑  
  65.      * @return  
  66.      * @throws Exception  
  67.      */  
  68.     public static String encodeFile(String filePath) throws Exception {  
  69.         byte[] bytes = fileToByte(filePath);  
  70.         return encode(bytes);  
  71.     }  
  72.       
  73.     /**  
  74.      * <p>  
  75.      * BASE64字符串轉回文件  
  76.      * </p>  
  77.      *   
  78.      * @param filePath 文件絕對路徑  
  79.      * @param base64 編碼字符串  
  80.      * @throws Exception  
  81.      */  
  82.     public static void decodeToFile(String filePath, String base64) throws Exception {  
  83.         byte[] bytes = decode(base64);  
  84.         byteArrayToFile(bytes, filePath);  
  85.     }  
  86.       
  87.     /**  
  88.      * <p>  
  89.      * 文件轉換爲二進制數組  
  90.      * </p>  
  91.      *   
  92.      * @param filePath 文件路徑  
  93.      * @return  
  94.      * @throws Exception  
  95.      */  
  96.     public static byte[] fileToByte(String filePath) throws Exception {  
  97.         byte[] data = new byte[0];  
  98.         File file = new File(filePath);  
  99.         if (file.exists()) {  
  100.             FileInputStream in = new FileInputStream(file);  
  101.             ByteArrayOutputStream out = new ByteArrayOutputStream(2048);  
  102.             byte[] cache = new byte[CACHE_SIZE];  
  103.             int nRead = 0;  
  104.             while ((nRead = in.read(cache)) != -1) {  
  105.                 out.write(cache, 0, nRead);  
  106.                 out.flush();  
  107.             }  
  108.             out.close();  
  109.             in.close();  
  110.             data = out.toByteArray();  
  111.          }  
  112.         return data;  
  113.     }  
  114.       
  115.     /**  
  116.      * <p>  
  117.      * 二進制數據寫文件  
  118.      * </p>  
  119.      *   
  120.      * @param bytes 二進制數據  
  121.      * @param filePath 文件生成目錄  
  122.      */  
  123.     public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {  
  124.         InputStream in = new ByteArrayInputStream(bytes);     
  125.         File destFile = new File(filePath);  
  126.         if (!destFile.getParentFile().exists()) {  
  127.             destFile.getParentFile().mkdirs();  
  128.         }  
  129.         destFile.createNewFile();  
  130.         OutputStream out = new FileOutputStream(destFile);  
  131.         byte[] cache = new byte[CACHE_SIZE];  
  132.         int nRead = 0;  
  133.         while ((nRead = in.read(cache)) != -1) {     
  134.             out.write(cache, 0, nRead);  
  135.             out.flush();  
  136.         }  
  137.         out.close();  
  138.         in.close();  
  139.     }  
  140.       
  141.       
  142. }  

測試用例:



[html] view plaincopy
  1. package util;  
  2.   
  3.   
  4. import java.util.Map;  
  5.   
  6. import RSAUtils;  
  7.   
  8. public class RSATester {  
  9.   
  10.     static String publicKey;  
  11.     static String privateKey;  
  12.   
  13.     static {  
  14.         try {  
  15.             Map<String, Object> keyMap = RSAUtils.genKeyPair();  
  16.             publicKey = RSAUtils.getPublicKey(keyMap);  
  17.             privateKey = RSAUtils.getPrivateKey(keyMap);  
  18.             System.err.println("公鑰: \n\r" + publicKey);  
  19.             System.err.println("私鑰: \n\r" + privateKey);  
  20.         } catch (Exception e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24.       
  25.     public static void main(String[] args) throws Exception {  
  26.         test();  
  27.         testSign();  
  28.     }  
  29.   
  30.     static void test() throws Exception {  
  31.         System.err.println("公鑰加密——私鑰解密");  
  32.         String source = "這是一行沒有任何意義的文字,你看完了等於沒看,不是嗎?";  
  33.         System.out.println("\r加密前文字:\r\n" + source);  
  34.         byte[] data = source.getBytes();  
  35.         byte[] encodedData = RSAUtils.encryptByPublicKey(data, publicKey);  
  36.         System.out.println("加密後文字:\r\n" + new String(encodedData));  
  37.         byte[] decodedData = RSAUtils.decryptByPrivateKey(encodedData, privateKey);  
  38.         String target = new String(decodedData);  
  39.         System.out.println("解密後文字: \r\n" + target);  
  40.     }  
  41.   
  42.     static void testSign() throws Exception {  
  43.         System.err.println("私鑰加密——公鑰解密");  
  44.         String source = "這是一行測試RSA數字簽名的無意義文字";  
  45.         System.out.println("原文字:\r\n" + source);  
  46.         byte[] data = source.getBytes();  
  47.         byte[] encodedData = RSAUtils.encryptByPrivateKey(data, privateKey);  
  48.         System.out.println("加密後:\r\n" + new String(encodedData));  
  49.         byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);  
  50.         String target = new String(decodedData);  
  51.         System.out.println("解密後: \r\n" + target);  
  52.         System.err.println("私鑰簽名——公鑰驗證簽名");  
  53.         String sign = RSAUtils.sign(encodedData, privateKey);  
  54.         System.err.println("簽名:\r" + sign);  
  55.         boolean status = RSAUtils.verify(encodedData, publicKey, sign);  
  56.         System.err.println("驗證結果:\r" + status);  
  57.     }  
  58.       
  59. }  

生成RSA密鑰、保存到文件、從文件讀取、加密、解密等操作

[html] view plaincopy
  1. import java.security.Key;     
  2. import java.security.KeyFactory;     
  3. import java.security.KeyPair;     
  4. import java.security.KeyPairGenerator;     
  5. import java.security.NoSuchAlgorithmException;     
  6. import java.security.PrivateKey;     
  7. import java.security.PublicKey;     
  8. import java.security.SecureRandom;     
  9. import java.security.interfaces.RSAPrivateKey;     
  10. import java.security.interfaces.RSAPublicKey;     
  11. import java.security.spec.InvalidKeySpecException;     
  12. import java.security.spec.PKCS8EncodedKeySpec;     
  13. import java.security.spec.X509EncodedKeySpec;     
  14. import javax.crypto.Cipher;     
  15. import org.apache.commons.configuration.ConfigurationException;     
  16. import org.apache.commons.configuration.PropertiesConfiguration;     
  17. import org.bouncycastle.jce.provider.BouncyCastleProvider;     
  18.     
  19. public class RSATest {     
  20.     
  21.     public static void main(String[] args) {     
  22.         try {     
  23.             RSATest encrypt = new RSATest();     
  24.             String encryptText = "encryptText";     
  25.     
  26.             // Generate keys     
  27.             KeyPair keyPair = encrypt.generateKey();     
  28.             RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();     
  29.             RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();     
  30.     
  31.             byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes());     
  32.             byte[] de = encrypt.decrypt(privateKey, e);     
  33.             System.out.println(toHexString(e));     
  34.             System.out.println(toHexString(de));     
  35.         } catch (Exception e) {     
  36.             e.printStackTrace();     
  37.         }     
  38.     }     
  39.     
  40.     public KeyPair generateKey() throws NoSuchAlgorithmException {     
  41.         KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");     
  42.         keyPairGen.initialize(1024, new SecureRandom());     
  43.     
  44.         KeyPair keyPair = keyPairGen.generateKeyPair();     
  45.         return keyPair;     
  46.     }     
  47.     
  48.     public void saveKey(KeyPair keyPair, String publicKeyFile,     
  49.             String privateKeyFile) throws ConfigurationException {     
  50.         PublicKey pubkey = keyPair.getPublic();     
  51.         PrivateKey prikey = keyPair.getPrivate();     
  52.     
  53.         // save public key     
  54.         PropertiesConfiguration publicConfig = new PropertiesConfiguration(     
  55.                 publicKeyFile);     
  56.         publicConfig.setProperty("PULIICKEY", toHexString(pubkey.getEncoded()));     
  57.         publicConfig.save();     
  58.     
  59.         // save private key     
  60.         PropertiesConfiguration privateConfig = new PropertiesConfiguration(     
  61.                 privateKeyFile);     
  62.         privateConfig.setProperty("PRIVATEKEY",     
  63.                 toHexString(prikey.getEncoded()));     
  64.         privateConfig.save();     
  65.     }     
  66.     
  67.     /**    
  68.      * @param filename    
  69.      * @param type:    
  70.      *            1-public 0-private    
  71.      * @return    
  72.      * @throws ConfigurationException    
  73.      * @throws NoSuchAlgorithmException    
  74.      * @throws InvalidKeySpecException    
  75.      */    
  76.     public Key loadKey(String filename, int type)     
  77.             throws ConfigurationException, NoSuchAlgorithmException,     
  78.             InvalidKeySpecException {     
  79.         PropertiesConfiguration config = new PropertiesConfiguration(filename);     
  80.         KeyFactory keyFactory = KeyFactory.getInstance("RSA",     
  81.                 new BouncyCastleProvider());     
  82.     
  83.         if (type == 0) {     
  84.             // privateKey     
  85.             String privateKeyValue = config.getString("PULIICKEY");     
  86.             PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(     
  87.                     toBytes(privateKeyValue));     
  88.             PrivateKey privateKey = keyFactory.generatePrivate(priPKCS8);     
  89.             return privateKey;     
  90.     
  91.         } else {     
  92.             // publicKey     
  93.             String privateKeyValue = config.getString("PRIVATEKEY");     
  94.             X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(     
  95.                     toBytes(privateKeyValue));     
  96.             PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);     
  97.             return publicKey;     
  98.         }     
  99.     }     
  100.     
  101.     /**    
  102.      * Encrypt String.    
  103.      *     
  104.      * @return byte[]    
  105.      */    
  106.     protected byte[] encrypt(RSAPublicKey publicKey, byte[] data) {     
  107.         if (publicKey != null) {     
  108.             try {     
  109.                 Cipher cipher = Cipher.getInstance("RSA",     
  110.                         new BouncyCastleProvider());     
  111.                 cipher.init(Cipher.ENCRYPT_MODE, publicKey);     
  112.                 return cipher.doFinal(data);     
  113.             } catch (Exception e) {     
  114.                 e.printStackTrace();     
  115.             }     
  116.         }     
  117.         return null;     
  118.     }     
  119.     
  120.     /**    
  121.      * Basic decrypt method    
  122.      *     
  123.      * @return byte[]    
  124.      */    
  125.     protected byte[] decrypt(RSAPrivateKey privateKey, byte[] raw) {     
  126.         if (privateKey != null) {     
  127.             try {     
  128.                 Cipher cipher = Cipher.getInstance("RSA",     
  129.                         new BouncyCastleProvider());     
  130.                 cipher.init(Cipher.DECRYPT_MODE, privateKey);     
  131.                 return cipher.doFinal(raw);     
  132.             } catch (Exception e) {     
  133.                 e.printStackTrace();     
  134.             }     
  135.         }     
  136.     
  137.         return null;     
  138.     }     
  139.     
  140.     public static String toHexString(byte[] b) {     
  141.         StringBuilder sb = new StringBuilder(b.length * 2);     
  142.         for (int i = 0; i < b.length; i++) {     
  143.             sb.append(HEXCHAR[(b[i] & 0xf0) >>> 4]);     
  144.             sb.append(HEXCHAR[b[i] & 0x0f]);     
  145.         }     
  146.         return sb.toString();     
  147.     }     
  148.     
  149.     public static final byte[] toBytes(String s) {     
  150.         byte[] bytes;     
  151.         bytes = new byte[s.length() / 2];     
  152.         for (int i = 0; i < bytes.length; i++) {     
  153.             bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),     
  154.                     16);     
  155.         }     
  156.         return bytes;     
  157.     }     
  158.     
  159.     private static char[] HEXCHAR = { '0', '1', '2', '3', '4', '5', '6', '7',     
  160.             '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };     
  161.     
  162. }  


Java中RSA非對稱密鑰加解密使用示例

一、簡介:

  RSA加密算法是最常用的非對稱加密算法,CFCA在證書服務中離不了它。RSA是第一個比較完善的公開密鑰算法,它既能用於加密,也能用於數字簽名。這個算法經受住了多年深入的密碼分析,雖然密碼分析者既不能證明也不能否定RSA的安全性,但這恰恰說明該算法有一定的可信性,目前它已經成爲最流行的公開密鑰算法。

  二、RSA的公鑰、私鑰的組成,以及加密、解密的公式可見於下表

  三、使用方式:

  ① 假設A、B機器進行通信,已A機器爲主;

  ② A首先需要用自己的私鑰爲發送請求數據簽名,並將公鑰一同發送給B;

  ③ B收到數據後,需要用A發送的公鑰進行驗證,已確保收到的數據是未經篡改的;

  ④ B驗籤通過後,處理邏輯,並把處理結果返回,返回數據需要用A發送的公鑰進行加密(公鑰加密後,只能用配對的私鑰解密);

  ⑤ A收到B返回的數據,使用私鑰解密,至此,一次數據交互完成。

  四、代碼示例:

  1、第一步獲取私鑰,爲簽名做準備。

[html] view plaincopy
  1. /**  
  2.      * 讀取私鑰  返回PrivateKey  
  3.      * @param path  包含私鑰的證書路徑  
  4.      * @param password  私鑰證書密碼  
  5.      * @return 返回私鑰PrivateKey  
  6.      * @throws KeyStoreException  
  7.      * @throws NoSuchAlgorithmException  
  8.      * @throws CertificateException  
  9.      * @throws IOException  
  10.      * @throws UnrecoverableKeyException  
  11.      */  
  12.     private static PrivateKey getPrivateKey(String path,String password)  
  13.             throws KeyStoreException, NoSuchAlgorithmException, CertificateException,  
  14.             IOException, UnrecoverableKeyException {  
  15.         KeyStore ks = KeyStore.getInstance("PKCS12");  
  16.         FileInputStream fis = new FileInputStream(path);  
  17.         char[] nPassword = null;  
  18.         if ((password == null) || password.trim().equals("")) {  
  19.             nPassword = null;  
  20.         } else {  
  21.             nPassword = password.toCharArray();  
  22.         }  
  23.         ks.load(fis, nPassword);  
  24.         fis.close();  
  25.         Enumeration<String> en = ks.aliases();  
  26.         String keyAlias = null;  
  27.         if (en.hasMoreElements()) {  
  28.             keyAlias = (String) en.nextElement();  
  29.         }  
  30.    
  31.         return (PrivateKey) ks.getKey(keyAlias, nPassword);  
  32.     }  


2、簽名示例:通過第一步得到的私鑰,進行簽名操作,具體請看以下代碼:

[html] view plaincopy
  1. /**  
  2.      * 私鑰簽名: 簽名方法如下:BASE64(RSA(MD5(src),privatekey)),其中src爲需要簽名的字符串,  
  3. privatekey是商戶的CFCA證書私鑰。  
  4.      * @param plainText 待簽名字符串  
  5.      * @param path 簽名私鑰路徑  
  6.      * @param password  簽名私鑰密碼  
  7.      * @return 返回簽名後的字符串  
  8.      * @throws Exception  
  9.      */  
  10.     public static String sign(String plainText,String path,String password)  
  11.             throws Exception  {  
  12.         /*  
  13.          * MD5加密  
  14.          */  
  15.         MessageDigest md5 = MessageDigest.getInstance("MD5");  
  16.         md5.update(plainText.getBytes("utf-8"));  
  17.         byte[] digestBytes = md5.digest();  
  18.         /*  
  19.          * 用私鑰進行簽名 RSA  
  20.          * Cipher負責完成加密或解密工作,基於RSA  
  21.          */  
  22.         Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");  
  23.         //ENCRYPT_MODE表示爲加密模式  
  24.         cipher.init(Cipher.ENCRYPT_MODE, getPrivateKey(path, password));  
  25.         //加密  
  26.         byte[] rsaBytes = cipher.doFinal(digestBytes);  
  27.         //Base64編碼  
  28.         return Base64.byteArrayToBase64(rsaBytes);}  

3、B收到數據後,需要使用A提供的公鑰信息進行驗籤,此處使用公鑰的N、E進行驗籤

  首先通過公鑰N、E得到公鑰PublicKey,如下:

[html] view plaincopy
  1. /**   
  2.      * 根據公鑰n、e生成公鑰  
  3.      * @param modulus   公鑰n串  
  4.      * @param publicExponent  公鑰e串  
  5.      * @return 返回公鑰PublicKey  
  6.      * @throws Exception  
  7.      */  
  8.     public static PublicKey getPublickKey(String modulus, String publicExponent)  
  9.             throws Exception {  
  10.         KeySpec publicKeySpec = new RSAPublicKeySpec(  
  11.                 new BigInteger(modulus, 16), new BigInteger(publicExponent, 16));  
  12.         KeyFactory factory = KeyFactory.getInstance("RSA");  
  13.         PublicKey publicKey = factory.generatePublic(publicKeySpec);  
  14.         return publicKey;  
  15.     }  

得到公鑰PublicKey後,再去驗證簽名,代碼如下:

[html] view plaincopy
  1. /**  
  2.      * 用公鑰證書進行驗籤  
  3.      * @param message  簽名之前的原文  
  4.      * @param cipherText  簽名  
  5.      * @param pubKeyn 公鑰n串  
  6.      * @param pubKeye 公鑰e串  
  7.      * @return boolean 驗籤成功爲true,失敗爲false  
  8.      * @throws Exception  
  9.      */  
  10.     public static boolean verify(String message, String cipherText,String pubKeyn,  
  11.             String pubKeye) throws Exception {  
  12.         Cipher c4 = Cipher.getInstance("RSA/ECB/PKCS1Padding");  
  13.         // 根據密鑰,對Cipher對象進行初始化,DECRYPT_MODE表示解密模式  
  14.         c4.init(Cipher.DECRYPT_MODE, getPublickKey(pubKeyn,pubKeye));  
  15.         // 解密  
  16.         byte[] desDecTextBytes = c4.doFinal(Base64.base64ToByteArray(cipherText));  
  17.         // 得到前置對原文進行的MD5  
  18.         String md5Digest1 = Base64.byteArrayToBase64(desDecTextBytes);  
  19.         MessageDigest md5 = MessageDigest.getInstance("MD5");  
  20.         md5.update(message.getBytes("utf-8"));  
  21.         byte[] digestBytes = md5.digest();  
  22.         // 得到商戶對原文進行的MD5  
  23.         String md5Digest2 = Base64.byteArrayToBase64(digestBytes);  
  24.         // 驗證簽名  
  25.         if (md5Digest1.equals(md5Digest2)) {  
  26.             return true;  
  27.         } else {  
  28.             return false;  
  29.         }  
  30.     }  

 至此,簽名驗籤已經完畢

  4、提供一個從.cer文件讀取公鑰的方法:

[html] view plaincopy
  1. /**  
  2.      * 讀取公鑰cer  
  3.      * @param path .cer文件的路徑  如:c:/abc.cer  
  4.      * @return  base64後的公鑰串  
  5.      * @throws IOException  
  6.      * @throws CertificateException  
  7.      */  
  8.     public static String getPublicKey(String path) throws IOException,  
  9.     CertificateException{  
  10.         InputStream inStream = new FileInputStream(path);  
  11.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  12.         int ch;  
  13.         String res = "";  
  14.         while ((ch = inStream.read()) != -1) {  
  15.             out.write(ch);  
  16.         }  
  17.         byte[] result = out.toByteArray();  
  18.         res = Base64.byteArrayToBase64(result);  
  19.         return res;  
  20.     }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章