Skip to content

Commit d5532a1

Browse files
committed
[Change] 将多个加密工具类方法中抛出的'RuntimeException'替换为'IllegalStateException';
[Update] 更新加密工具类的Javadoc内容;
1 parent 2090796 commit d5532a1

File tree

7 files changed

+51
-19
lines changed

7 files changed

+51
-19
lines changed

src/main/java/net/lamgc/utils/encrypt/AESEncrypt.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package net.lamgc.utils.encrypt;
22

33
import javax.crypto.*;
4-
import javax.crypto.spec.SecretKeySpec;
54
import java.security.InvalidKeyException;
65
import java.security.NoSuchAlgorithmException;
7-
import java.security.SecureRandom;
86

97
/**
108
* AES对称加密工具类
@@ -55,7 +53,7 @@ public static byte[] encrypt(byte[] data, SecretKey key) throws NoSuchPaddingExc
5553
try {
5654
cipher = Cipher.getInstance(Algorithm);
5755
} catch (NoSuchAlgorithmException e) {
58-
throw new RuntimeException(e);
56+
throw new IllegalStateException(e);
5957
}
6058
//初始化为加密模式,导入密钥
6159
//初始化密码器
@@ -79,7 +77,7 @@ public static byte[] decrypt(byte[] data, SecretKey key) throws NoSuchPaddingExc
7977
try {
8078
cipher = Cipher.getInstance(Algorithm);
8179
} catch (NoSuchAlgorithmException e) {
82-
throw new RuntimeException(e);
80+
throw new IllegalStateException(e);
8381
}
8482
//初始化为加密模式,导入密钥
8583
//初始化密码器

src/main/java/net/lamgc/utils/encrypt/DiffieHellmanEncrypt.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import java.security.spec.InvalidKeySpecException;
77
import java.security.spec.X509EncodedKeySpec;
88

9+
/**
10+
* DH密钥交换
11+
*/
912
public final class DiffieHellmanEncrypt {
1013

1114
static {
@@ -21,11 +24,18 @@ public final class DiffieHellmanEncrypt {
2124

2225
private KeyPair keyPair;
2326

24-
27+
/**
28+
* 使用默认的密钥长度构造一个DH密钥交换对象
29+
* @see #DEFAULT_KEY_SIZE
30+
*/
2531
public DiffieHellmanEncrypt(){
2632
initKey(DEFAULT_KEY_SIZE);
2733
}
2834

35+
/**
36+
* 构造一个DH密钥交换对象并指定DH密钥长度
37+
* @param keySize 密钥长度
38+
*/
2939
public DiffieHellmanEncrypt(int keySize){
3040
initKey(keySize);
3141
}
@@ -40,7 +50,7 @@ public void initKey(int keySize){
4050
try {
4151
keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
4252
} catch (NoSuchAlgorithmException e) {
43-
throw new RuntimeException(e);
53+
throw new IllegalStateException(e);
4454
}
4555
//初始化密钥对生成器
4656
keyPairGenerator.initialize(keySize);
@@ -70,22 +80,29 @@ public SecretKey getSecretKey(byte[] publicKey, Algorithm algorithm) throws Inva
7080
PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
7181
return getSecretKey(pubKey, algorithm);
7282
}catch(NoSuchAlgorithmException | InvalidKeySpecException e){
73-
throw new RuntimeException(e);
83+
throw new IllegalStateException(e);
7484
}
7585
}
7686

87+
/**
88+
* 引入对方公钥, 构造指定算法的密钥
89+
* @param publicKey 对方公钥
90+
* @param algorithm 公共密钥算法
91+
* @return 密钥对象
92+
* @throws InvalidKeyException 当PublicKey无效时抛出
93+
*/
7794
public SecretKey getSecretKey(PublicKey publicKey, Algorithm algorithm) throws InvalidKeyException {
7895
try{
7996
KeyAgreement keyAgree = KeyAgreement.getInstance(KEY_ALGORITHM);
8097
keyAgree.init(this.keyPair.getPrivate());
8198
keyAgree.doPhase(publicKey, true);
8299
return keyAgree.generateSecret(algorithm.algorithmName);
83100
}catch(NoSuchAlgorithmException e){
84-
throw new RuntimeException(e);
101+
throw new IllegalStateException(e);
85102
}
86103
}
87104

88-
105+
@SuppressWarnings("unused")
89106
public enum Algorithm{
90107
AES("AES"),
91108
RC2("RC2"),

src/main/java/net/lamgc/utils/encrypt/EncryptUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public final class EncryptUtils {
1414
* @param keyEncode 密钥编码数据
1515
* @param algorithm 密钥所属算法
1616
* @return SecretKey对象
17+
* @throws NullPointerException 当byteEncode或algorithm为null时抛出
1718
*/
1819
public static SecretKey BytesToSecretKey(byte[] keyEncode, String algorithm){
1920
Objects.requireNonNull(keyEncode);
@@ -26,8 +27,8 @@ public static SecretKey BytesToSecretKey(byte[] keyEncode, String algorithm){
2627
* @param encodeRules 密钥规则, 可以使用随机数据
2728
* @param keySize 随机源大小,默认128
2829
* @param keyAlgorithm 算法名
29-
* @throws IllegalStateException 当传入的算法名不支持时抛出, 该异常的Cause为{@link NoSuchAlgorithmException}异常.
3030
* @return AES密钥
31+
* @throws IllegalStateException 当传入的算法名不支持时抛出, 该异常的Cause为{@link NoSuchAlgorithmException}异常.
3132
*/
3233
public static SecretKey getSecretKey(byte[] encodeRules, int keySize, String keyAlgorithm)
3334
throws IllegalStateException {

src/main/java/net/lamgc/utils/encrypt/HmacEncryptUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import java.security.NoSuchAlgorithmException;
77
import java.util.Objects;
88

9+
/**
10+
* Hmac加密工具类
11+
* <p>通过该类可以很便捷的进行Hmac加密操作.</p>
12+
*/
913
public final class HmacEncryptUtils {
1014

1115
private HmacEncryptUtils() {}

src/main/java/net/lamgc/utils/encrypt/MessageDigestUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44
import java.security.NoSuchAlgorithmException;
55
import java.util.Objects;
66

7+
/**
8+
* 数据摘要工具类.
9+
*/
710
public final class MessageDigestUtils {
811

912
private MessageDigestUtils() {}
1013

14+
/**
15+
* 对指定数据取摘要
16+
* @param data 待取摘要的数据
17+
* @param algorithm 数据摘要算法
18+
* @return 返回数据摘要
19+
*/
1120
public static byte[] encrypt(byte[] data, Algorithm algorithm){
1221
MessageDigest digest;
1322
try {

src/main/java/net/lamgc/utils/encrypt/RSAEncrypt.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import java.security.spec.X509EncodedKeySpec;
1313
import java.util.Objects;
1414

15+
/**
16+
* RSA加密类.
17+
*/
1518
public final class RSAEncrypt {
1619

1720
public static int DEFAULT_KEY_SIZE = 3072;
@@ -28,7 +31,7 @@ public static KeyPair getKeyPair(int keySize){
2831
try{
2932
keyPairGen = KeyPairGenerator.getInstance("RSA");
3033
}catch(NoSuchAlgorithmException e){
31-
throw new RuntimeException(e);
34+
throw new IllegalStateException(e);
3235
}
3336

3437
keyPairGen.initialize(keySize, new SecureRandom());
@@ -46,7 +49,7 @@ public static RSAPublicKey bytesToRSAPublicKey(byte[] keyBytes) throws InvalidKe
4649
try {
4750
return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes));
4851
} catch (NoSuchAlgorithmException e) {
49-
throw new RuntimeException(e);
52+
throw new IllegalStateException(e);
5053
}
5154
}
5255

@@ -60,7 +63,7 @@ public static RSAPrivateKey bytesToRSAPrivateKey(byte[] keyBytes) throws Invalid
6063
try {
6164
return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
6265
} catch (NoSuchAlgorithmException e) {
63-
throw new RuntimeException(e);
66+
throw new IllegalStateException(e);
6467
}
6568
}
6669

@@ -82,7 +85,7 @@ public static byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData) throw
8285
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
8386
output = cipher.doFinal(plainTextData);
8487
} catch (NoSuchAlgorithmException e) {
85-
throw new RuntimeException(e);
88+
throw new IllegalStateException(e);
8689
}
8790
return output;
8891
}
@@ -105,7 +108,7 @@ public static byte[] encrypt(RSAPrivateKey privateKey, byte[] plainTextData) thr
105108
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
106109
output = cipher.doFinal(plainTextData);
107110
} catch (NoSuchAlgorithmException e) {
108-
throw new RuntimeException(e);
111+
throw new IllegalStateException(e);
109112
}
110113
return output;
111114
}
@@ -129,7 +132,7 @@ public static byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData) throws
129132
cipher.init(Cipher.DECRYPT_MODE, privateKey);
130133
output = cipher.doFinal(cipherData);
131134
} catch (NoSuchAlgorithmException e) {
132-
throw new RuntimeException(e);
135+
throw new IllegalStateException(e);
133136
}
134137
return output;
135138
}
@@ -153,7 +156,7 @@ public static byte[] decrypt(RSAPublicKey publicKey, byte[] cipherData) throws N
153156
cipher.init(Cipher.DECRYPT_MODE, publicKey);
154157
output = cipher.doFinal(cipherData);
155158
} catch (NoSuchAlgorithmException e) {
156-
throw new RuntimeException(e);
159+
throw new IllegalStateException(e);
157160
}
158161
return output;
159162
}

src/main/java/net/lamgc/utils/encrypt/RSASign.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static byte[] sign(byte[] content, byte[] privateKey, SignAlgorithm algor
2929
priKey = KeyFactory.getInstance("RSA").generatePrivate(privatePKCS8);
3030
signature = Signature.getInstance(algorithm.algorithmName);
3131
}catch(NoSuchAlgorithmException e){
32-
throw new RuntimeException(e);
32+
throw new IllegalStateException(e);
3333
}
3434
signature.initSign(priKey);
3535
signature.update(content);
@@ -71,7 +71,7 @@ public static boolean checkSign(byte[] content, byte[] sign, byte[] publicKey, S
7171
pubKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(publicKey));
7272
signature = Signature.getInstance(algorithm.algorithmName);
7373
}catch(NoSuchAlgorithmException e){
74-
throw new RuntimeException(e);
74+
throw new IllegalStateException(e);
7575
}
7676
signature.initVerify(pubKey);
7777
signature.update(content);

0 commit comments

Comments
 (0)