SecretKeyUtils.java
5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.huaheng.control.management.utils;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import com.huaheng.control.management.utils.constant.RSA256Key;
/**
* KeyPairGenerator https://www.jianshu.com/p/4de1ee0e7206 key的生成使用方法
*/
public class SecretKeyUtils {
public static final String KEY_ALGORITHM = "RSA";
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
private static RSA256Key rsa256Key;
// 获得公钥
public static String getPublicKey(Map<String, Object> keyMap) throws Exception {
// 获得map中的公钥对象 转为key对象
Key key = (Key)keyMap.get(PUBLIC_KEY);
// byte[] publicKey = key.getEncoded();
// 编码返回字符串
return encryptBASE64(key.getEncoded());
}
public static String getPublicKey(RSA256Key rsa256Key) throws Exception {
// 获得map中的公钥对象 转为key对象
Key key = rsa256Key.getPublicKey();
// byte[] publicKey = key.getEncoded();
// 编码返回字符串
return encryptBASE64(key.getEncoded());
}
// 获得私钥
public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {
// 获得map中的私钥对象 转为key对象
Key key = (Key)keyMap.get(PRIVATE_KEY);
// byte[] privateKey = key.getEncoded();
// 编码返回字符串
return encryptBASE64(key.getEncoded());
}
// 获得私钥
public static String getPrivateKey(RSA256Key rsa256Key) throws Exception {
// 获得map中的私钥对象 转为key对象
Key key = rsa256Key.getPrivateKey();
// byte[] privateKey = key.getEncoded();
// 编码返回字符串
return encryptBASE64(key.getEncoded());
}
// 解码返回byte
public static byte[] decryptBASE64(String key) throws Exception {
return Base64.getDecoder().decode(key);
}
// 编码返回字符串
public static String encryptBASE64(byte[] key) throws Exception {
return Base64.getEncoder().encodeToString(key);
}
// 使用KeyPairGenerator 生成公私钥,存放于map对象中
public static Map<String, Object> initKey() throws Exception {
/* RSA算法要求有一个可信任的随机数源 */
// 获得对象 KeyPairGenerator 参数 RSA 1024个字节
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
// 通过对象 KeyPairGenerator 生成密匙对 KeyPair
KeyPair keyPair = keyPairGen.generateKeyPair();
// 通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKey
RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate();
// 公私钥对象存入map中
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
return keyMap;
}
/**
* 获取公私钥
* @return
* @throws Exception
*/
public static synchronized RSA256Key getRSA256Key() throws Exception {
if (rsa256Key == null) {
synchronized (RSA256Key.class) {
if (rsa256Key == null) {
rsa256Key = new RSA256Key();
Map<String, Object> map = initKey();
rsa256Key.setPrivateKey((RSAPrivateKey)map.get(SecretKeyUtils.PRIVATE_KEY));
rsa256Key.setPublicKey((RSAPublicKey)map.get(SecretKeyUtils.PUBLIC_KEY));
}
}
}
return rsa256Key;
}
/**
* 解码 PublicKey
* @param key
* @return
*/
public static PublicKey getPublicKey(String key) {
try {
byte[] byteKey = Base64.getDecoder().decode(key);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(byteKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(x509EncodedKeySpec);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解码 PrivateKey
* @param key
* @return
*/
public static PrivateKey getPrivateKey(String key) {
try {
byte[] byteKey = Base64.getDecoder().decode(key);
PKCS8EncodedKeySpec x509EncodedKeySpec = new PKCS8EncodedKeySpec(byteKey);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(x509EncodedKeySpec);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Map<String, Object> keyMap;
try {
keyMap = initKey(); // 使用 java.security.KeyPairGenerator 生成 公/私钥
String publicKey = getPublicKey(keyMap);
System.out.println("公钥:\n" + publicKey);
String privateKey = getPrivateKey(keyMap);
System.out.println("私钥:\n" + privateKey);
} catch (Exception e) {
e.printStackTrace();
}
}
}