当前位置:   article > 正文

java实现RSA的简单加密解密_zhcvh

zhcvh

RSAUtil

 

  1. package com.zhuyun.rsa;
  2. import java.io.IOException;
  3. import java.security.KeyFactory;
  4. import java.security.KeyPair;
  5. import java.security.KeyPairGenerator;
  6. import java.security.PrivateKey;
  7. import java.security.PublicKey;
  8. import java.security.spec.PKCS8EncodedKeySpec;
  9. import java.security.spec.X509EncodedKeySpec;
  10. import javax.crypto.Cipher;
  11. import sun.misc.BASE64Decoder;
  12. import sun.misc.BASE64Encoder;
  13. public class RSAUtil {
  14. //生成秘钥对
  15. public static KeyPair getKeyPair() throws Exception {
  16. KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
  17. keyPairGenerator.initialize(2048);
  18. KeyPair keyPair = keyPairGenerator.generateKeyPair();
  19. return keyPair;
  20. }
  21. //获取公钥(Base64编码)
  22. public static String getPublicKey(KeyPair keyPair){
  23. PublicKey publicKey = keyPair.getPublic();
  24. byte[] bytes = publicKey.getEncoded();
  25. return byte2Base64(bytes);
  26. }
  27. //获取私钥(Base64编码)
  28. public static String getPrivateKey(KeyPair keyPair){
  29. PrivateKey privateKey = keyPair.getPrivate();
  30. byte[] bytes = privateKey.getEncoded();
  31. return byte2Base64(bytes);
  32. }
  33. //将Base64编码后的公钥转换成PublicKey对象
  34. public static PublicKey string2PublicKey(String pubStr) throws Exception{
  35. byte[] keyBytes = base642Byte(pubStr);
  36. X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
  37. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  38. PublicKey publicKey = keyFactory.generatePublic(keySpec);
  39. return publicKey;
  40. }
  41. //将Base64编码后的私钥转换成PrivateKey对象
  42. public static PrivateKey string2PrivateKey(String priStr) throws Exception{
  43. byte[] keyBytes = base642Byte(priStr);
  44. PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
  45. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  46. PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
  47. return privateKey;
  48. }
  49. //公钥加密
  50. public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{
  51. Cipher cipher = Cipher.getInstance("RSA");
  52. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  53. byte[] bytes = cipher.doFinal(content);
  54. return bytes;
  55. }
  56. //私钥解密
  57. public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{
  58. Cipher cipher = Cipher.getInstance("RSA");
  59. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  60. byte[] bytes = cipher.doFinal(content);
  61. return bytes;
  62. }
  63. //字节数组转Base64编码
  64. public static String byte2Base64(byte[] bytes){
  65. BASE64Encoder encoder = new BASE64Encoder();
  66. return encoder.encode(bytes);
  67. }
  68. //Base64编码转字节数组
  69. public static byte[] base642Byte(String base64Key) throws IOException{
  70. BASE64Decoder decoder = new BASE64Decoder();
  71. return decoder.decodeBuffer(base64Key);
  72. }
  73. }

 

 

 

 

 

