1. package com.lms.jdk8.rsa;
    2. import sun.security.rsa.RSAKeyPairGenerator;
    3. import javax.crypto.Cipher;
    4. import java.nio.charset.StandardCharsets;
    5. import java.security.*;
    6. import java.security.interfaces.RSAPrivateKey;
    7. import java.security.interfaces.RSAPublicKey;
    8. import java.security.spec.PKCS8EncodedKeySpec;
    9. import java.security.spec.X509EncodedKeySpec;
    10. import java.util.Base64;
    11. /**
    12. * @Author: 李孟帅
    13. * @Date: 2021-12-09 10:17
    14. * @Description:
    15. */
    16. public class RSAEncryptor {
    17. private String privateKey;
    18. private String publicKey;
    19. public RSAEncryptor() {
    20. generateKeyPair();
    21. }
    22. /**
    23. * @Author 李孟帅
    24. * @Date 2021-12-09 10:26
    25. * @Description TODO 生成密钥对
    26. */
    27. private void generateKeyPair() {
    28. // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
    29. // KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
    30. RSAKeyPairGenerator keyPairGenerator = new RSAKeyPairGenerator();
    31. // 初始化密钥对生成器,密钥大小为1024位
    32. keyPairGenerator.initialize(2034, new SecureRandom());
    33. // 生成一个密钥对,保存在keyPair中
    34. KeyPair keyPair = keyPairGenerator.generateKeyPair();
    35. RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
    36. RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公钥
    37. // 得到公钥字符串
    38. String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded());
    39. // 得到私钥字符串
    40. String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());
    41. // 将公钥和私钥保存到Map
    42. this.publicKey = publicKeyString;
    43. this.privateKey = privateKeyString;
    44. }
    45. /**
    46. * @Author 李孟帅
    47. * @Date 2021-12-09 10:32
    48. * @Description TODO 使用公钥解密
    49. */
    50. public String encrypt(String content) throws Exception {
    51. return encrypt(content, this.publicKey);
    52. }
    53. /**
    54. * @Author 李孟帅
    55. * @Date 2021-12-09 10:55
    56. * @Description TODO 使用公钥加密
    57. */
    58. public static String encrypt(String content, String publicKey) throws Exception {
    59. //base64编码的公钥
    60. byte[] publicKeyString = Base64.getDecoder().decode(publicKey);
    61. RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKeyString));
    62. //RSA加密
    63. Cipher cipher = Cipher.getInstance("RSA");
    64. cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    65. return Base64.getEncoder().encodeToString(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
    66. }
    67. /**
    68. * @Author 李孟帅
    69. * @Date 2021-12-09 10:55
    70. * @Description TODO 使用私钥解密
    71. */
    72. public String decrypt(String content) throws Exception {
    73. return decrypt(content, this.privateKey);
    74. }
    75. /**
    76. * @Author 李孟帅
    77. * @Date 2021-12-09 10:55
    78. * @Description TODO 使用私钥解密
    79. */
    80. public static String decrypt(String content, String privateKey) throws Exception {
    81. //base64编码的私钥
    82. byte[] privateKeyString = Base64.getDecoder().decode(privateKey);
    83. RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(privateKeyString));
    84. //RSA解密
    85. Cipher cipher = Cipher.getInstance("RSA");
    86. cipher.init(Cipher.DECRYPT_MODE, priKey);
    87. return new String(cipher.doFinal(Base64.getDecoder().decode(content.getBytes(StandardCharsets.UTF_8))));
    88. }
    89. /**
    90. * @Author 李孟帅
    91. * @Date 2021-12-09 11:04
    92. * @Description TODO 使用私钥签名
    93. */
    94. public String sign(String content) throws Exception {
    95. return sign(content, privateKey);
    96. }
    97. /**
    98. * @Author 李孟帅
    99. * @Date 2021-12-09 11:04
    100. * @Description TODO 使用私钥签名
    101. */
    102. public static String sign(String content, String privateKey) throws Exception {
    103. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    104. PrivateKey priKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
    105. Signature signature = Signature.getInstance("MD5WithRSA");
    106. signature.initSign(priKey);
    107. signature.update(content.getBytes(StandardCharsets.UTF_8));
    108. byte[] signed = signature.sign();
    109. return Base64.getEncoder().encodeToString(signed);
    110. }
    111. /**
    112. * @Author 李孟帅
    113. * @Date 2021-12-09 11:14
    114. * @Description TODO 验签
    115. */
    116. public static boolean verifySign(String content, String sign, String publicKey) throws Exception {
    117. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    118. PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey)));
    119. Signature signature = Signature.getInstance("MD5WithRSA");
    120. signature.initVerify(pubKey);
    121. signature.update(content.getBytes(StandardCharsets.UTF_8));
    122. return signature.verify(Base64.getDecoder().decode(sign));
    123. }
    124. /**
    125. * @Author 李孟帅
    126. * @Date 2021-12-09 10:56
    127. * @Description TODO 获取公钥
    128. */
    129. public String getPublicKey() {
    130. return publicKey;
    131. }
    132. /**
    133. * @Author 李孟帅
    134. * @Date 2021-12-09 10:56
    135. * @Description TODO 获取私钥
    136. */
    137. public String getPrivateKey() {
    138. return privateKey;
    139. }
    140. /**
    141. * @Author 李孟帅
    142. * @Date 2021-12-09 10:56
    143. * @Description TODO 测试
    144. */
    145. public static void main(String[] args) throws Exception {
    146. RSAEncryptor rsa = new RSAEncryptor();
    147. String publicKey = rsa.getPublicKey();
    148. String privateKey = rsa.getPrivateKey();
    149. System.out.println("公钥:" + publicKey);
    150. System.out.println("私钥:" + privateKey);
    151. System.out.println("数据加密测试 >========================");
    152. System.out.println();
    153. // 加密
    154. String encrypt = rsa.encrypt("中文123");
    155. System.out.println(encrypt);
    156. // 解密
    157. String decrypt = rsa.decrypt(encrypt);
    158. System.out.println(decrypt);
    159. System.out.println("模拟数据传输====》");
    160. System.out.println();
    161. String content = "2021-12-11";
    162. String encrypt1 = RSAEncryptor.encrypt(content, publicKey);
    163. System.out.println("服务端发送加密后的数据:" + encrypt1);
    164. String sign = RSAEncryptor.sign(encrypt1, privateKey);
    165. System.out.println("签名:" + sign);
    166. boolean b = RSAEncryptor.verifySign(encrypt1 , sign, publicKey);
    167. if (b) {
    168. System.out.println("客户端验证签名 成功");
    169. // 一般有两个公钥私钥对,谁发送数据就用对方的公钥加密,这里模拟的的同一个公钥私钥对
    170. content = RSAEncryptor.decrypt(encrypt1, privateKey);
    171. System.out.println("客户端解析数据:" + content);
    172. } else {
    173. System.out.println("客户端验证签名 失败");
    174. }
    175. }
    176. }