Chinaunix首页 | 论坛 | 博客
  • 博客访问: 605018
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-09-10 13:30:31


点击(此处)折叠或打开

  1. import org.apache.commons.codec.binary.Base64;
  2.  
  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.  
  10. /**
  11.  * @from fhadmin.cn
  12.  * @description Rsa 工具类,公钥私钥生成,加解密
  13.  * @date 2020-05-18
  14.  **/
  15. public class RsaUtils {
  16.  
  17.     private static final String SRC = "123456";
  18.  
  19.     public static void main(String[] args) throws Exception {
  20.         System.out.println("\n");
  21.         RsaKeyPair keyPair = generateKeyPair();
  22.         System.out.println("公钥:" + keyPair.getPublicKey());
  23.         System.out.println("私钥:" + keyPair.getPrivateKey());
  24.         System.out.println("\n");
  25.         test1(keyPair);
  26.         System.out.println("\n");
  27.         test2(keyPair);
  28.         System.out.println("\n");
  29.     }
  30.  
  31.     /**
  32.      * 公钥加密私钥解密
  33.      */
  34.     private static void test1(RsaKeyPair keyPair) throws Exception {
  35.         System.out.println("***************** 公钥加密私钥解密开始 *****************");
  36.         String text1 = encryptByPublicKey(keyPair.getPublicKey(), RsaUtils.SRC);
  37.         String text2 = decryptByPrivateKey(keyPair.getPrivateKey(), text1);
  38.         System.out.println("加密前:" + RsaUtils.SRC);
  39.         System.out.println("加密后:" + text1);
  40.         System.out.println("解密后:" + text2);
  41.         if (RsaUtils.SRC.equals(text2)) {
  42.             System.out.println("解密字符串和原始字符串一致,解密成功");
  43.         } else {
  44.             System.out.println("解密字符串和原始字符串不一致,解密失败");
  45.         }
  46.         System.out.println("***************** 公钥加密私钥解密结束 *****************");
  47.     }
  48.  
  49.     /**
  50.      * 私钥加密公钥解密
  51.      * @throws Exception /
  52.      */
  53.     private static void test2(RsaKeyPair keyPair) throws Exception {
  54.         System.out.println("***************** 私钥加密公钥解密开始 *****************");
  55.         String text1 = encryptByPrivateKey(keyPair.getPrivateKey(), RsaUtils.SRC);
  56.         String text2 = decryptByPublicKey(keyPair.getPublicKey(), text1);
  57.         System.out.println("加密前:" + RsaUtils.SRC);
  58.         System.out.println("加密后:" + text1);
  59.         System.out.println("解密后:" + text2);
  60.         if (RsaUtils.SRC.equals(text2)) {
  61.             System.out.println("解密字符串和原始字符串一致,解密成功");
  62.         } else {
  63.             System.out.println("解密字符串和原始字符串不一致,解密失败");
  64.         }
  65.         System.out.println("***************** 私钥加密公钥解密结束 *****************");
  66.     }
  67.  
  68.     /**
  69.      * 公钥解密
  70.      * @from fhadmin.cn
  71.      * @param publicKeyText 公钥
  72.      * @param text 待解密的信息
  73.      * @return /
  74.      * @throws Exception /
  75.      */
  76.     public static String decryptByPublicKey(String publicKeyText, String text) throws Exception {
  77.         X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
  78.         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  79.         PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
  80.         Cipher cipher = Cipher.getInstance("RSA");
  81.         cipher.init(Cipher.DECRYPT_MODE, publicKey);
  82.         byte[] result = cipher.doFinal(Base64.decodeBase64(text));
  83.         return new String(result);
  84.     }
  85.  
  86.     /**
  87.      * 私钥加密
  88.      * @from fhadmin.cn
  89.      * @param privateKeyText 私钥
  90.      * @param text 待加密的信息
  91.      * @return /
  92.      * @throws Exception /
  93.      */
  94.     public static String encryptByPrivateKey(String privateKeyText, String text) throws Exception {
  95.         PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
  96.         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  97.         PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
  98.         Cipher cipher = Cipher.getInstance("RSA");
  99.         cipher.init(Cipher.ENCRYPT_MODE, privateKey);
  100.         byte[] result = cipher.doFinal(text.getBytes());
  101.         return Base64.encodeBase64String(result);
  102.     }
  103.  
  104.     /**
  105.      * 私钥解密
  106.      * @from fhadmin.cn
  107.      * @param privateKeyText 私钥
  108.      * @param text 待解密的文本
  109.      * @return /
  110.      * @throws Exception /
  111.      */
  112.     public static String decryptByPrivateKey(String privateKeyText, String text) throws Exception {
  113.         PKCS8EncodedKeySpec pkcs8EncodedKeySpec5 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKeyText));
  114.         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  115.         PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5);
  116.         Cipher cipher = Cipher.getInstance("RSA");
  117.         cipher.init(Cipher.DECRYPT_MODE, privateKey);
  118.         byte[] result = cipher.doFinal(Base64.decodeBase64(text));
  119.         return new String(result);
  120.     }
  121.  
  122.     /**
  123.      * 公钥加密
  124.      * @from fhadmin.cn
  125.      * @param publicKeyText 公钥
  126.      * @param text 待加密的文本
  127.      * @return /
  128.      */
  129.     public static String encryptByPublicKey(String publicKeyText, String text) throws Exception {
  130.         X509EncodedKeySpec x509EncodedKeySpec2 = new X509EncodedKeySpec(Base64.decodeBase64(publicKeyText));
  131.         KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  132.         PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2);
  133.         Cipher cipher = Cipher.getInstance("RSA");
  134.         cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  135.         byte[] result = cipher.doFinal(text.getBytes());
  136.         return Base64.encodeBase64String(result);
  137.     }
  138.  
  139.     /**
  140.      * 构建RSA密钥对
  141.      * @from fhadmin.cn
  142.      * @return /
  143.      * @throws NoSuchAlgorithmException /
  144.      */
  145.     public static RsaKeyPair generateKeyPair() throws NoSuchAlgorithmException {
  146.         KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
  147.         keyPairGenerator.initialize(1024);
  148.         KeyPair keyPair = keyPairGenerator.generateKeyPair();
  149.         RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
  150.         RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
  151.         String publicKeyString = Base64.encodeBase64String(rsaPublicKey.getEncoded());
  152.         String privateKeyString = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
  153.         return new RsaKeyPair(publicKeyString, privateKeyString);
  154.     }
  155.  
  156.  
  157.     /**
  158.      * RSA密钥对对象
  159.      */
  160.     public static class RsaKeyPair {
  161.  
  162.         private final String publicKey;
  163.         private final String privateKey;
  164.  
  165.         public RsaKeyPair(String publicKey, String privateKey) {
  166.             this.publicKey = publicKey;
  167.             this.privateKey = privateKey;
  168.         }
  169.  
  170.         public String getPublicKey() {
  171.             return publicKey;
  172.         }
  173.  
  174.         public String getPrivateKey() {
  175.             return privateKey;
  176.         }
  177.  
  178.     }
  179. }

阅读(924) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~