Skip to content

Commit a5ad265

Browse files
authored
Merge pull request #16 from yanghaiji/update_md_fe
优化原有代码 🆕 📖
2 parents a4ac773 + 25914a9 commit a5ad265

File tree

5 files changed

+170
-34
lines changed

5 files changed

+170
-34
lines changed

src/main/java/com/javayh/secure/transmit/encrypt/SecureTransmitTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface SecureTransmitTemplate {
2121
* @param data 加密的数据
2222
* @return {@link String} 最终的结果
2323
*/
24-
String encrypt(String key, String data) throws Exception;
24+
String encrypt(String key, String data);
2525

2626
/**
2727
* 数据解密

src/main/java/com/javayh/secure/transmit/encrypt/aes/AESEncryption.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.javayh.secure.transmit.constant.EncryptConstant;
55
import com.javayh.secure.transmit.encrypt.SecureTransmitTemplate;
66
import com.javayh.secure.transmit.encrypt.base.Base64Util;
7+
import com.javayh.secure.transmit.exception.EncryptionException;
78
import lombok.extern.slf4j.Slf4j;
89

910
import javax.crypto.BadPaddingException;
@@ -43,18 +44,19 @@ public AESEncryption(SecretProperties secretProperties) {
4344
* @param key 加密的key
4445
* @param encData
4546
* @return
46-
* @throws Exception
4747
*/
4848
@Override
49-
public String encrypt(String key, String encData) throws Exception {
49+
public String encrypt(String key, String encData) {
5050
key = setSuffix(key, secretProperties.getAes().getIv());
5151
byte[] result = new byte[0];
5252
try {
5353
result = encCipher(key).doFinal(encData.getBytes());
5454
} catch (IllegalBlockSizeException e) {
5555
log.error(" IllegalBlockSizeException is error {}", e.getMessage());
56+
throw new EncryptionException(e.getMessage());
5657
} catch (BadPaddingException e) {
5758
log.error(" BadPaddingException is error {}", e.getMessage());
59+
throw new EncryptionException(e.getMessage());
5860
}
5961
return Base64Util.encode(result);
6062
}

src/main/java/com/javayh/secure/transmit/encrypt/aes/AESGCMEncryption.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import com.javayh.secure.transmit.constant.EncryptConstant;
55
import com.javayh.secure.transmit.encrypt.SecureTransmitTemplate;
66
import com.javayh.secure.transmit.encrypt.base.Base64Util;
7+
import com.javayh.secure.transmit.exception.EncryptionException;
78
import lombok.extern.slf4j.Slf4j;
89

9-
import javax.crypto.Cipher;
10-
import javax.crypto.SecretKey;
10+
import javax.crypto.*;
1111
import javax.crypto.spec.GCMParameterSpec;
1212
import java.nio.charset.StandardCharsets;
13+
import java.security.InvalidAlgorithmParameterException;
14+
import java.security.InvalidKeyException;
15+
import java.security.NoSuchAlgorithmException;
1316

1417
/**
1518
* <p>
@@ -30,14 +33,24 @@ public AESGCMEncryption(SecretProperties secretProperties) {
3033
}
3134

3235
@Override
33-
public String encrypt(String keyAsString, String data) throws Exception {
36+
public String encrypt(String keyAsString, String data) {
3437
SecretKey secretKey = convertStringToKey(keyAsString);
38+
Cipher cipher;
39+
try {
40+
cipher = Cipher.getInstance(EncryptConstant.AES_GCM);
41+
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, iv.getBytes());
42+
cipher.init(Cipher.ENCRYPT_MODE, secretKey, gcmParameterSpec);
43+
return Base64Util.encode(cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)));
44+
} catch (NoSuchAlgorithmException |
45+
NoSuchPaddingException |
46+
InvalidKeyException |
47+
InvalidAlgorithmParameterException |
48+
IllegalBlockSizeException |
49+
BadPaddingException e) {
50+
log.error("GCM 解密异常 {}", e.getMessage(), e);
51+
throw new EncryptionException(e.getMessage());
52+
}
3553

36-
Cipher cipher = Cipher.getInstance(EncryptConstant.AES_GCM);
37-
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, iv.getBytes());
38-
cipher.init(Cipher.ENCRYPT_MODE, secretKey, gcmParameterSpec);
39-
40-
return Base64Util.encode(cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)));
4154
}
4255

4356

@@ -53,12 +66,13 @@ public String decrypt(String keyAsString, String encryptedData) {
5366
return Base64Util.encode(decryptedBytes);
5467
} catch (Exception e) {
5568
log.error("AESGCMEncryption 解密失败 {}", e.getMessage());
69+
throw new EncryptionException(e.getMessage());
5670
}
57-
return null;
5871
}
5972

6073
/**
6174
* 将字符串转换为密钥
75+
*
6276
* @param keyAsString 密钥key
6377
*/
6478
private SecretKey convertStringToKey(String keyAsString) {

src/main/java/com/javayh/secure/transmit/encrypt/ecc/EccEncryption.java

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
import com.javayh.secure.transmit.constant.EncryptConstant;
55
import com.javayh.secure.transmit.encrypt.SecureTransmitTemplate;
66
import com.javayh.secure.transmit.encrypt.base.Base64Util;
7-
import lombok.SneakyThrows;
7+
import com.javayh.secure.transmit.exception.EncryptionException;
88
import lombok.extern.slf4j.Slf4j;
99

10+
import javax.crypto.BadPaddingException;
1011
import javax.crypto.Cipher;
11-
import java.security.KeyFactory;
12-
import java.security.PrivateKey;
13-
import java.security.PublicKey;
14-
import java.security.Security;
12+
import javax.crypto.IllegalBlockSizeException;
13+
import javax.crypto.NoSuchPaddingException;
14+
import java.security.*;
15+
import java.security.spec.InvalidKeySpecException;
1516
import java.security.spec.PKCS8EncodedKeySpec;
1617
import java.security.spec.X509EncodedKeySpec;
1718
import java.util.Base64;
@@ -39,47 +40,80 @@ public class EccEncryption implements SecureTransmitTemplate {
3940
* @param plaintext 代加密的数据
4041
*/
4142
@Override
42-
public String encrypt(String pubKey, String plaintext) throws Exception {
43+
public String encrypt(String pubKey, String plaintext) {
4344
PublicKey publicKey = stringToKey(pubKey);
44-
Cipher cipher = Cipher.getInstance(EncryptConstant.ECIES, EncryptConstant.BC);
45-
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
46-
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
47-
return Base64Util.encode(encryptedBytes);
45+
Cipher cipher = null;
46+
try {
47+
cipher = Cipher.getInstance(EncryptConstant.ECIES, EncryptConstant.BC);
48+
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
49+
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
50+
return Base64Util.encode(encryptedBytes);
51+
} catch (NoSuchAlgorithmException |
52+
NoSuchProviderException |
53+
NoSuchPaddingException |
54+
IllegalBlockSizeException |
55+
BadPaddingException |
56+
InvalidKeyException e) {
57+
log.error("EccEncryption 解密失败 {}", e.getMessage());
58+
throw new EncryptionException(e.getMessage());
59+
}
60+
4861
}
4962

5063
/**
5164
* @param priKey 解密私钥 {@link EccProperties#getPrivateKey()}
5265
* @param encryptedData 待解密的数据
5366
*/
5467
@Override
55-
@SneakyThrows
5668
public String decrypt(String priKey, String encryptedData) {
5769
PrivateKey privateKey = stringToPrivateKey(priKey);
58-
Cipher cipher = Cipher.getInstance(EncryptConstant.ECIES, EncryptConstant.BC);
59-
cipher.init(Cipher.DECRYPT_MODE, privateKey);
60-
byte[] encryptedBytes = Base64Util.decode(encryptedData);
61-
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
62-
return Base64Util.encode(decryptedBytes);
70+
Cipher cipher;
71+
try {
72+
cipher = Cipher.getInstance(EncryptConstant.ECIES, EncryptConstant.BC);
73+
cipher.init(Cipher.DECRYPT_MODE, privateKey);
74+
byte[] encryptedBytes = Base64Util.decode(encryptedData);
75+
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
76+
return Base64Util.encode(decryptedBytes);
77+
} catch (NoSuchAlgorithmException |
78+
NoSuchProviderException |
79+
NoSuchPaddingException |
80+
IllegalBlockSizeException |
81+
BadPaddingException |
82+
InvalidKeyException e) {
83+
log.error("EccEncryption 解密失败 {}", e.getMessage());
84+
throw new EncryptionException(e.getMessage());
85+
}
86+
6387
}
6488

6589
/**
6690
* 反算公钥
6791
*/
68-
@SneakyThrows
6992
private static PublicKey stringToKey(String keyString) {
7093
byte[] keyBytes = Base64.getDecoder().decode(keyString);
71-
KeyFactory keyFactory = KeyFactory.getInstance(EncryptConstant.EC, EncryptConstant.BC);
72-
return keyFactory.generatePublic(new X509EncodedKeySpec(keyBytes));
94+
KeyFactory keyFactory;
95+
try {
96+
keyFactory = KeyFactory.getInstance(EncryptConstant.EC, EncryptConstant.BC);
97+
return keyFactory.generatePublic(new X509EncodedKeySpec(keyBytes));
98+
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException e) {
99+
log.error("EccEncryption 解密失败 {}", e.getMessage());
100+
throw new EncryptionException(e.getMessage());
101+
}
73102
}
74103

75104
/**
76105
* 反算私钥
77106
*/
78-
@SneakyThrows
79107
private static PrivateKey stringToPrivateKey(String keyString) {
80108
byte[] keyBytes = Base64Util.decode(keyString);
81-
KeyFactory keyFactory = KeyFactory.getInstance(EncryptConstant.EC, EncryptConstant.BC);
82-
return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
109+
KeyFactory keyFactory;
110+
try {
111+
keyFactory = KeyFactory.getInstance(EncryptConstant.EC, EncryptConstant.BC);
112+
return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
113+
} catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException e) {
114+
log.error("EccEncryption 解密失败 {}", e.getMessage());
115+
throw new EncryptionException(e.getMessage());
116+
}
83117
}
84118

85119
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.javayh.secure.transmit.exception;
2+
3+
/**
4+
* <p>
5+
* 解密异常
6+
* </p>
7+
*
8+
* @author hai ji
9+
* @version 1.0.0
10+
* @since 2023-08-22
11+
*/
12+
public class EncryptionException extends RuntimeException {
13+
/**
14+
* Constructs a new runtime exception with {@code null} as its
15+
* detail message. The cause is not initialized, and may subsequently be
16+
* initialized by a call to {@link #initCause}.
17+
*/
18+
public EncryptionException() {
19+
super();
20+
}
21+
22+
/**
23+
* Constructs a new runtime exception with the specified detail message.
24+
* The cause is not initialized, and may subsequently be initialized by a
25+
* call to {@link #initCause}.
26+
*
27+
* @param message the detail message. The detail message is saved for
28+
* later retrieval by the {@link #getMessage()} method.
29+
*/
30+
public EncryptionException(String message) {
31+
super(message);
32+
}
33+
34+
/**
35+
* Constructs a new runtime exception with the specified detail message and
36+
* cause. <p>Note that the detail message associated with
37+
* {@code cause} is <i>not</i> automatically incorporated in
38+
* this runtime exception's detail message.
39+
*
40+
* @param message the detail message (which is saved for later retrieval
41+
* by the {@link #getMessage()} method).
42+
* @param cause the cause (which is saved for later retrieval by the
43+
* {@link #getCause()} method). (A <tt>null</tt> value is
44+
* permitted, and indicates that the cause is nonexistent or
45+
* unknown.)
46+
* @since 1.4
47+
*/
48+
public EncryptionException(String message, Throwable cause) {
49+
super(message, cause);
50+
}
51+
52+
/**
53+
* Constructs a new runtime exception with the specified cause and a
54+
* detail message of <tt>(cause==null ? null : cause.toString())</tt>
55+
* (which typically contains the class and detail message of
56+
* <tt>cause</tt>). This constructor is useful for runtime exceptions
57+
* that are little more than wrappers for other throwables.
58+
*
59+
* @param cause the cause (which is saved for later retrieval by the
60+
* {@link #getCause()} method). (A <tt>null</tt> value is
61+
* permitted, and indicates that the cause is nonexistent or
62+
* unknown.)
63+
* @since 1.4
64+
*/
65+
public EncryptionException(Throwable cause) {
66+
super(cause);
67+
}
68+
69+
/**
70+
* Constructs a new runtime exception with the specified detail
71+
* message, cause, suppression enabled or disabled, and writable
72+
* stack trace enabled or disabled.
73+
*
74+
* @param message the detail message.
75+
* @param cause the cause. (A {@code null} value is permitted,
76+
* and indicates that the cause is nonexistent or unknown.)
77+
* @param enableSuppression whether or not suppression is enabled
78+
* or disabled
79+
* @param writableStackTrace whether or not the stack trace should
80+
* be writable
81+
* @since 1.7
82+
*/
83+
protected EncryptionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
84+
super(message, cause, enableSuppression, writableStackTrace);
85+
}
86+
}

0 commit comments

Comments
 (0)