1. public class RsaUtil {
    2. private static final String KEY_ALGORITHM = "RSA";
    3. private static final String SIGNATURE_ALGORITHM = "MD5withRSA";
    4. private static final String PUBLIC_KEY = "RSAPublicKey";
    5. private static final String PRIVATE_KEY = "RSAPrivateKey";
    6. private static byte[] decryptBASE64(String key) {
    7. return Base64.getDecoder().decode(key);
    8. }
    9. public static String encryptBASE64(byte[] bytes) {
    10. return Base64.getEncoder().encodeToString(bytes);
    11. }
    12. /**
    13. * 用私钥对信息生成数字签名
    14. *
    15. * @param data 加密数据
    16. * @param privateKey 私钥
    17. * @return
    18. * @throws Exception
    19. */
    20. public static String sign(byte[] data, String privateKey) throws Exception {
    21. // 解密由base64编码的私钥
    22. byte[] keyBytes = decryptBASE64(privateKey);
    23. // 构造PKCS8EncodedKeySpec对象
    24. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    25. // KEY_ALGORITHM 指定的加密算法
    26. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    27. // 取私钥匙对象
    28. PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
    29. // 用私钥对信息生成数字签名
    30. Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    31. signature.initSign(priKey);
    32. signature.update(data);
    33. return encryptBASE64(signature.sign());
    34. }
    35. /**
    36. * 校验数字签名
    37. *
    38. * @param data 加密数据
    39. * @param publicKey 公钥
    40. * @param sign 数字签名
    41. * @return 校验成功返回true 失败返回false
    42. * @throws Exception
    43. */
    44. public static boolean verify(byte[] data, String publicKey, String sign) throws Exception {
    45. // 解密由base64编码的公钥
    46. byte[] keyBytes = decryptBASE64(publicKey);
    47. // 构造X509EncodedKeySpec对象
    48. X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
    49. // KEY_ALGORITHM 指定的加密算法
    50. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    51. // 取公钥匙对象
    52. PublicKey pubKey = keyFactory.generatePublic(keySpec);
    53. Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    54. signature.initVerify(pubKey);
    55. signature.update(data);
    56. // 验证签名是否正常
    57. return signature.verify(decryptBASE64(sign));
    58. }
    59. public static byte[] decryptByPrivateKey(byte[] data, String key) throws Exception {
    60. // 对密钥解密
    61. byte[] keyBytes = decryptBASE64(key);
    62. // 取得私钥
    63. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    64. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    65. Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
    66. // 对数据解密
    67. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    68. cipher.init(Cipher.DECRYPT_MODE, privateKey);
    69. return cipher.doFinal(data);
    70. }
    71. /**
    72. * 解密<br>
    73. * 用私钥解密
    74. *
    75. * @param data
    76. * @param key
    77. * @return
    78. * @throws Exception
    79. */
    80. public static byte[] decryptByPrivateKey(String data, String key) throws Exception {
    81. return decryptByPrivateKey(decryptBASE64(data), key);
    82. }
    83. /**
    84. * 解密<br>
    85. * 用公钥解密
    86. *
    87. * @param data
    88. * @param key
    89. * @return
    90. * @throws Exception
    91. */
    92. public static byte[] decryptByPublicKey(byte[] data, String key) throws Exception {
    93. // 对密钥解密
    94. byte[] keyBytes = decryptBASE64(key);
    95. // 取得公钥
    96. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    97. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    98. Key publicKey = keyFactory.generatePublic(x509KeySpec);
    99. // 对数据解密
    100. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    101. cipher.init(Cipher.DECRYPT_MODE, publicKey);
    102. return cipher.doFinal(data);
    103. }
    104. /**
    105. * 加密<br>
    106. * 用公钥加密
    107. *
    108. * @param data
    109. * @param key
    110. * @return
    111. * @throws Exception
    112. */
    113. public static byte[] encryptByPublicKey(String data, String key) throws Exception {
    114. // 对公钥解密
    115. byte[] keyBytes = decryptBASE64(key);
    116. // 取得公钥
    117. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    118. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    119. Key publicKey = keyFactory.generatePublic(x509KeySpec);
    120. // 对数据加密
    121. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    122. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    123. return cipher.doFinal(data.getBytes("utf-8"));
    124. }
    125. /**
    126. * 加密<br>
    127. * 用私钥加密
    128. *
    129. * @param data
    130. * @param key
    131. * @return
    132. * @throws Exception
    133. */
    134. public static byte[] encryptByPrivateKey(byte[] data, String key) throws Exception {
    135. // 对密钥解密
    136. byte[] keyBytes = decryptBASE64(key);
    137. // 取得私钥
    138. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    139. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    140. Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
    141. // 对数据加密
    142. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    143. cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    144. return cipher.doFinal(data);
    145. }
    146. /**
    147. * 取得私钥
    148. *
    149. * @param keyMap
    150. * @return
    151. * @throws Exception
    152. */
    153. public static String getPrivateKey(Map<String, Key> keyMap) throws Exception {
    154. Key key = keyMap.get(PRIVATE_KEY);
    155. return encryptBASE64(key.getEncoded());
    156. }
    157. /**
    158. * 取得公钥
    159. *
    160. * @param keyMap
    161. * @return
    162. * @throws Exception
    163. */
    164. public static String getPublicKey(Map<String, Key> keyMap) throws Exception {
    165. Key key = keyMap.get(PUBLIC_KEY);
    166. return encryptBASE64(key.getEncoded());
    167. }
    168. /**
    169. * 初始化密钥
    170. *
    171. * @return
    172. * @throws Exception
    173. */
    174. public static Map<String, Key> initKey() throws Exception {
    175. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
    176. keyPairGen.initialize(2048);
    177. KeyPair keyPair = keyPairGen.generateKeyPair();
    178. Map<String, Key> keyMap = new HashMap<String, Key>(2);
    179. // 公钥
    180. keyMap.put(PUBLIC_KEY, keyPair.getPublic());
    181. // 私钥
    182. keyMap.put(PRIVATE_KEY, keyPair.getPrivate());
    183. return keyMap;
    184. }
    185. public static void main(String[] args) throws Exception {
    186. // System.out.println(URLEncoder.encode("http://10.19.120.216:8099/idatafusion/"));
    187. // System.out.println(URLEncoder.encode("http://10.19.120.216:8099/idatafusion/","utf-8"));
    188. /* Map<String, Key> map = initKey();
    189. System.out.println(getPublicKey(map));
    190. System.out.println(getPrivateKey(map));*/
    191. // System.out.println(Base64.getEncoder().encodeToString(encryptByPublicKey("330621198809143813"
    192. // ,"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsYcEqcYeD9m3QZoDsNqTmPrbCjIGCKEd+CpV1Jgid94ilYFsnyJ+kKWMn/EfWEGx5JSQF3jrPduXclnZacg9BcoH1AWN0bfsNZOrKE9GF20/QqHkFsZk0wCBcphUagr0z5ShZn4bcRdiFSwgpXnLaEYaeL+hF69ySaY1UJPxLmZTTj63bxC7Q2u+c0GaRgaCqkiDUne7jPw6Dal66yeNFw3QS2MBRHY8+bgeGH3FHJZJQ7acbFl6+M44LniFtamfXodwiZaqSsl3FlxlGFIK68e59Qv6LJArJC5TXUZ1m9gjqrTKl7NpTeUP5Wfeo59eXY1j85hnTnDh3cTOssvWEQIDAQAB")));
    193. // System.out.println(Base64.getEncoder().encodeToString(encryptByPublicKey("Abc123@@", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyoKqrJZGLyfo7K38cBub7uR091nns0gZBReeJUwfNqqWZE/iyOV/bklr1NTrrGtdAn8wn34AX5YMsWM9s9M+aQQ5MsVH/SEPngE87Vjt17kcScIDIGgmygLooSRQhqB9ckiykyfYVkGUYCE2XTj10t+dsA57g0GxphPbdCdPze5ZPBM8HyJo/uNZRHh6xqu4Lb6qye2sHNQyz4uX/xcps7AvN2VH/j/wSvU58qnilLtJzu6OwLXUaLECyy9QEMdsy47cs+8UAA2jEvmwp/fUWv23QcudS+YfX48wNejMtWwg9rvdLGzV6ff7LjhtCOq0aBFrEkg5RDCzaVGkhR/azQIDAQAB")));
    194. }
    195. }