RSAUtils 注释

RSAUtils 注释
springVersion               =5.3.16
version                         =3.3.3
This commit is contained in:
MaxKey 2022-02-21 15:21:44 +08:00
parent d53cc127e3
commit 1faaefb870
7 changed files with 127 additions and 42 deletions

View File

@ -1,6 +1,6 @@
#maxkey properties #maxkey properties
group =maxkey.top group =maxkey.top
version =3.3.2 version =3.3.3
vendor =https://www.maxkey.top vendor =https://www.maxkey.top
author =MaxKeyTop author =MaxKeyTop
@ -44,7 +44,7 @@ poiVersion =5.1.0
tomcatVersion =9.0.58 tomcatVersion =9.0.58
tomcatembedloggingjuliVersion =8.5.2 tomcatembedloggingjuliVersion =8.5.2
#spring #spring
springVersion =5.3.15 springVersion =5.3.16
springBootVersion =2.6.3 springBootVersion =2.6.3
springSecurityVersion =5.6.1 springSecurityVersion =5.6.1
springDataVersion =2.6.1 springDataVersion =2.6.1

View File

@ -32,6 +32,10 @@ import java.util.Map;
import javax.crypto.Cipher; import javax.crypto.Cipher;
/**
* @author shiming
*
*/
public final class RSAUtils { public final class RSAUtils {
public static final String KEY_ALGORTHM = "RSA"; public static final String KEY_ALGORTHM = "RSA";
@ -40,12 +44,17 @@ public final class RSAUtils {
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<String, Object> genKeyPair() throws Exception { public static Map<String, Object> genKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORTHM); KeyPair keyPair = genRSAKeyPair();
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
@ -57,28 +66,63 @@ public final class RSAUtils {
return keyMap; 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<String, Object> keyMap)throws Exception { public static byte[] getPublicKey(Map<String, Object> keyMap)throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY); Key key = (Key) keyMap.get(PUBLIC_KEY);
return key.getEncoded(); return key.getEncoded();
} }
/**
* 获取私钥
* @param keyMap
* @return 私钥
* @throws Exception
*/
public static byte[] getPrivateKey(Map<String, Object> keyMap)throws Exception { public static byte[] getPrivateKey(Map<String, Object> keyMap)throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY); Key key = (Key) keyMap.get(PRIVATE_KEY);
return key.getEncoded(); return key.getEncoded();
} }
/**
* 公钥数据转换为Hex字符串
* @param keyMap
* @return 公钥
* @throws Exception
*/
public static String getPublicKey2Hex(Map<String, Object> keyMap)throws Exception { public static String getPublicKey2Hex(Map<String, Object> keyMap)throws Exception {
return HexUtils.bytes2HexString(getPublicKey(keyMap)); return HexUtils.bytes2HexString(getPublicKey(keyMap));
} }
/**
* 私钥数据转换为Hex字符串
* @param keyMap
* @return 私钥
* @throws Exception
*/
public static String getPrivateKey2Hex(Map<String, Object> keyMap)throws Exception { public static String getPrivateKey2Hex(Map<String, Object> keyMap)throws Exception {
return HexUtils.bytes2HexString(getPrivateKey(keyMap)); return HexUtils.bytes2HexString(getPrivateKey(keyMap));
} }
/** /**
* <EFBFBD><EFBFBD>˽Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> * 私钥加密
* @param data <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> * @param data 明文数据
* @param hexKey <EFBFBD><EFBFBD>Կ * @param hexKey 私钥HEX编码
* @return * @return
* @throws Exception * @throws Exception
*/ */
@ -87,13 +131,19 @@ public final class RSAUtils {
return encryptByPrivateKey(data,keyBytes); return encryptByPrivateKey(data,keyBytes);
} }
/**
* 私钥加密
* @param data 明文数据
* @param hexKey 私钥
* @return
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data, byte[] keyBytes)throws Exception { public static byte[] encryptByPrivateKey(byte[] data, byte[] keyBytes)throws Exception {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݼ<EFBFBD><EFBFBD><EFBFBD>
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey); cipher.init(Cipher.ENCRYPT_MODE, privateKey);
@ -101,24 +151,31 @@ public final class RSAUtils {
} }
/** /**
* <EFBFBD><EFBFBD>˽Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> * 私钥解密
* @param data <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> * @param data 解密数据
* @param hexKey <EFBFBD><EFBFBD>Կ * @param hexKey 私钥HEX编码
* @return * @return 明文数据
* @throws Exception * @throws Exception
*/ */
public static byte[] decryptByPrivateKey(byte[] data, String hexKey)throws Exception { public static byte[] decryptByPrivateKey(byte[] data, String hexKey)throws Exception {
// <EFBFBD><EFBFBD>˽Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> // 私钥HEX编码转换为byte
byte[] keyBytes = HexUtils.hex2Bytes(hexKey); byte[] keyBytes = HexUtils.hex2Bytes(hexKey);
return decryptByPrivateKey(data,keyBytes); return decryptByPrivateKey(data,keyBytes);
} }
/**
* 私钥解密
* @param data 解密数据
* @param keyBytes 私钥
* @return 明文数据
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] data, byte[] keyBytes)throws Exception { public static byte[] decryptByPrivateKey(byte[] data, byte[] keyBytes)throws Exception {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Key privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD><EFBFBD> // 解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey); cipher.init(Cipher.DECRYPT_MODE, privateKey);
@ -126,10 +183,10 @@ public final class RSAUtils {
} }
/** /**
* <EFBFBD>ù<EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> * 公钥解密
* @param data <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> * @param data 明文数据
* @param hexKey <EFBFBD><EFBFBD>Կ * @param hexKey 公钥HEX
* @return * @return 密文
* @throws Exception * @throws Exception
*/ */
public static byte[] encryptByPublicKey(byte[] data, String hexKey)throws Exception { public static byte[] encryptByPublicKey(byte[] data, String hexKey)throws Exception {
@ -138,6 +195,13 @@ public final class RSAUtils {
return encryptByPublicKey(data,keyBytes); return encryptByPublicKey(data,keyBytes);
} }
/**
* 公钥解密
* @param data 明文数据
* @param hexKey 公钥
* @return 密文
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] data, byte[] keyBytes)throws Exception { public static byte[] encryptByPublicKey(byte[] data, byte[] keyBytes)throws Exception {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
@ -152,31 +216,43 @@ public final class RSAUtils {
} }
/** /**
* <EFBFBD>ù<EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> * 公钥解密
* @param data <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> * @param data 密文数据
* @param hexKey <EFBFBD><EFBFBD>Կ * @param hexKey 公钥HEX
* @return * @return 明文
* @throws Exception * @throws Exception
*/ */
public static byte[] decryptByPublicKey(byte[] data, String hexKey)throws Exception { public static byte[] decryptByPublicKey(byte[] data, String hexKey)throws Exception {
// <EFBFBD><EFBFBD>˽Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> // hexKey 公钥HEX转换为byte
byte[] keyBytes = HexUtils.hex2Bytes(hexKey); byte[] keyBytes = HexUtils.hex2Bytes(hexKey);
return decryptByPublicKey(data,keyBytes); return decryptByPublicKey(data,keyBytes);
} }
/**
* 公钥解密
* @param data 密文数据
* @param keyBytes 公钥
* @return 明文
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] data, byte[] keyBytes)throws Exception { public static byte[] decryptByPublicKey(byte[] data, byte[] keyBytes)throws Exception {
// <EFBFBD><EFBFBD>˽Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> // 通过keyBytes构建公钥
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORTHM);
Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec); Key publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD><EFBFBD> // 解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey); cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data); return cipher.doFinal(data);
} }
/**
* 获取公钥的PEM格式
* @param encoded 公钥
* @return PEM格式公钥
*/
public static String getPublicKeyPEM(byte[] encoded) { public static String getPublicKeyPEM(byte[] encoded) {
StringBuffer base64String = StringBuffer base64String =
new StringBuffer(""); new StringBuffer("");
@ -186,6 +262,11 @@ public final class RSAUtils {
return base64String.toString(); return base64String.toString();
} }
/**
* 获取私钥的PEM格式
* @param encoded 私钥
* @return PEM格式私钥
*/
public static String getPrivateKeyPEM(byte[] encoded) { public static String getPrivateKeyPEM(byte[] encoded) {
StringBuffer base64String = StringBuffer base64String =
new StringBuffer(""); new StringBuffer("");
@ -195,15 +276,20 @@ public final class RSAUtils {
return base64String.toString(); return base64String.toString();
} }
/**
* 获取密钥的PEM格式
* @param encoded 密钥
* @return PEM格式密钥
*/
public static String getBase64PEM(byte[] encoded) { public static String getBase64PEM(byte[] encoded) {
String base64String = Base64.getEncoder().encodeToString(encoded); String base64String = Base64.getEncoder().encodeToString(encoded);
StringBuffer base64ArrayString = new StringBuffer(""); StringBuffer base64ArrayString = new StringBuffer("");
int startPosition = 0; int startPosition = 0;
int endPosition = BASE64ARRAY_SIZE; int endPosition = PEM_ARRAY_SIZE;
while(endPosition < base64String.length()) { while(endPosition < base64String.length()) {
base64ArrayString.append(base64String.substring(startPosition, endPosition)).append("\n"); base64ArrayString.append(base64String.substring(startPosition, endPosition)).append("\n");
startPosition = endPosition; startPosition = endPosition;
endPosition = endPosition + BASE64ARRAY_SIZE; endPosition = endPosition + PEM_ARRAY_SIZE;
} }
if(startPosition < base64String.length()) { if(startPosition < base64String.length()) {
base64ArrayString.append(base64String.substring(startPosition)).append("\n"); base64ArrayString.append(base64String.substring(startPosition)).append("\n");

View File

@ -28,8 +28,7 @@ public class RSAUtilsTest {
@Test @Test
public void test() throws Exception { public void test() throws Exception {
// ˽Կ<EFBFBD><EFBFBD><EFBFBD>ܡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> // RSA KeyPair
// ˽Կǩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD>֤ǩ<EFBFBD><EFBFBD>
Map<String, Object> key = RSAUtils.genKeyPair(); Map<String, Object> key = RSAUtils.genKeyPair();
String privateKey = RSAUtils.getPublicKey2Hex(key); String privateKey = RSAUtils.getPublicKey2Hex(key);
String publicKey = RSAUtils.getPrivateKey2Hex(key); String publicKey = RSAUtils.getPrivateKey2Hex(key);
@ -40,8 +39,8 @@ public class RSAUtilsTest {
System.out.println("privateKey:" + Base64Utils.base64UrlEncode(keyp.getEncoded())); System.out.println("privateKey:" + Base64Utils.base64UrlEncode(keyp.getEncoded()));
byte[] encodedData = RSAUtils.encryptByPrivateKey(signString.getBytes(), privateKey); byte[] encodedData = RSAUtils.encryptByPrivateKey(signString.getBytes(), privateKey);
System.out.println("<EFBFBD><EFBFBD><EFBFBD>ܺ<EFBFBD>\r\n" + new String(encodedData)); System.out.println("encodedData \r\n" + new String(encodedData));
System.out.println("<EFBFBD><EFBFBD><EFBFBD>ܺ<EFBFBD>B64<EFBFBD><EFBFBD>\r\n" + HexUtils.bytes2HexString(encodedData)); System.out.println("encodedData HexString \r\n" + HexUtils.bytes2HexString(encodedData));
byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey); byte[] decodedData = RSAUtils.decryptByPublicKey(encodedData, publicKey);
String target = new String(decodedData); String target = new String(decodedData);
System.out.println("target:" + target); System.out.println("target:" + target);

View File

@ -1,7 +1,7 @@
#端口号 #端口号
application: application:
name: maxkey-gateway-server name: maxkey-gateway-server
formatted-version: v3.3.2 GA formatted-version: v3.3.3 GA
server: server:
port: 9000 port: 9000
spring: spring:

View File

@ -18,7 +18,7 @@
application.title =MaxKey application.title =MaxKey
#for dynamic service discovery #for dynamic service discovery
spring.application.name =maxkey-monitor spring.application.name =maxkey-monitor
application.formatted-version =v3.3.2 GA application.formatted-version =v3.3.3 GA
#nacos discovery #nacos discovery
spring.cloud.nacos.discovery.enabled =${NACOS_DISCOVERY_ENABLED:false} spring.cloud.nacos.discovery.enabled =${NACOS_DISCOVERY_ENABLED:false}
spring.cloud.nacos.discovery.instance-enabled =false spring.cloud.nacos.discovery.instance-enabled =false

View File

@ -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"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
#MaxKey Title and Version # #MaxKey Title and Version #
############################################################################ ############################################################################
application.title =MaxKey application.title =MaxKey
application.formatted-version =v3.3.2 GA application.formatted-version =v3.3.3 GA
#for dynamic service discovery #for dynamic service discovery
spring.application.name =maxkey spring.application.name =maxkey
############################################################################ ############################################################################

View File

@ -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"); # Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License. # you may not use this file except in compliance with the License.
@ -16,7 +16,7 @@
#MaxKey Title and Version # #MaxKey Title and Version #
############################################################################ ############################################################################
application.title =MaxKey-Mgt application.title =MaxKey-Mgt
application.formatted-version =v3.3.2 GA application.formatted-version =v3.3.3 GA
#for dynamic service discovery #for dynamic service discovery
spring.application.name =maxkey-mgt spring.application.name =maxkey-mgt
############################################################################ ############################################################################