TestRSA

 

  1. package com.zhuyun.test;
  2. import java.security.KeyPair;
  3. import java.security.PrivateKey;
  4. import java.security.PublicKey;
  5. import org.junit.Test;
  6. import com.zhuyun.rsa.RSAUtil;
  7. public class TestRSA {
  8. @Test
  9. public void testRSA(){
  10. try {
  11. //===============生成公钥和私钥,公钥传给客户端,私钥服务端保留==================
  12. //生成RSA公钥和私钥,并Base64编码
  13. KeyPair keyPair = RSAUtil.getKeyPair();
  14. String publicKeyStr = RSAUtil.getPublicKey(keyPair);
  15. String privateKeyStr = RSAUtil.getPrivateKey(keyPair);
  16. System.out.println("RSA公钥Base64编码:" + publicKeyStr);
  17. System.out.println("RSA私钥Base64编码:" + privateKeyStr);
  18. //=================客户端=================
  19. //hello, i am infi, good night!加密
  20. String message = "hello, i am infi, good night!";
  21. //将Base64编码后的公钥转换成PublicKey对象
  22. PublicKey publicKey = RSAUtil.string2PublicKey(publicKeyStr);
  23. //用公钥加密
  24. byte[] publicEncrypt = RSAUtil.publicEncrypt(message.getBytes(), publicKey);
  25. //加密后的内容Base64编码
  26. String byte2Base64 = RSAUtil.byte2Base64(publicEncrypt);
  27. System.out.println("公钥加密并Base64编码的结果:" + byte2Base64);
  28. //############## 网络上传输的内容有Base64编码后的公钥 和 Base64编码后的公钥加密的内容 #################
  29. //===================服务端================
  30. //将Base64编码后的私钥转换成PrivateKey对象
  31. PrivateKey privateKey = RSAUtil.string2PrivateKey(privateKeyStr);
  32. //加密后的内容Base64解码
  33. byte[] base642Byte = RSAUtil.base642Byte(byte2Base64);
  34. //用私钥解密
  35. byte[] privateDecrypt = RSAUtil.privateDecrypt(base642Byte, privateKey);
  36. //解密后的明文
  37. System.out.println("解密后的明文: " + new String(privateDecrypt));
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }

 

 

 

 

 

测试结果如下:

 

  1. RSA公钥Base64编码:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA20p3Q88SmVUPpA+r/0hWY/LbMCsllwXK
  2. UrXY13pG26BKsgtMm6IAGr6psS8cQ+AU75RpH3lTtt81wIfCHm9+vo4kVt66y8G0kfYSJ9Qlw2oH
  3. 5lwab8knPPb+NQNwE7WvceRczKh4XIEfNcgXZyKrcDUIpIsP6rNbZ70JYRhYiagqyLsb39ib4eO6
  4. Zf6nmnrl3NPGeW4qFoivNudAld5E04gt2vCEEpuVt3x1/W3lVzD2gzz9rfkzjFvWwfGYjHMcZZNH
  5. Z1h1G60ssSHh7WKV8f6EglPRQssimUKXX4De6g5SVABPs6MEOj0RYYyunJ8unA448u7/TCcMjV/b
  6. vnndDwIDAQAB
  7. RSA私钥Base64编码:MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDbSndDzxKZVQ+kD6v/SFZj8tsw
  8. KyWXBcpStdjXekbboEqyC0ybogAavqmxLxxD4BTvlGkfeVO23zXAh8Ieb36+jiRW3rrLwbSR9hIn
  9. 1CXDagfmXBpvySc89v41A3ATta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+FOlC7new6uqhGE
  23. UhXqnZxfVFhOg4r4hm6nWo9+n+oBkVLtklwTDFKMTLsSURURYCZwM76FewKBgF6T7mMs98jdAfYx
  24. 06yWIKtvVlc8Au0XdHfmolxYUScn3x9CN1a3f2ycxbj45y3r+Lvu+rqhk1WQ1X6py1S2mPnjxg/n
  25. lnrIDpaZ6EB7fBtsJQGgJ/tK3R30vEntv+Zv4M5rRmIxmpG6YyCac3Tx50gZYuCUAXz52qFUiwQR
  26. 25KRAoGAKiM+cNZLGq6ksesjvC5jpekZMAaiFwZa1EmYkDQ7MZeRBm8whOok2/h8nmw4uJVboipI
  27. K7qjqSaHQrMJNLhNMZ0BsOR/I0vJkPuqKI38QHVxRMnaqWb/KXljqY+/b6PQ16RYjjN7V6+/IkOw
  28. 9Cks8paAHqEwho2Z/Q8sntNbyVQ=
  29. 公钥加密并Base64编码的结果:rbHqJI9jlPLj/n4VP7d4jJRG1WDsDoWYib4BGjXEep6ym9FLiuCKCEwTDPuvvRzlH2BVFjkA0nN+
  30. cjDo/u4PPofxK0a8Ipz0xYvnnQc9q+k8r0QD1uTx8PmnZLEv4rjwfIra/ho8VpYkPA7F1KkwDTyI
  31. RIL06hngkhCi1aDym5oIP4vv+kxvi1uArVU04afXo6K4MTtzAesbG8f/qLHV7ywcBVNvb0QnuLNX
  32. 7jAWtfHfEGQibuA1EKbh35RcTjKlSW6vHTmg/f3fdHAfwKM/eh9LdMUjpsmfUoFfm8Ep7avjXbn+
  33. 12aXhtVZy8EYLIf1ZckefM95F808XN4HIsth6A==
  34. 解密后的明文: hello, i am infi, good night!

 

 

 

 

 

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

闽ICP备14008679号