天天看點

RSA加密與解密

[html]  view plain  copy

  1. package utils;  
  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. import javax.crypto.Cipher;  
  16. /**  
  17.  * <p>  
  18.  * RSA公鑰/私鑰/簽名工具包  
  19.  * </p>  
  20.  * 羅納德·李維斯特(Ron [R]ivest)、阿迪·薩莫爾(Adi [S]hamir)和倫納德·阿德曼(Leonard [A]dleman)  
  21.  * 字元串格式的密鑰在未在特殊說明情況下都為BASE64編碼格式<br/>  
  22.  * 由于非對稱加密速度極其緩慢,一般檔案不使用它來加密而是使用對稱加密,<br/>  
  23.  * 非對稱加密算法可以用來對對稱加密的密鑰加密,這樣保證密鑰的安全也就保證了資料的安全  
  24.  *   
  25.  * @author IceWee  
  26.  * @date 2012-4-26  
  27.  * @version 1.0  
  28.  */  
  29. public class RSAUtils {  
  30.     /**  
  31.      * 加密算法RSA  
  32.      */  
  33.     public static final String KEY_ALGORITHM = "RSA";  
  34.      * 簽名算法  
  35.     public static final String SIGNATURE_ALGORITHM = "MD5withRSA";  
  36.      * 擷取公鑰的key  
  37.     private static final String PUBLIC_KEY = "RSAPublicKey";  
  38.      * 擷取私鑰的key  
  39.     private static final String PRIVATE_KEY = "RSAPrivateKey";  
  40.      * RSA最大加密明文大小  
  41.     private static final int MAX_ENCRYPT_BLOCK = 117;  
  42.      * RSA最大解密密文大小  
  43.     private static final int MAX_DECRYPT_BLOCK = 128;  
  44.      * <p>  
  45.      * 生成密鑰對(公鑰和私鑰)  
  46.      * </p>  
  47.      *   
  48.      * @return  
  49.      * @throws Exception  
  50.     public static Map<String, Object> genKeyPair() throws Exception {  
  51.         KeyPairGenerator keyPairGen = KeyPairGenerator  
  52.                 .getInstance(KEY_ALGORITHM);  
  53.         keyPairGen.initialize(1024);  
  54.         KeyPair keyPair = keyPairGen.generateKeyPair();  
  55.         RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  
  56.         RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();  
  57.         Map<String, Object> keyMap = new HashMap<String, Object>(2);  
  58.         keyMap.put(PUBLIC_KEY, publicKey);  
  59.         keyMap.put(PRIVATE_KEY, privateKey);  
  60.         return keyMap;  
  61.     }  
  62.      * 用私鑰對資訊生成數字簽名  
  63.      * @param data  
  64.      *            已加密資料  
  65.      * @param privateKey  
  66.      *            私鑰(BASE64編碼)  
  67.     public static String sign(byte[] data, String privateKey) throws Exception {  
  68.         byte[] keyBytes = Base64Utils.decode(privateKey);  
  69.         PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);  
  70.         KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);  
  71.         PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);  
  72.         Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);  
  73.         signature.initSign(privateK);  
  74.         signature.update(data);  
  75.         return Base64Utils.encode(signature.sign());  
  76.      * 校驗數字簽名  
  77.      * @param publicKey  
  78.      *            公鑰(BASE64編碼)  
  79.      * @param sign  
  80.      *            數字簽名  
  81.     public static boolean verify(byte[] data, String publicKey, String sign)  
  82.             throws Exception {  
  83.         byte[] keyBytes = Base64Utils.decode(publicKey);  
  84.         X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);  
  85.         PublicKey publicK = keyFactory.generatePublic(keySpec);  
  86.         signature.initVerify(publicK);  
  87.         return signature.verify(Base64Utils.decode(sign));  
  88.      * <P>  
  89.      * 私鑰解密  
  90.      * @param encryptedData  
  91.     public static byte[] decryptByPrivateKey(byte[] encryptedData,  
  92.             String privateKey) throws Exception {  
  93.         Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);  
  94.         Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
  95.         cipher.init(Cipher.DECRYPT_MODE, privateK);  
  96.         int inputLen = encryptedData.length;  
  97.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  98.         int offSet = 0;  
  99.         byte[] cache;  
  100.         int i = 0;  
  101.         // 對資料分段解密  
  102.         while (inputLen - offSet > 0) {  
  103.             if (inputLen - offSet > MAX_DECRYPT_BLOCK) {  
  104.                 cache = cipher  
  105.                         .doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);  
  106.             } else {  
  107.                         .doFinal(encryptedData, offSet, inputLen - offSet);  
  108.             }  
  109.             out.write(cache, 0, cache.length);  
  110.             i++;  
  111.             offSet = i * MAX_DECRYPT_BLOCK;  
  112.         }  
  113.         byte[] decryptedData = out.toByteArray();  
  114.         out.close();  
  115.         return decryptedData;  
  116.      * 公鑰解密  
  117.     public static byte[] decryptByPublicKey(byte[] encryptedData,  
  118.             String publicKey) throws Exception {  
  119.         X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);  
  120.         Key publicK = keyFactory.generatePublic(x509KeySpec);  
  121.         cipher.init(Cipher.DECRYPT_MODE, publicK);  
  122.      * 公鑰加密  
  123.      *            源資料  
  124.     public static byte[] encryptByPublicKey(byte[] data, String publicKey)  
  125.         // 對資料加密  
  126.         cipher.init(Cipher.ENCRYPT_MODE, publicK);  
  127.         int inputLen = data.length;  
  128.         // 對資料分段加密  
  129.             if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {  
  130.                 cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);  
  131.                 cache = cipher.doFinal(data, offSet, inputLen - offSet);  
  132.             offSet = i * MAX_ENCRYPT_BLOCK;  
  133.         byte[] encryptedData = out.toByteArray();  
  134.         return encryptedData;  
  135.      * 私鑰加密  
  136.     public static byte[] encryptByPrivateKey(byte[] data, String privateKey)  
  137.         cipher.init(Cipher.ENCRYPT_MODE, privateK);  
  138.      * 擷取私鑰  
  139.      * @param keyMap  
  140.      *            密鑰對  
  141.     public static String getPrivateKey(Map<String, Object> keyMap)  
  142.         Key key = (Key) keyMap.get(PRIVATE_KEY);  
  143.         return Base64Utils.encode(key.getEncoded());  
  144.      * 擷取公鑰  
  145.     public static String getPublicKey(Map<String, Object> keyMap)  
  146.         Key key = (Key) keyMap.get(PUBLIC_KEY);  
  147. }  

