当前位置:   article > 正文

Security RSA非对称加密_spring-security-rsa

spring-security-rsa

 pom

  1. <dependency>
  2. <groupId>org.springframework.security</groupId>
  3. <artifactId>spring-security-rsa</artifactId>
  4. <version>1.0.9.RELEASE</version>
  5. </dependency>
RsaUtils
  1. package com.example.demo.util;
  2. import org.apache.tomcat.util.codec.binary.Base64;
  3. import javax.crypto.Cipher;
  4. import java.security.*;
  5. import java.security.spec.PKCS8EncodedKeySpec;
  6. import java.security.spec.X509EncodedKeySpec;
  7. /**
  8. * Created by Eleven on 2022/10/28
  9. */
  10. public class RsaUtils {
  11. private static final String algorithm = "RSA";
  12. /**
  13. * 构建RSA密钥对
  14. *
  15. * @return 生成后的公私钥信息
  16. */
  17. public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
  18. KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
  19. keyPairGenerator.initialize(1024);
  20. KeyPair keyPair = keyPairGenerator.generateKeyPair();
  21. PublicKey publicKey = keyPair.getPublic();
  22. PrivateKey privateKey = keyPair.getPrivate();
  23. String publicKeyString = Base64.encodeBase64String(publicKey.getEncoded());
  24. String privateKeyString = Base64.encodeBase64String(privateKey.getEncoded());
  25. return new RsaKeyPair(publicKeyString, privateKeyString);
  26. }
  27. /**
  28. * 公钥加密
  29. *
  30. * @param publicKeyString 公钥
  31. * @param text 待加密的文本
  32. * @return 加密后的文本
  33. */
  34. public static String encryptByPublicKey(String publicKeyString, String text) throws Exception {
  35. X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
  36. KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
  37. PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
  38. Cipher cipher = Cipher.getInstance(algorithm);
  39. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  40. byte[] result = cipher.doFinal(text.getBytes());
  41. return Base64.encodeBase64String(result);
  42. }
  43. /**
  44. * 公钥解密
  45. *
  46. * @param publicKeyString 公钥
  47. * @param text 待解密的信息
  48. * @return 解密后的文本
  49. */
  50. public static String decryptByPublicKey(String publicKeyString, String text) throws Exception {
  51. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
  52. KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
  53. PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
  54. Cipher cipher = Cipher.getInstance(algorithm);
  55. cipher.init(Cipher.DECRYPT_MODE, publicKey);
  56. byte[] result = cipher.doFinal(Base64.decodeBase64(text));
  57. return new String(result);
  58. }
  59. /**
  60. * 私钥加密
  61. *
  62. * @param privateKeyString 私钥
  63. * @param text 待加密的信息
  64. * @return 加密后的文本
  65. */
  66. public static String encryptByPrivateKey(String privateKeyString, String text) throws Exception {
  67. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
  68. KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
  69. PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
  70. Cipher cipher = Cipher.getInstance(algorithm);
  71. cipher.init(Cipher.ENCRYPT_MODE, privateKey);
  72. byte[] result = cipher.doFinal(text.getBytes());
  73. return Base64.encodeBase64String(result);
  74. }
  75. /**
  76. * 私钥解密
  77. *
  78. * @param privateKeyString 私钥
  79. * @param text 待解密的文本
  80. * @return 解密后的文本
  81. */
  82. public static String decryptByPrivateKey(String privateKeyString, String text) throws Exception {
  83. PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
  84. KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
  85. PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
  86. Cipher cipher = Cipher.getInstance(algorithm);
  87. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  88. byte[] result = cipher.doFinal(Base64.decodeBase64(text));
  89. return new String(result);
  90. }
  91. public static void main(String[] args) throws Exception {
  92. RsaUtils.RsaKeyPair rsaKeyPair = null;
  93. try {
  94. rsaKeyPair = RsaUtils.generateKeyPair();
  95. } catch (NoSuchAlgorithmException e) {
  96. e.printStackTrace();
  97. }
  98. System.out.println("公钥:");
  99. System.out.println(rsaKeyPair.getPublicKey());
  100. System.out.println("私钥:");
  101. System.out.println(rsaKeyPair.getPrivateKey());
  102. String phone = "aaaaaaaaaaaaaaa";
  103. //公钥加密,私钥解密
  104. System.out.println("公钥加密,私钥解密");
  105. String phoneRsa = RsaUtils.encryptByPublicKey(rsaKeyPair.getPublicKey(), phone);
  106. System.out.println("加密后:" + phoneRsa);
  107. String phoneRsa2 = RsaUtils.decryptByPrivateKey(rsaKeyPair.getPrivateKey(), phoneRsa);
  108. System.out.println("解密后:" + phoneRsa2);
  109. //私钥加密,公钥解密
  110. System.out.println("私钥加密,公钥解密");
  111. String phoneRsa3 = RsaUtils.encryptByPrivateKey(rsaKeyPair.getPrivateKey(), phone);
  112. System.out.println("加密后:" + phoneRsa3);
  113. // phoneRsa3 = phoneRsa3.substring(0, phoneRsa3.length() - 2);// 测试完整性, 删除2字符 是否还能解密
  114. String phoneRsa4 = RsaUtils.decryptByPublicKey(rsaKeyPair.getPublicKey(), phoneRsa3);
  115. System.out.println("解密后:" + phoneRsa4);
  116. }
  117. static class RsaKeyPair {
  118. private String publicKey;
  119. private String privateKey;
  120. public RsaKeyPair(String publicKey, String privateKey) {
  121. this.publicKey = publicKey;
  122. this.privateKey = privateKey;
  123. }
  124. public String getPublicKey() {
  125. return publicKey;
  126. }
  127. public void setPublicKey(String publicKey) {
  128. this.publicKey = publicKey;
  129. }
  130. public String getPrivateKey() {
  131. return privateKey;
  132. }
  133. public void setPrivateKey(String privateKey) {
  134. this.privateKey = privateKey;
  135. }
  136. }
  137. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/948557
推荐阅读
相关标签
  

闽ICP备14008679号