提交 1faaefb8 编写于 作者: M MaxKey

RSAUtils 注释

RSAUtils 注释
springVersion               =5.3.16
version                         =3.3.3
上级 d53cc127
#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
......
......@@ -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 KEY_SIZE = 1024;
public static final int BASE64ARRAY_SIZE = 64;
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 {
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<String, Object> keyMap)throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return key.getEncoded();
}
/**
* 获取私钥
* @param keyMap
* @return 私钥
* @throws Exception
*/
public static byte[] getPrivateKey(Map<String, Object> keyMap)throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return key.getEncoded();
}
/**
* 公钥数据转换为Hex字符串
* @param keyMap
* @return 公钥
* @throws Exception
*/
public static String getPublicKey2Hex(Map<String, Object> keyMap)throws Exception {
return HexUtils.bytes2HexString(getPublicKey(keyMap));
}
/**
* 私钥数据转换为Hex字符串
* @param keyMap
* @return 私钥
* @throws Exception
*/
public static String getPrivateKey2Hex(Map<String, Object> 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");
......
......@@ -28,8 +28,7 @@ public class RSAUtilsTest {
@Test
public void test() throws Exception {
// ˽Կ���ܡ�����Կ����
// ˽Կǩ����Կ��֤ǩ��
// RSA KeyPair
Map<String, Object> 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);
......
#端口号
application:
name: maxkey-gateway-server
formatted-version: v3.3.2 GA
formatted-version: v3.3.3 GA
server:
port: 9000
spring:
......
......@@ -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
......
############################################################################
# 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
############################################################################
......
############################################################################
# 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
############################################################################
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册