当前位置:   article > 正文

Java实现RSA加密算法_java实现rsa公钥加密

java实现rsa公钥加密
  1. package Security1;
  2. import java.math.BigInteger;
  3. import java.security.KeyFactory;
  4. import java.security.KeyPair;
  5. import java.security.KeyPairGenerator;
  6. import java.security.NoSuchAlgorithmException;
  7. import java.security.PrivateKey;
  8. import java.security.PublicKey;
  9. import java.security.interfaces.RSAPrivateKey;
  10. import java.security.interfaces.RSAPublicKey;
  11. import java.security.spec.InvalidKeySpecException;
  12. import java.security.spec.PKCS8EncodedKeySpec;
  13. import java.security.spec.RSAPrivateKeySpec;
  14. import java.security.spec.RSAPublicKeySpec;
  15. import java.security.spec.X509EncodedKeySpec;
  16. import java.util.HashMap;
  17. import javax.crypto.Cipher;
  18. import sun.misc.BASE64Decoder;
  19. import sun.misc.BASE64Encoder;
  20. /*
  21. * 公钥(N,e)
  22. * 私钥(N,d)
  23. */
  24. public class RSAdemo {
  25. private static HashMap<String,String> map = new HashMap<String,String>();
  26. public static void main(String[] args) throws InvalidKeySpecException {
  27. try {
  28. //创建RSA加密
  29. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
  30. //密钥位数
  31. keyPairGen.initialize(1024);
  32. // 动态生成密钥对,这是当前最耗时的操作,一般要2s以上。
  33. KeyPair key = keyPairGen.generateKeyPair();
  34. //公钥
  35. PublicKey publicKey = key.getPublic();
  36. //密钥
  37. PrivateKey privateKey = key.getPrivate();
  38. printPrivateKey(privateKey);
  39. printPublicKey(publicKey);
  40. // 公钥比特编码
  41. // byte[] privateData = privateKey.getEncoded(); //对应着getPrivateKey()方法
  42. //密钥比特编码
  43. // byte[] publicData = publicKey.getEncoded(); //对应着getPublicKey()方法
  44. String message ="吕s,你已经不是我的女朋友";
  45. String inputStr = encrypt(message, new String(new BASE64Encoder().encodeBuffer(publicKey.getEncoded())));
  46. System.out.println(inputStr);
  47. System.out.println(decrypt(inputStr, new String(new BASE64Encoder().encodeBuffer(privateKey.getEncoded()))));
  48. } catch (NoSuchAlgorithmException e) {
  49. // TODO Auto-generated catch block
  50. e.printStackTrace();
  51. } catch (Exception e) {
  52. // TODO Auto-generated catch block
  53. e.printStackTrace();
  54. }
  55. }
  56. // 通过公钥byte[]将公钥还原,适用于RSA算法
  57. public static PublicKey getPublicKey(byte[] keyBytes) throws NoSuchAlgorithmException,InvalidKeySpecException {
  58. X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
  59. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  60. PublicKey publicKey = keyFactory.generatePublic(keySpec);
  61. return publicKey;
  62. }
  63. //通过私钥byte[]将密钥还原,适用于RSA算法
  64. public static PrivateKey getPrivateKey(byte[] keyBytes) throws NoSuchAlgorithmException,InvalidKeySpecException{
  65. PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
  66. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  67. PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
  68. return privateKey;
  69. }
  70. //打印公钥信息
  71. public static void printPublicKey(PublicKey publicKey) {
  72. RSAPublicKey key = (RSAPublicKey)publicKey;
  73. map.put("N", key.getModulus().toString());
  74. map.put("E", key.getPublicExponent().toString());
  75. }
  76. //获取私钥信息
  77. public static void printPrivateKey(PrivateKey privateKey) {
  78. RSAPrivateKey key = (RSAPrivateKey)privateKey;
  79. map.put("D", key.getPrivateExponent().toString());
  80. }
  81. // 使用N、E值还原公钥
  82. public static PublicKey getPublicKeyByN_E(String N,String E) throws NoSuchAlgorithmException, InvalidKeySpecException {
  83. BigInteger bigN = new BigInteger(N);
  84. BigInteger bigE = new BigInteger(E);
  85. RSAPublicKeySpec spec = new RSAPublicKeySpec(bigN, bigE);
  86. KeyFactory factory = KeyFactory.getInstance("RSA");
  87. return factory.generatePublic(spec);
  88. }
  89. // 使用N、D值还原公钥
  90. public static PrivateKey getPrivateKeyByN_D(String N,String D) throws NoSuchAlgorithmException, InvalidKeySpecException {
  91. BigInteger bigN = new BigInteger(N);
  92. BigInteger bigD = new BigInteger(D);
  93. RSAPrivateKeySpec spec = new RSAPrivateKeySpec(bigN, bigD);
  94. KeyFactory factory = KeyFactory.getInstance("RSA");
  95. return factory.generatePrivate(spec);
  96. }
  97. //加密
  98. /*
  99. * 公钥加密
  100. * @Param str : 加密字符串
  101. * @Param publicKey : 公钥
  102. * return : 密文
  103. */
  104. public static String encrypt(String str , String publicKey) throws Exception {
  105. //base64编码的公钥
  106. byte[] decoded = new BASE64Decoder().decodeBuffer(publicKey);
  107. X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decoded);
  108. RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(keySpec);
  109. //RSA加密
  110. Cipher cipher = Cipher.getInstance("RSA");
  111. cipher.init(Cipher.ENCRYPT_MODE, pubKey);
  112. String outStr = new BASE64Encoder().encode(cipher.doFinal(str.getBytes("UTF-8")));
  113. return outStr;
  114. }
  115. /*
  116. * RSA私钥解密
  117. * @param str :加密后的字符串
  118. * @param privateKey : 解密后的字符串
  119. */
  120. public static String decrypt(String str,String privateKey) throws Exception{
  121. //64位解码加密后的字符串
  122. byte[] inputStr = new BASE64Decoder().decodeBuffer(new String(str.getBytes("utf-8"),"utf-8"));
  123. //base64解码的私钥
  124. byte[] decode = new BASE64Decoder().decodeBuffer(privateKey);
  125. PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decode);
  126. RSAPrivateKey priKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(keySpec);
  127. Cipher cipher = Cipher.getInstance("RSA");
  128. cipher.init(Cipher.DECRYPT_MODE, priKey);
  129. String outStr = new String(cipher.doFinal(inputStr));
  130. return outStr;
  131. }
  132. }

参考https://blog.csdn.net/Trustauth/article/details/79095675

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

闽ICP备14008679号