From 1faaefb870f1d32189a2ae8a998910494aa03ced Mon Sep 17 00:00:00 2001 From: MaxKey Date: Mon, 21 Feb 2022 15:21:44 +0800 Subject: [PATCH] =?UTF-8?q?RSAUtils=20=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit RSAUtils 注释 springVersion =5.3.16 version =3.3.3 --- gradle.properties | 4 +- .../main/java/org/maxkey/crypto/RSAUtils.java | 146 ++++++++++++++---- .../java/org/maxkey/crypto/RSAUtilsTest.java | 7 +- .../src/main/resources/application.yml | 2 +- .../src/main/resources/application.properties | 2 +- .../src/main/resources/application.properties | 4 +- .../src/main/resources/application.properties | 4 +- 7 files changed, 127 insertions(+), 42 deletions(-) diff --git a/gradle.properties b/gradle.properties index 6c4522c37..baf7176e0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ #maxkey properties group =maxkey.top -version =3.3.2 +version =3.3.3 vendor =https://www.maxkey.top author =MaxKeyTop @@ -44,7 +44,7 @@ poiVersion =5.1.0 tomcatVersion =9.0.58 tomcatembedloggingjuliVersion =8.5.2 #spring -springVersion =5.3.15 +springVersion =5.3.16 springBootVersion =2.6.3 springSecurityVersion =5.6.1 springDataVersion =2.6.1 diff --git a/maxkey-common/src/main/java/org/maxkey/crypto/RSAUtils.java b/maxkey-common/src/main/java/org/maxkey/crypto/RSAUtils.java index ed954eeb6..706c1231a 100644 --- a/maxkey-common/src/main/java/org/maxkey/crypto/RSAUtils.java +++ b/maxkey-common/src/main/java/org/maxkey/crypto/RSAUtils.java @@ -32,20 +32,29 @@ import java.util.Map; import javax.crypto.Cipher; +/** + * @author shiming + * + */ public final class RSAUtils { - public static final String KEY_ALGORTHM = "RSA"; + public static final String KEY_ALGORTHM = "RSA"; - public static final String PUBLIC_KEY = "RSAPublicKey"; + public static final String PUBLIC_KEY = "RSAPublicKey"; - public static final String PRIVATE_KEY = "RSAPrivateKey"; + public static final String PRIVATE_KEY = "RSAPrivateKey"; - public static final int BASE64ARRAY_SIZE = 64; + public static final int KEY_SIZE = 1024; + + public static final int PEM_ARRAY_SIZE = 64; + /** + * 生成KEY_SIZE长度的RSA密钥对,存放在keyMap中 + * @return keyMap RSA密钥对 + * @throws Exception + */ public static Map genKeyPair() throws Exception { - KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORTHM); - keyPairGenerator.initialize(1024); - KeyPair keyPair = keyPairGenerator.generateKeyPair(); + KeyPair keyPair = genRSAKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); @@ -56,29 +65,64 @@ public final class RSAUtils { return keyMap; } + + /** + * gen RSA KeyPair + * @return KeyPair + * @throws Exception + */ + public static KeyPair genRSAKeyPair() throws Exception { + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORTHM); + keyPairGenerator.initialize(KEY_SIZE); + return keyPairGenerator.generateKeyPair(); + } + /** + * 获取公钥 + * @param keyMap + * @return 公钥 + * @throws Exception + */ public static byte[] getPublicKey(Map keyMap)throws Exception { Key key = (Key) keyMap.get(PUBLIC_KEY); return key.getEncoded(); } + /** + * 获取私钥 + * @param keyMap + * @return 私钥 + * @throws Exception + */ public static byte[] getPrivateKey(Map keyMap)throws Exception { Key key = (Key) keyMap.get(PRIVATE_KEY); return key.getEncoded(); } + /** + * 公钥数据转换为Hex字符串 + * @param keyMap + * @return 公钥 + * @throws Exception + */ public static String getPublicKey2Hex(Map keyMap)throws Exception { return HexUtils.bytes2HexString(getPublicKey(keyMap)); } + /** + * 私钥数据转换为Hex字符串 + * @param keyMap + * @return 私钥 + * @throws Exception + */ public static String getPrivateKey2Hex(Map keyMap)throws Exception { return HexUtils.bytes2HexString(getPrivateKey(keyMap)); } /** - * ��˽Կ���� - * @param data ������� - * @param hexKey ��Կ + * 私钥加密 + * @param data 明文数据 + * @param hexKey 私钥HEX编码 * @return * @throws Exception */ @@ -87,13 +131,19 @@ public final class RSAUtils { return encryptByPrivateKey(data,keyBytes); } + /** + * 私钥加密 + * @param data 明文数据 + * @param hexKey 私钥 + * @return + * @throws Exception + */ public static byte[] encryptByPrivateKey(byte[] data, byte[] keyBytes)throws Exception { PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM); Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); - // ����ݼ��� Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); @@ -101,24 +151,31 @@ public final class RSAUtils { } /** - * ��˽Կ���� - * @param data ������� - * @param hexKey ��Կ - * @return + * 私钥解密 + * @param data 解密数据 + * @param hexKey 私钥HEX编码 + * @return 明文数据 * @throws Exception */ public static byte[] decryptByPrivateKey(byte[] data, String hexKey)throws Exception { - // ��˽Կ���� + // 私钥HEX编码转换为byte byte[] keyBytes = HexUtils.hex2Bytes(hexKey); return decryptByPrivateKey(data,keyBytes); } + /** + * 私钥解密 + * @param data 解密数据 + * @param keyBytes 私钥 + * @return 明文数据 + * @throws Exception + */ public static byte[] decryptByPrivateKey(byte[] data, byte[] keyBytes)throws Exception { PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM); Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); - // ����ݽ��� + // 解密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); @@ -126,10 +183,10 @@ public final class RSAUtils { } /** - * �ù�Կ���� - * @param data ������� - * @param hexKey ��Կ - * @return + * 公钥解密 + * @param data 明文数据 + * @param hexKey 公钥HEX + * @return 密文 * @throws Exception */ public static byte[] encryptByPublicKey(byte[] data, String hexKey)throws Exception { @@ -138,6 +195,13 @@ public final class RSAUtils { return encryptByPublicKey(data,keyBytes); } + /** + * 公钥解密 + * @param data 明文数据 + * @param hexKey 公钥 + * @return 密文 + * @throws Exception + */ public static byte[] encryptByPublicKey(byte[] data, byte[] keyBytes)throws Exception { X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); @@ -152,31 +216,43 @@ public final class RSAUtils { } /** - * �ù�Կ���� - * @param data ������� - * @param hexKey ��Կ - * @return + * 公钥解密 + * @param data 密文数据 + * @param hexKey 公钥HEX + * @return 明文 * @throws Exception */ public static byte[] decryptByPublicKey(byte[] data, String hexKey)throws Exception { - // ��˽Կ���� + // hexKey 公钥HEX转换为byte byte[] keyBytes = HexUtils.hex2Bytes(hexKey); return decryptByPublicKey(data,keyBytes); } + /** + * 公钥解密 + * @param data 密文数据 + * @param keyBytes 公钥 + * @return 明文 + * @throws Exception + */ public static byte[] decryptByPublicKey(byte[] data, byte[] keyBytes)throws Exception { - // ��˽Կ���� + // 通过keyBytes构建公钥 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM); Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec); - // ����ݽ��� + // 解密 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, publicKey); return cipher.doFinal(data); } + /** + * 获取公钥的PEM格式 + * @param encoded 公钥 + * @return PEM格式公钥 + */ public static String getPublicKeyPEM(byte[] encoded) { StringBuffer base64String = new StringBuffer(""); @@ -186,6 +262,11 @@ public final class RSAUtils { return base64String.toString(); } + /** + * 获取私钥的PEM格式 + * @param encoded 私钥 + * @return PEM格式私钥 + */ public static String getPrivateKeyPEM(byte[] encoded) { StringBuffer base64String = new StringBuffer(""); @@ -195,15 +276,20 @@ public final class RSAUtils { return base64String.toString(); } + /** + * 获取密钥的PEM格式 + * @param encoded 密钥 + * @return PEM格式密钥 + */ public static String getBase64PEM(byte[] encoded) { String base64String = Base64.getEncoder().encodeToString(encoded); StringBuffer base64ArrayString = new StringBuffer(""); int startPosition = 0; - int endPosition = BASE64ARRAY_SIZE; + int endPosition = PEM_ARRAY_SIZE; while(endPosition < base64String.length()) { base64ArrayString.append(base64String.substring(startPosition, endPosition)).append("\n"); startPosition = endPosition; - endPosition = endPosition + BASE64ARRAY_SIZE; + endPosition = endPosition + PEM_ARRAY_SIZE; } if(startPosition < base64String.length()) { base64ArrayString.append(base64String.substring(startPosition)).append("\n"); diff --git a/maxkey-common/src/test/java/org/maxkey/crypto/RSAUtilsTest.java b/maxkey-common/src/test/java/org/maxkey/crypto/RSAUtilsTest.java index 39e869757..02ab62e31 100644 --- a/maxkey-common/src/test/java/org/maxkey/crypto/RSAUtilsTest.java +++ b/maxkey-common/src/test/java/org/maxkey/crypto/RSAUtilsTest.java @@ -28,8 +28,7 @@ public class RSAUtilsTest { @Test public void test() throws Exception { - // ˽Կ���ܡ�����Կ���� - // ˽Կǩ����Կ��֤ǩ�� + // RSA KeyPair Map key = RSAUtils.genKeyPair(); String privateKey = RSAUtils.getPublicKey2Hex(key); String publicKey = RSAUtils.getPrivateKey2Hex(key); @@ -40,8 +39,8 @@ public class RSAUtilsTest { System.out.println("privateKey:" + Base64Utils.base64UrlEncode(keyp.getEncoded())); byte[] encodedData = RSAUtils.encryptByPrivateKey(signString.getBytes(), privateKey); - System.out.println("���ܺ�\r\n" + new String(encodedData)); - System.out.println("���ܺ�B64��\r\n" + HexUtils.bytes2HexString(encodedData)); + System.out.println("encodedData \r\n" + new String(encodedData)); + System.out.println("encodedData HexString \r\n" + HexUtils.bytes2HexString(encodedData)); byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey); String target = new String(decodedData); System.out.println("target:" + target); diff --git a/maxkey-gataway/src/main/resources/application.yml b/maxkey-gataway/src/main/resources/application.yml index 9af16be15..37a0d9129 100644 --- a/maxkey-gataway/src/main/resources/application.yml +++ b/maxkey-gataway/src/main/resources/application.yml @@ -1,7 +1,7 @@ #端口号 application: name: maxkey-gateway-server - formatted-version: v3.3.2 GA + formatted-version: v3.3.3 GA server: port: 9000 spring: diff --git a/maxkey-webs/maxkey-boot-monitor/src/main/resources/application.properties b/maxkey-webs/maxkey-boot-monitor/src/main/resources/application.properties index caa505d66..08f483173 100644 --- a/maxkey-webs/maxkey-boot-monitor/src/main/resources/application.properties +++ b/maxkey-webs/maxkey-boot-monitor/src/main/resources/application.properties @@ -18,7 +18,7 @@ application.title =MaxKey #for dynamic service discovery spring.application.name =maxkey-monitor -application.formatted-version =v3.3.2 GA +application.formatted-version =v3.3.3 GA #nacos discovery spring.cloud.nacos.discovery.enabled =${NACOS_DISCOVERY_ENABLED:false} spring.cloud.nacos.discovery.instance-enabled =false diff --git a/maxkey-webs/maxkey-web-maxkey/src/main/resources/application.properties b/maxkey-webs/maxkey-web-maxkey/src/main/resources/application.properties index 4cf02c343..09d530816 100644 --- a/maxkey-webs/maxkey-web-maxkey/src/main/resources/application.properties +++ b/maxkey-webs/maxkey-web-maxkey/src/main/resources/application.properties @@ -1,5 +1,5 @@ ############################################################################ -# Copyright [2021] [MaxKey of copyright http://www.maxkey.top] +# Copyright [2022] [MaxKey of copyright http://www.maxkey.top] # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ #MaxKey Title and Version # ############################################################################ application.title =MaxKey -application.formatted-version =v3.3.2 GA +application.formatted-version =v3.3.3 GA #for dynamic service discovery spring.application.name =maxkey ############################################################################ diff --git a/maxkey-webs/maxkey-web-mgt/src/main/resources/application.properties b/maxkey-webs/maxkey-web-mgt/src/main/resources/application.properties index 92ef80612..75fb0ad12 100644 --- a/maxkey-webs/maxkey-web-mgt/src/main/resources/application.properties +++ b/maxkey-webs/maxkey-web-mgt/src/main/resources/application.properties @@ -1,5 +1,5 @@ ############################################################################ -# Copyright [2021] [MaxKey of copyright http://www.maxkey.top] +# Copyright [2022] [MaxKey of copyright http://www.maxkey.top] # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,7 +16,7 @@ #MaxKey Title and Version # ############################################################################ application.title =MaxKey-Mgt -application.formatted-version =v3.3.2 GA +application.formatted-version =v3.3.3 GA #for dynamic service discovery spring.application.name =maxkey-mgt ############################################################################