From f526920f5151292b55be408c4b59f7e4a2129f12 Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期四, 14 十月 2021 10:10:01 +0800 Subject: [PATCH] 新增RSA非对称加密方法 --- src/main/java/com/hx/util/rsa/RSAUtil.java | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 120 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/hx/util/rsa/RSAUtil.java b/src/main/java/com/hx/util/rsa/RSAUtil.java new file mode 100644 index 0000000..45349a3 --- /dev/null +++ b/src/main/java/com/hx/util/rsa/RSAUtil.java @@ -0,0 +1,120 @@ +package com.hx.util.rsa; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import sun.misc.BASE64Decoder; +import sun.misc.BASE64Encoder; + +import javax.crypto.Cipher; +import java.io.IOException; +import java.security.*; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; + +public class RSAUtil { + + //log4j鏃ュ織 + private static Logger logger = LoggerFactory.getLogger(RSAUtil.class.getName()); + + /**鐢熸垚绉橀挜瀵�*/ + public static KeyPair getKeyPair() throws Exception { + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); + keyPairGenerator.initialize(2048); + KeyPair keyPair = keyPairGenerator.generateKeyPair(); + return keyPair; + } + + /**鑾峰彇鍏挜(Base64缂栫爜)*/ + public static String getPublicKey(KeyPair keyPair) { + PublicKey publicKey = keyPair.getPublic(); + byte[] bytes = publicKey.getEncoded(); + return byte2Base64(bytes); + } + + /**鑾峰彇绉侀挜(Base64缂栫爜)*/ + public static String getPrivateKey(KeyPair keyPair) { + PrivateKey privateKey = keyPair.getPrivate(); + byte[] bytes = privateKey.getEncoded(); + return byte2Base64(bytes); + } + + /**灏咮ase64缂栫爜鍚庣殑鍏挜杞崲鎴怭ublicKey瀵硅薄*/ + public static PublicKey string2PublicKey(String pubStr) throws Exception { + byte[] keyBytes = base642Byte(pubStr); + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + PublicKey publicKey = keyFactory.generatePublic(keySpec); + return publicKey; + } + + /**灏咮ase64缂栫爜鍚庣殑绉侀挜杞崲鎴怭rivateKey瀵硅薄*/ + public static PrivateKey string2PrivateKey(String priStr) throws Exception { + byte[] keyBytes = base642Byte(priStr); + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + PrivateKey privateKey = keyFactory.generatePrivate(keySpec); + return privateKey; + } + + /**鍏挜鍔犲瘑*/ + public static String publicEncrypt(String content, String publicKey) { + try { + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, string2PublicKey(publicKey)); + byte[] bytes = cipher.doFinal(content.getBytes()); + return byte2Base64(bytes); + } catch (Exception e) { + logger.error("鍏挜鍔犲瘑澶辫触{}", e); + } + return null; + } + + /**绉侀挜瑙e瘑*/ + public static String privateDecrypt(String content, String privateKey) { + try { + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.DECRYPT_MODE, string2PrivateKey(privateKey)); + byte[] bytes = cipher.doFinal(base642Byte(content)); + return new String(bytes); + } catch (Exception e) { + logger.error("绉侀挜瑙e瘑澶辫触{}", e); + } + return null; + } + + //瀛楄妭鏁扮粍杞珺ase64缂栫爜 + public static String byte2Base64(byte[] bytes) { + BASE64Encoder encoder = new BASE64Encoder(); + return encoder.encode(bytes); + } + + //Base64缂栫爜杞瓧鑺傛暟缁� + public static byte[] base642Byte(String base64Key) throws IOException { + BASE64Decoder decoder = new BASE64Decoder(); + return decoder.decodeBuffer(base64Key); + } + + public static void main(String[] args) { + try { + + KeyPair keyPair = getKeyPair(); + String publicKeyStr = getPublicKey(keyPair); + String privateKeyStr = getPrivateKey(keyPair); + System.out.println("publicKey:"+publicKeyStr); + System.out.println("privateKey:"+privateKeyStr); + + //String publicKeyStr = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCWY1VKIXGStXOMtSkb2nqV9A4V6MqlTPv8Dg9Sdxm8TZgOYJJxFAjpUOwt0au6q5JDTrslZngg9um1IhNJlRLEySbTvN7Bzeq6XOpZx5w6XRZ+7/o0Ui4YvcYwIHB5DgS5XJnLa3vLqWOk4NAtY0lqC20170mHi5Fmjdak63OTzwIDAQAB"; + //=================瀹㈡埛绔�================= + String message = "jia_he"; + //鐢ㄥ叕閽ュ姞瀵� + String byte2Base64 = RSAUtil.publicEncrypt(message, publicKeyStr); + System.out.println("鍏挜鍔犲瘑骞禕ase64缂栫爜鐨勭粨鏋滐細" + byte2Base64); + //===================鏈嶅姟绔�================ + //String privateKeyStr = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJZjVUohcZK1c4y1KRvaepX0DhXoyqVM+/wOD1J3GbxNmA5gknEUCOlQ7C3Rq7qrkkNOuyVmeCD26bUiE0mVEsTJJtO83sHN6rpc6lnHnDpdFn7v+jRSLhi9xjAgcHkOBLlcmctre8upY6Tg0C1jSWoLbTXvSYeLkWaN1qTrc5PPAgMBAAECgYAJuQBRm5npHzwKM8glmdllCnNCrVs0lqaP5CTPcw3B485Z15qAHwh4dRff2ndcySzalyN4RoirsOrpH/vZPP8KinIhOT9zcHInWMKEPqGH+twB+c0hS6x2YZFuJqW3+zy56jnUMn3MDjNF5A5N9hD6taP1V+UOqgZvYwwMSCFLkQJBANZtQS2AqahHNjPgjkWcuaG8zXzgbu0VeU+wXDjxR81aLLJBOK6AGe7w5yJnip2w/FqGxPfORcn/bLxyDHOhpQcCQQCzi5zeeiXt1cxeGGqVxNvC51PuSna9YnPs+phiwwGVdAqVdMOJzsThs5EDVhX4eQYIeA4B6PItiPLHsw+6AXD5AkAp/ac/4+xVeeyRaC40T6bCl5ieFc1jPEtPYbgNpqJrAneySLdy5L8vXZnF0QUCMICasb2s0YY1MoH2vVbW5hbNAkEAsCxD5oFQikiI2aN3ojGhuWMnFeB3Fmlueo+ByxaxjSZp5DDIVYZP5W8+0Vk9Aawu4Ux74h/i0g9Yud7XhZo4cQJARyq8WJGDawo65CVcQQ2opbL8LqApr7Co4CAKmV4YFDraY00q9h1Dbj7WO+urJz7XUqbEYG0Yga+37jQAnQHUUQ=="; + //瑙e瘑鍚庣殑鏄庢枃 + System.out.println("瑙e瘑鍚庣殑鏄庢枃: " +RSAUtil.privateDecrypt(byte2Base64, privateKeyStr)); + } catch (Exception e) { + e.printStackTrace(); + } + } +} -- Gitblit v1.8.0