RsaMima.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.ruoyi.app.utils;
  2. import org.apache.commons.codec.binary.Base64;
  3. import javax.crypto.Cipher;
  4. import java.security.*;
  5. import java.security.interfaces.RSAPrivateKey;
  6. import java.security.interfaces.RSAPublicKey;
  7. import java.security.spec.PKCS8EncodedKeySpec;
  8. import java.security.spec.X509EncodedKeySpec;
  9. public class RsaMima {
  10. // Rsa 私钥
  11. public static String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAqhHyZfSsYourNxaY"
  12. + "7Nt+PrgrxkiA50efORdI5U5lsW79MmFnusUA355oaSXcLhu5xxB38SMSyP2KvuKN"
  13. + "PuH3owIDAQABAkAfoiLyL+Z4lf4Myxk6xUDgLaWGximj20CUf+5BKKnlrK+Ed8gA"
  14. + "kM0HqoTt2UZwA5E2MzS4EI2gjfQhz5X28uqxAiEA3wNFxfrCZlSZHb0gn2zDpWow"
  15. + "cSxQAgiCstxGUoOqlW8CIQDDOerGKH5OmCJ4Z21v+F25WaHYPxCFMvwxpcw99Ecv"
  16. + "DQIgIdhDTIqD2jfYjPTY8Jj3EDGPbH2HHuffvflECt3Ek60CIQCFRlCkHpi7hthh"
  17. + "YhovyloRYsM+IS9h/0BzlEAuO0ktMQIgSPT3aFAgJYwKpqRYKlLDVcflZFCKY7u3"
  18. + "UP8iWi1Qw0Y=";
  19. /**
  20. * 私钥解密
  21. *
  22. * @param text 待解密的文本
  23. * @return 解密后的文本
  24. */
  25. public String decryptByPrivateKey(String text) throws Exception
  26. {
  27. return decryptByPrivateKey(privateKey, text);
  28. }
  29. /**
  30. * 公钥解密
  31. *
  32. * @param publicKeyString 公钥
  33. * @param text 待解密的信息
  34. * @return 解密后的文本
  35. */
  36. public static String decryptByPublicKey(String publicKeyString, String text) throws Exception
  37. {
  38. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
  39. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  40. PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
  41. Cipher cipher = Cipher.getInstance("RSA");
  42. cipher.init(Cipher.DECRYPT_MODE, publicKey);
  43. byte[] result = cipher.doFinal(Base64.decodeBase64(text));
  44. return new String(result);
  45. }
  46. /**
  47. * 私钥加密
  48. *
  49. * @param privateKeyString 私钥
  50. * @param text 待加密的信息
  51. * @return 加密后的文本
  52. */
  53. public static String encryptByPrivateKey(String privateKeyString, String text) throws Exception
  54. {
  55. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
  56. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  57. PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
  58. Cipher cipher = Cipher.getInstance("RSA");
  59. cipher.init(Cipher.ENCRYPT_MODE, privateKey);
  60. byte[] result = cipher.doFinal(text.getBytes());
  61. return Base64.encodeBase64String(result);
  62. }
  63. /**
  64. * 私钥解密
  65. *
  66. * @param privateKeyString 私钥
  67. * @param text 待解密的文本
  68. * @return 解密后的文本
  69. */
  70. public static String decryptByPrivateKey(String privateKeyString, String text) throws Exception
  71. {
  72. PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyString));
  73. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  74. PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
  75. Cipher cipher = Cipher.getInstance("RSA");
  76. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  77. byte[] result = cipher.doFinal(Base64.decodeBase64(text));
  78. return new String(result);
  79. }
  80. /**
  81. * 公钥加密
  82. *
  83. * @param publicKeyString 公钥
  84. * @param text 待加密的文本
  85. * @return 加密后的文本
  86. */
  87. public static String encryptByPublicKey(String publicKeyString, String text) throws Exception
  88. {
  89. X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyString));
  90. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  91. PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
  92. Cipher cipher = Cipher.getInstance("RSA");
  93. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  94. byte[] result = cipher.doFinal(text.getBytes());
  95. return Base64.encodeBase64String(result);
  96. }
  97. /**
  98. * 构建RSA密钥对
  99. *
  100. * @return 生成后的公私钥信息
  101. */
  102. public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException
  103. {
  104. KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
  105. keyPairGenerator.initialize(1024);
  106. KeyPair keyPair = keyPairGenerator.generateKeyPair();
  107. RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
  108. RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
  109. String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
  110. String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
  111. return new RsaKeyPair(publicKeyString, privateKeyString);
  112. }
  113. /**
  114. * RSA密钥对对象
  115. */
  116. public static class RsaKeyPair
  117. {
  118. private final String publicKey;
  119. private final String privateKey;
  120. public RsaKeyPair(String publicKey, String privateKey)
  121. {
  122. this.publicKey = publicKey;
  123. this.privateKey = privateKey;
  124. }
  125. public String getPublicKey()
  126. {
  127. return publicKey;
  128. }
  129. public String getPrivateKey()
  130. {
  131. return privateKey;
  132. }
  133. }
  134. }