Base64Utils檔案

  1. import java.io.ByteArrayInputStream;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import com.sun.org.apache.xml.internal.security.utils.Base64;    
  8.  * BASE64編碼解碼工具包  
  9.  * 依賴javabase64-1.3.1.jar  
  10.  * @date 2012-5-19  
  11. public class Base64Utils {  
  12.      * 檔案讀取緩沖區大小  
  13.     private static final int CACHE_SIZE = 1024;  
  14.      * BASE64字元串解碼為二進制資料  
  15.      * @param base64  
  16.     public static byte[] decode(String base64) throws Exception {  
  17.         return Base64.decode(base64.getBytes());  
  18.      * 二進制資料編碼為BASE64字元串  
  19.      * @param bytes  
  20.     public static String encode(byte[] bytes) throws Exception {  
  21.         return new String(Base64.encode(bytes));  
  22.      * 将檔案編碼為BASE64字元串  
  23.      * 大檔案慎用,可能會導緻記憶體溢出  
  24.      * @param filePath 檔案絕對路徑  
  25.     public static String encodeFile(String filePath) throws Exception {  
  26.         byte[] bytes = fileToByte(filePath);  
  27.         return encode(bytes);  
  28.      * BASE64字元串轉回檔案  
  29.      * @param base64 編碼字元串  
  30.     public static void decodeToFile(String filePath, String base64) throws Exception {  
  31.         byte[] bytes = decode(base64);  
  32.         byteArrayToFile(bytes, filePath);  
  33.      * 檔案轉換為二進制數組  
  34.      * @param filePath 檔案路徑  
  35.     public static byte[] fileToByte(String filePath) throws Exception {  
  36.         byte[] data = new byte[0];  
  37.         File file = new File(filePath);  
  38.         if (file.exists()) {  
  39.             FileInputStream in = new FileInputStream(file);  
  40.             ByteArrayOutputStream out = new ByteArrayOutputStream(2048);  
  41.             byte[] cache = new byte[CACHE_SIZE];  
  42.             int nRead = 0;  
  43.             while ((nRead = in.read(cache)) != -1) {  
  44.                 out.write(cache, 0, nRead);  
  45.                 out.flush();  
  46.             out.close();  
  47.             in.close();  
  48.             data = out.toByteArray();  
  49.          }  
  50.         return data;  
  51.      * 二進制資料寫檔案  
  52.      * @param bytes 二進制資料  
  53.      * @param filePath 檔案生成目錄  
  54.     public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {  
  55.         InputStream in = new ByteArrayInputStream(bytes);     
  56.         File destFile = new File(filePath);  
  57.         if (!destFile.getParentFile().exists()) {  
  58.             destFile.getParentFile().mkdirs();  
  59.         destFile.createNewFile();  
  60.         OutputStream out = new FileOutputStream(destFile);  
  61.         byte[] cache = new byte[CACHE_SIZE];  
  62.         int nRead = 0;  
  63.         while ((nRead = in.read(cache)) != -1) {     
  64.             out.write(cache, 0, nRead);  
  65.             out.flush();  
  66.         in.close();  

測試用例:

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

生成RSA密鑰、儲存到檔案、從檔案讀取、加密、解密等操作

  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. public class RSATest {     
  19.     public static void main(String[] args) {     
  20.         try {     
  21.             RSATest encrypt = new RSATest();     
  22.             String encryptText = "encryptText";     
  23.             // Generate keys     
  24.             KeyPair keyPair = encrypt.generateKey();     
  25.             RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();     
  26.             RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();     
  27.             byte[] e = encrypt.encrypt(publicKey, encryptText.getBytes());     
  28.             byte[] de = encrypt.decrypt(privateKey, e);     
  29.             System.out.println(toHexString(e));     
  30.             System.out.println(toHexString(de));     
  31.         } catch (Exception e) {     
  32.             e.printStackTrace();     
  33.         }     
  34.     }     
  35.     public KeyPair generateKey() throws NoSuchAlgorithmException {     
  36.         KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");     
  37.         keyPairGen.initialize(1024, new SecureRandom());     
  38.         KeyPair keyPair = keyPairGen.generateKeyPair();     
  39.         return keyPair;     
  40.     public void saveKey(KeyPair keyPair, String publicKeyFile,     
  41.             String privateKeyFile) throws ConfigurationException {     
  42.         PublicKey pubkey = keyPair.getPublic();     
  43.         PrivateKey prikey = keyPair.getPrivate();     
  44.         // save public key     
  45.         PropertiesConfiguration publicConfig = new PropertiesConfiguration(     
  46.                 publicKeyFile);     
  47.         publicConfig.setProperty("PULIICKEY", toHexString(pubkey.getEncoded()));     
  48.         publicConfig.save();     
  49.         // save private key     
  50.         PropertiesConfiguration privateConfig = new PropertiesConfiguration(     
  51.                 privateKeyFile);     
  52.         privateConfig.setProperty("PRIVATEKEY",     
  53.                 toHexString(prikey.getEncoded()));     
  54.         privateConfig.save();     
  55.     /**    
  56.      * @param filename    
  57.      * @param type:    
  58.      *            1-public 0-private    
  59.      * @return    
  60.      * @throws ConfigurationException    
  61.      * @throws NoSuchAlgorithmException    
  62.      * @throws InvalidKeySpecException    
  63.      */    
  64.     public Key loadKey(String filename, int type)     
  65.             throws ConfigurationException, NoSuchAlgorithmException,     
  66.             InvalidKeySpecException {     
  67.         PropertiesConfiguration config = new PropertiesConfiguration(filename);     
  68.         KeyFactory keyFactory = KeyFactory.getInstance("RSA",     
  69.                 new BouncyCastleProvider());     
  70.         if (type == 0) {     
  71.             // privateKey     
  72.             String privateKeyValue = config.getString("PULIICKEY");     
  73.             PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(     
  74.                     toBytes(privateKeyValue));     
  75.             PrivateKey privateKey = keyFactory.generatePrivate(priPKCS8);     
  76.             return privateKey;     
  77.         } else {     
  78.             // publicKey     
  79.             String privateKeyValue = config.getString("PRIVATEKEY");     
  80.             X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(     
  81.             PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);     
  82.             return publicKey;     
  83.      * Encrypt String.    
  84.      *     
  85.      * @return byte[]    
  86.     protected byte[] encrypt(RSAPublicKey publicKey, byte[] data) {     
  87.         if (publicKey != null) {     
  88.             try {     
  89.                 Cipher cipher = Cipher.getInstance("RSA",     
  90.                         new BouncyCastleProvider());     
  91.                 cipher.init(Cipher.ENCRYPT_MODE, publicKey);     
  92.                 return cipher.doFinal(data);     
  93.             } catch (Exception e) {     
  94.                 e.printStackTrace();     
  95.             }     
  96.         return null;     
  97.      * Basic decrypt method    
  98.     protected byte[] decrypt(RSAPrivateKey privateKey, byte[] raw) {     
  99.         if (privateKey != null) {     
  100.                 cipher.init(Cipher.DECRYPT_MODE, privateKey);     
  101.                 return cipher.doFinal(raw);     
  102.     public static String toHexString(byte[] b) {     
  103.         StringBuilder sb = new StringBuilder(b.length * 2);     
  104.         for (int i = 0; i < b.length; i++) {     
  105.             sb.append(HEXCHAR[(b[i] & 0xf0) >>> 4]);     
  106.             sb.append(HEXCHAR[b[i] & 0x0f]);     
  107.         return sb.toString();     
  108.     public static final byte[] toBytes(String s) {     
  109.         byte[] bytes;     
  110.         bytes = new byte[s.length() / 2];     
  111.         for (int i = 0; i < bytes.length; i++) {     
  112.             bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),     
  113.                     16);     
  114.         return bytes;     
  115.     private static char[] HEXCHAR = { '0', '1', '2', '3', '4', '5', '6', '7',     
  116.             '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };     

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

一、簡介:

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

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

  三、使用方式:

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

  ② A首先需要用自己的私鑰為發送請求資料簽名,并将公鑰一同發送給B;

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

  ④ B驗簽通過後,處理邏輯,并把處理結果傳回,傳回資料需要用A發送的公鑰進行加密(公鑰加密後,隻能用配對的私鑰解密);

  ⑤ A收到B傳回的資料,使用私鑰解密,至此,一次資料互動完成。

  四、代碼示例:

  1、第一步擷取私鑰,為簽名做準備。

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

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

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

3、B收到資料後,需要使用A提供的公鑰資訊進行驗簽,此處使用公鑰的N、E進行驗簽

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

  1. /**   
  2.      * 根據公鑰n、e生成公鑰  
  3.      * @param modulus   公鑰n串  
  4.      * @param publicExponent  公鑰e串  
  5.      * @return 傳回公鑰PublicKey  
  6.     public static PublicKey getPublickKey(String modulus, String publicExponent)  
  7.         KeySpec publicKeySpec = new RSAPublicKeySpec(  
  8.                 new BigInteger(modulus, 16), new BigInteger(publicExponent, 16));  
  9.         KeyFactory factory = KeyFactory.getInstance("RSA");  
  10.         PublicKey publicKey = factory.generatePublic(publicKeySpec);  
  11.         return publicKey;  

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

  1.      * 用公鑰證書進行驗簽  
  2.      * @param message  簽名之前的原文  
  3.      * @param cipherText  簽名  
  4.      * @param pubKeyn 公鑰n串  
  5.      * @param pubKeye 公鑰e串  
  6.      * @return boolean 驗簽成功為true,失敗為false  
  7.     public static boolean verify(String message, String cipherText,String pubKeyn,  
  8.             String pubKeye) throws Exception {  
  9.         Cipher c4 = Cipher.getInstance("RSA/ECB/PKCS1Padding");  
  10.         // 根據密鑰,對Cipher對象進行初始化,DECRYPT_MODE表示解密模式  
  11.         c4.init(Cipher.DECRYPT_MODE, getPublickKey(pubKeyn,pubKeye));  
  12.         // 解密  
  13.         byte[] desDecTextBytes = c4.doFinal(Base64.base64ToByteArray(cipherText));  
  14.         // 得到前置對原文進行的MD5  
  15.         String md5Digest1 = Base64.byteArrayToBase64(desDecTextBytes);  
  16.         md5.update(message.getBytes("utf-8"));  
  17.         // 得到商戶對原文進行的MD5  
  18.         String md5Digest2 = Base64.byteArrayToBase64(digestBytes);  
  19.         // 驗證簽名  
  20.         if (md5Digest1.equals(md5Digest2)) {  
  21.             return true;  
  22.             return false;  
  1.      * 讀取公鑰cer  
  2.      * @param path .cer檔案的路徑  如:c:/abc.cer  
  3.      * @return  base64後的公鑰串  
  4.     public static String getPublicKey(String path) throws IOException,  
  5.     CertificateException{  
  6.         InputStream inStream = new FileInputStream(path);  
  7.         int ch;  
  8.         String res = "";  
  9.         while ((ch = inStream.read()) != -1) {  
  10.             out.write(ch);  
  11.         byte[] result = out.toByteArray();  
  12.         res = Base64.byteArrayToBase64(result);  
  13.         return res;  
  14.     }