forked from AlEinstein/javaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSAUtil.java
More file actions
391 lines (346 loc) · 13.8 KB
/
Copy pathRSAUtil.java
File metadata and controls
391 lines (346 loc) · 13.8 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package common.utils;
import common.enumeration.errorMessageEnum.ErrorMessage;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import javax.crypto.Cipher;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.*;
/**
* RSAUtil.
*
* 注意:
* 1.RSA是一种非对称加密算法,加密算法复杂,速度慢,故不适合用来加密大量数据。
* 2.可用于公钥加密,私钥解密,或私钥加密,公钥验证。
* 3.一般用来做签名校验和加密密钥(通过RSA加密AES的密钥,传输给接收方,接收方再解密得到AES密钥。双方用DES对传输的大量数据进行加解密。)
* 4.为了数据安全着想,每次获取的默认密钥(未给定密钥参数生成的密钥)都是不一样的。
* Create by mulin on 2018/12/21.
*/
public class RSAUtil {
private static Logger logger = Logger.getLogger(RSAUtil.class);
public static final String SIGN_ALGORITHMS = "RSA";
private static final int KEY_SIZE = 1024;
/*
Get keys. -------------------------------
*/
/**
* 生成并返回RSA密钥对。
*
* @return KeyPair
*/
public static synchronized KeyPair generateKeyPair() {
KeyPairGenerator keyPairGen = null;
KeyPair oneKeyPair = null;
try {
keyPairGen = KeyPairGenerator.getInstance(SIGN_ALGORITHMS);
SecureRandom random = new SecureRandom();
random.setSeed(("" + System.currentTimeMillis() * Math.random() * Math.random()).getBytes(Charset.forName("UTF-8")));
keyPairGen.initialize(KEY_SIZE, random);
oneKeyPair = keyPairGen.generateKeyPair();
return oneKeyPair;
} catch (InvalidParameterException ex) {
logger.error("KeyPairGenerator does not support a key length of " + KEY_SIZE + ".");
throw new InvalidParameterException();
} catch (NullPointerException ex) {
logger.error(ErrorMessage.KEY_PAIR_GEN_IS_NULL.getErrorInfo());
throw new NullPointerException();
} catch (NoSuchAlgorithmException ne) {
logger.error(ErrorMessage.NO_SUCH_ALGORITHM.getErrorInfo() + "KeyPair return null. | Algorithm:" + SIGN_ALGORITHMS);
return null;
}
}
/**
* 获取RSA密钥对
*
* @return String[] 十六进制字符串数组
*/
public static String[] getRSAKeyPair() throws Exception {
KeyPair rsaKeyPair = null;
try {
logger.info("Generating a pair of RSAUtil key ... ");
rsaKeyPair = generateKeyPair();
PublicKey rsaPublic = rsaKeyPair.getPublic();
PrivateKey rsaPrivate = rsaKeyPair.getPrivate();
String[] privateAndPublic = new String[2];
privateAndPublic[0] = RadixUtil.binaryBytesToHexString(rsaPrivate.getEncoded());
privateAndPublic[1] = RadixUtil.binaryBytesToHexString(rsaPublic.getEncoded());
logger.info(KEY_SIZE + "-bit RSAUtil key generated..");
return privateAndPublic;
} catch (Exception e) {
logger.error(ErrorMessage.GET_RSAKEYPAIR_FAILED.getErrorInfo() + e.getMessage(), e);
throw new Exception();
}
}
/**
* 获取公钥
*
* @return RSAPublicKey
*/
public static RSAPublicKey getDefaultPublicKey() {
KeyPair keyPair = generateKeyPair();
if (keyPair != null) {
return (RSAPublicKey) keyPair.getPublic();
}
return null;
}
/**
* 获取私钥
*
* @return RSAPrivateKey
*/
public static RSAPrivateKey getDefaultPrivateKey() {
KeyPair keyPair = generateKeyPair();
if (keyPair != null) {
return (RSAPrivateKey) keyPair.getPrivate();
}
return null;
}
/**
* 根据给定的系数和专用指数构造一个RSA专用的公钥对象。
*
* @param modulus 系数。
* @param publicExponent 专用指数。
* @return RSA专用公钥对象。
*/
public static RSAPublicKey generateRSAPublicKey(byte[] modulus, byte[] publicExponent) {
KeyFactory keyFactory = null;
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));
try {
keyFactory = KeyFactory.getInstance(SIGN_ALGORITHMS);
return (RSAPublicKey) keyFactory.generatePublic(publicKeySpec);
} catch (InvalidKeySpecException ex) {
logger.error(ErrorMessage.INVALID_PARAMETER.getErrorInfo());
throw new InvalidParameterException();
} catch (NullPointerException ex) {
logger.error(ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo());
throw new NullPointerException();
} catch (NoSuchAlgorithmException ne) {
logger.error(ErrorMessage.NO_SUCH_ALGORITHM.getErrorInfo() + "KeyPair return null. | Algorithm:" + SIGN_ALGORITHMS);
return null;
}
}
/**
* 根据给定的系数和专用指数构造一个RSA专用的私钥对象。
*
* @param modulus 系数。
* @param privateExponent 专用指数。
* @return RSA专用私钥对象。
*/
public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus, byte[] privateExponent) {
KeyFactory keyFactory = null;
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(new BigInteger(modulus), new BigInteger(privateExponent));
try {
keyFactory = KeyFactory.getInstance(SIGN_ALGORITHMS);
return (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);
} catch (InvalidKeySpecException ex) {
logger.error(ErrorMessage.INVALID_PARAMETER.getErrorInfo());
throw new InvalidParameterException();
} catch (NullPointerException ex) {
logger.error(ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo());
throw new NullPointerException();
} catch (NoSuchAlgorithmException ne) {
logger.error(ErrorMessage.NO_SUCH_ALGORITHM.getErrorInfo() + "KeyPair return null. | Algorithm:" + SIGN_ALGORITHMS);
return null;
}
}
/**
* 根据给定的16进制系数和专用指数字符串构造一个RSA专用的私钥对象。
*
* @param hexModulus 系数。
* @param hexPrivateExponent 专用指数。
* @return RSA专用私钥对象。
*/
public static RSAPrivateKey getRSAPrivateKey(String hexModulus, String hexPrivateExponent) throws Exception {
if (StringUtil.isEmpty(hexModulus) || StringUtil.isEmpty(hexPrivateExponent)) {
logger.error(ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo());
throw new NullPointerException();
}
byte[] modulus = null;
byte[] privateExponent = null;
try {
modulus = RadixUtil.hexStringToBytes(hexModulus);
privateExponent = RadixUtil.hexStringToBytes(hexPrivateExponent);
} catch (Exception ex) {
logger.error(ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo());
throw new Exception();
}
if (modulus != null && privateExponent != null) {
return generateRSAPrivateKey(modulus, privateExponent);
}
return null;
}
/**
* 根据给定的16进制系数和专用指数字符串构造一个RSA专用的公钥对象。
*
* @param hexModulus 系数。
* @param hexPublicExponent 专用指数。
* @return RSA专用公钥对象。
*/
public static RSAPublicKey getRSAPublicKey(String hexModulus, String hexPublicExponent) {
if (StringUtil.isEmpty(hexModulus) || StringUtil.isEmpty(hexPublicExponent)) {
logger.error(ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo());
throw new NullPointerException();
}
byte[] modulus = null;
byte[] publicExponent = null;
try {
modulus = RadixUtil.hexStringToBytes(hexModulus);
publicExponent = RadixUtil.hexStringToBytes(hexPublicExponent);
} catch (IllegalArgumentException ex) {
logger.error(ErrorMessage.FORMAT_INCORRECT_IllEGAL.getErrorInfo());
throw new IllegalArgumentException();
}
if (modulus != null && publicExponent != null) {
return generateRSAPublicKey(modulus, publicExponent);
}
return null;
}
/*
Encrypt or decrypt. -------------------------------
*/
/**
* 使用指定的公钥加密数据。
*
* @param publicKey 给定的公钥。
* @param data 要加密的数据。
* @return 加密后的数据。
*/
public static byte[] encrypt(PublicKey publicKey, byte[] data) throws Exception {
Cipher ci = Cipher.getInstance(SIGN_ALGORITHMS);
ci.init(Cipher.ENCRYPT_MODE, publicKey);
return ci.doFinal(data);
}
/**
* 使用指定的私钥解密数据。
*
* @param privateKey 给定的私钥。
* @param data 要解密的数据。
* @return 原数据。
*/
public static byte[] decrypt(PrivateKey privateKey, byte[] data) throws Exception {
Cipher ci = Cipher.getInstance(SIGN_ALGORITHMS);
ci.init(Cipher.DECRYPT_MODE, privateKey);
return ci.doFinal(data);
}
/**
* 使用给定的公钥加密给定的字符串。
*
* @param publicKey 给定的公钥。
* @param plaintext 字符串。
* @return 给定字符串的密文。
*/
public static String encryptString(PublicKey publicKey, String plaintext) throws Exception {
if (publicKey == null || plaintext == null) {
logger.error(ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo());
throw new NullPointerException();
}
byte[] data = plaintext.getBytes();
try {
byte[] en_data = encrypt(publicKey, data);
return new String(RadixUtil.binaryBytesToHexString(en_data));
} catch (Exception ex) {
throw new Exception();
}
}
/**
* 使用默认的公钥加密给定的字符串。
*
* @param plaintext 字符串
* @return 给定字符串的密文
*/
public static String encryptString(String plaintext) throws Exception {
if (plaintext == null) {
logger.error(ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo());
return null;
}
byte[] data = plaintext.getBytes();
KeyPair keyPair = generateKeyPair();
try {
byte[] en_data = encrypt(keyPair.getPublic(), data);
return new String(RadixUtil.binaryBytesToHexString(en_data));
} catch (NullPointerException ex) {
logger.error(ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo());
throw new NullPointerException();
} catch (Exception ex) {
throw new Exception();
}
}
/**
* 使用默认的私钥解密给定的字符串。
*
* @param encryptText 密文。
* @return 原文字符串。
*/
public static String decryptString(String encryptText) throws Exception {
if (StringUtil.isEmpty(encryptText)) {
return null;
}
KeyPair keyPair = generateKeyPair();
try {
byte[] en_data = RadixUtil.hexStringToBytes(encryptText);
byte[] data = decrypt(keyPair.getPrivate(), en_data);
return new String(data);
} catch (NullPointerException ex) {
logger.error(ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo());
throw new NullPointerException();
} catch (Exception ex) {
logger.error(String.format("\"%s\" Decryption failed. Cause: %s", encryptText, ex.getMessage()));
throw new Exception();
}
}
/*
Sign or verify. -------------------------------
*/
/**
* 获取身份签名
*
* @param privateKey
* @param src
* @param encode
* @return String
* @throws Exception
*/
public static String sign(PrivateKey privateKey, String src, String encode) throws Exception {
try {
Signature sigEng = Signature.getInstance(SIGN_ALGORITHMS);
sigEng.initSign(privateKey);
sigEng.update(src.getBytes(encode)); //更新要由字节签名或验证的数据。
byte[] signature = sigEng.sign();
return RadixUtil.binaryBytesToHexString(signature);
} catch (Exception e) {
String info = "sign failed: " + src + " | " + e.getMessage();
logger.error(info, e);
throw new Exception(info);
}
}
/**
* 校验
*
* @param publicKey
* @param sign
* @param src
* @param encode
* @throws Exception
*/
public static void verify(PublicKey publicKey, String sign, String src, String encode) throws Exception {
try {
if (StringUtils.isBlank(sign) || StringUtils.isBlank(src)) {
throw new NullPointerException(ErrorMessage.PARAMETER_EMPTY_ERROR.getErrorInfo());
}
Signature sigEng = Signature.getInstance(SIGN_ALGORITHMS);
sigEng.initVerify(publicKey);
sigEng.update(src.getBytes(encode));
byte[] sign1 = RadixUtil.hexStringToBytes(sign); //签名数据
if (!sigEng.verify(sign1)) { //数字签名
throw new Exception(ErrorMessage.VERIFY_SIGNATURE_FAIL.getErrorInfo());
}
} catch (Exception e) {
String info = "verify failed: " + sign + " | " + src + " | " + e.getMessage();
logger.error(info, e);
throw new Exception(info);
}
}
}