RSA.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //package com.huimv.management.rsa;/*
  2. // --------------------------------------------**********--------------------------------------------
  3. //
  4. // 该算法于1977年由美国麻省理工学院MIT(Massachusetts Institute of Technology)的Ronal Rivest,Adi Shamir和Len Adleman三位年轻教授提出,并以三人的姓氏Rivest,Shamir和Adlernan命名为RSA算法,是一个支持变长密钥的公共密钥算法,需要加密的文件快的长度也是可变的!
  5. //
  6. // 所谓RSA加密算法,是世界上第一个非对称加密算法,也是数论的第一个实际应用。它的算法如下:
  7. //
  8. // 1.找两个非常大的质数p和q(通常p和q都有155十进制位或都有512十进制位)并计算n=pq,k=(p-1)(q-1)。
  9. //
  10. // 2.将明文编码成整数M,保证M不小于0但是小于n。
  11. //
  12. // 3.任取一个整数e,保证e和k互质,而且e不小于0但是小于k。加密钥匙(称作公钥)是(e, n)。
  13. //
  14. // 4.找到一个整数d,使得ed除以k的余数是1(只要e和n满足上面条件,d肯定存在)。解密钥匙(称作密钥)是(d, n)。
  15. //
  16. // 加密过程: 加密后的编码C等于M的e次方除以n所得的余数。
  17. //
  18. // 解密过程: 解密后的编码N等于C的d次方除以n所得的余数。
  19. //
  20. // 只要e、d和n满足上面给定的条件。M等于N。
  21. //
  22. // --------------------------------------------**********--------------------------------------------
  23. // */
  24. //import lombok.extern.slf4j.Slf4j;
  25. //
  26. //import java.math.BigInteger;
  27. //import java.security.Key;
  28. //import java.security.KeyFactory;
  29. //import java.security.KeyPair;
  30. //import java.security.KeyPairGenerator;
  31. //import java.security.PrivateKey;
  32. //import java.security.PublicKey;
  33. //import java.security.SecureRandom;
  34. //import java.security.Signature;
  35. //import java.security.interfaces.RSAPublicKey;
  36. //import java.security.spec.PKCS8EncodedKeySpec;
  37. //import java.security.spec.X509EncodedKeySpec;
  38. //import java.util.HashMap;
  39. //import java.util.Map;
  40. //
  41. //import javax.crypto.Cipher;
  42. //
  43. //@Slf4j
  44. //public class RSA {
  45. //
  46. // /** 指定key的大小 */
  47. // private static int KEYSIZE = 2048;
  48. // /**
  49. // * 生成密钥对
  50. // */
  51. // public static Map<String, String> generateKeyPair() throws Exception {
  52. // /** RSA算法要求有一个可信任的随机数源 */
  53. // SecureRandom sr = new SecureRandom();
  54. // /** 为RSA算法创建一个KeyPairGenerator对象 */
  55. // KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  56. // /** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */
  57. // kpg.initialize(KEYSIZE, sr);
  58. // /** 生成密匙对 */
  59. // KeyPair kp = kpg.generateKeyPair();
  60. // /** 得到公钥 */
  61. // Key publicKey = kp.getPublic();
  62. // byte[] publicKeyBytes = publicKey.getEncoded();
  63. // String pub = new String(Base64Sign.encodeBase64(publicKeyBytes),
  64. // ConfigureEncryptAndDecrypt.CHAR_ENCODING);
  65. // /** 得到私钥 */
  66. // Key privateKey = kp.getPrivate();
  67. // byte[] privateKeyBytes = privateKey.getEncoded();
  68. // String pri = new String(Base64Sign.encodeBase64(privateKeyBytes),
  69. // ConfigureEncryptAndDecrypt.CHAR_ENCODING);
  70. //
  71. // Map<String, String> map = new HashMap<String, String>();
  72. // map.put("publicKey", pub);
  73. // map.put("privateKey", pri);
  74. // RSAPublicKey rsp = (RSAPublicKey) kp.getPublic();
  75. // BigInteger bint = rsp.getModulus();
  76. // byte[] b = bint.toByteArray();
  77. // byte[] deBase64Value = Base64Sign.encodeBase64(b);
  78. // String retValue = new String(deBase64Value);
  79. // map.put("modulus", retValue);
  80. // return map;
  81. // }
  82. //
  83. // /**
  84. // * 加密方法 source: 源数据
  85. // */
  86. // public static String encrypt(String source, String publicKey)
  87. // throws Exception {
  88. // Key key = getPublicKey(publicKey);
  89. // /** 得到Cipher对象来实现对源数据的RSA加密 */
  90. // Cipher cipher = Cipher.getInstance(ConfigureEncryptAndDecrypt.RSA_ALGORITHM);
  91. // cipher.init(Cipher.ENCRYPT_MODE, key);
  92. // byte[] b = source.getBytes();
  93. // /** 执行加密操作 */
  94. // byte[] b1 = cipher.doFi nal(b);
  95. // return new String(Base64Sign.encodeBase64(b1),
  96. // ConfigureEncryptAndDecrypt.CHAR_ENCODING);
  97. // }
  98. //
  99. // /**
  100. // * 解密算法 cryptograph:密文
  101. // */
  102. // public static String decrypt(String cryptograph, String privateKey)
  103. // throws Exception {
  104. // Key key = getPrivateKey(privateKey);
  105. // /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */
  106. // Cipher cipher = Cipher.getInstance(ConfigureEncryptAndDecrypt.RSA_ALGORITHM);
  107. // cipher.init(Cipher.DECRYPT_MODE, key);
  108. // byte[] b1 = Base64Sign.decodeBase64(cryptograph.getBytes());
  109. // /** 执行解密操作 */
  110. // byte[] b = cipher.doFinal(b1);
  111. // return new String(b);
  112. // }
  113. //
  114. // /**
  115. // * 得到公钥
  116. // *
  117. // * @param key
  118. // * 密钥字符串(经过base64编码)
  119. // * @throws Exception
  120. // */
  121. // public static PublicKey getPublicKey(String key) throws Exception {
  122. // X509EncodedKeySpec keySpec = new X509EncodedKeySpec(
  123. // Base64Sign.decodeBase64(key.getBytes()));
  124. // KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  125. // PublicKey publicKey = keyFactory.generatePublic(keySpec);
  126. // return publicKey;
  127. // }
  128. //
  129. // /**
  130. // * 得到私钥
  131. // *
  132. // * @param key
  133. // * 密钥字符串(经过base64编码)
  134. // * @throws Exception
  135. // */
  136. // public static PrivateKey getPrivateKey(String key) throws Exception {
  137. // PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
  138. // Base64Sign.decodeBase64(key.getBytes()));
  139. // KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  140. // PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
  141. // return privateKey;
  142. // }
  143. //
  144. // public static String sign(String content, String privateKey) {
  145. // String charset = ConfigureEncryptAndDecrypt.CHAR_ENCODING;
  146. // try {
  147. // PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(
  148. // Base64Sign.decodeBase64(privateKey.getBytes()));
  149. // KeyFactory keyf = KeyFactory.getInstance("RSA");
  150. // PrivateKey priKey = keyf.generatePrivate(priPKCS8);
  151. //
  152. // Signature signature = Signature.getInstance("SHA256WithRSA");
  153. //
  154. // signature.initSign(priKey);
  155. // signature.update(content.getBytes(charset));
  156. //
  157. // byte[] signed = signature.sign();
  158. //
  159. // return new String(Base64Sign.encodeBase64(signed));
  160. // } catch (Exception e) {
  161. //
  162. // }
  163. //
  164. // return null;
  165. // }
  166. //
  167. // public static boolean checkSign(String content, String sign, String publicKey)
  168. // {
  169. // try
  170. // {
  171. // KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  172. // byte[] encodedKey = Base64Sign.decode2(publicKey);
  173. // PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
  174. //
  175. //
  176. // java.security.Signature signature = java.security.Signature
  177. // .getInstance("SHA256WithRSA");
  178. //
  179. // signature.initVerify(pubKey);
  180. // signature.update( content.getBytes("utf-8") );
  181. //
  182. // boolean bverify = signature.verify( Base64Sign.decode2(sign) );
  183. // return bverify;
  184. //
  185. // }
  186. // catch (Exception e)
  187. // {
  188. // log.error("RSA-checkSign Exception", e);
  189. // }
  190. //
  191. // return false;
  192. // }
  193. //
  194. // public static void main(String[] args) throws Exception {
  195. //
  196. // for(String key : generateKeyPair().keySet()) {
  197. // System.out.println(key + ":");
  198. // System.out.println(generateKeyPair().get(key));
  199. // }
  200. // }
  201. //}