Skip to content
This repository was archived by the owner on May 23, 2025. It is now read-only.

Commit c7540ad

Browse files
committed
Update OpenSecretUtils.java
1 parent 2cafffc commit c7540ad

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/main/java/io/github/minecraftchampions/dodoopenjava/utils/OpenSecretUtils.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import javax.crypto.spec.IvParameterSpec;
77
import javax.crypto.spec.SecretKeySpec;
88
import java.security.AlgorithmParameters;
9+
import java.security.NoSuchAlgorithmException;
10+
import java.security.spec.InvalidParameterSpecException;
911

1012
/**
1113
* 开放秘密工具
@@ -24,29 +26,37 @@ public class OpenSecretUtils {
2426
*/
2527
public static String webHookDecrypt(String payload, String secretKey) {
2628
try {
27-
return aesDecrypt(hexToBytes(payload), hexToBytes(secretKey), new byte[16], Cipher.getInstance("AES/CBC/PKCS5Padding"));
29+
return aesDecrypt(hexToBytes(payload), hexToBytes(secretKey), Cipher.getInstance("AES/CBC/PKCS5Padding"));
2830
} catch (Exception e) {
2931
log.error("WebHook数据解密时错误", e);
3032
return null;
3133
}
3234
}
3335

36+
public static final AlgorithmParameters AES_PARAMS;
37+
38+
static {
39+
try {
40+
AES_PARAMS = AlgorithmParameters.getInstance("AES");
41+
AES_PARAMS.init(new IvParameterSpec(new byte[16]));
42+
} catch (NoSuchAlgorithmException | InvalidParameterSpecException e) {
43+
throw new RuntimeException(e);
44+
}
45+
}
3446

3547
/**
3648
* AES解密
3749
*
3850
* @param payload 加密消息
3951
* @param secretKey 解密密钥
40-
* @param iv IV向量
4152
* @param cipher AES配置
4253
* @return 解密密过后的值
4354
*/
44-
private static String aesDecrypt(byte[] payload, byte[] secretKey, byte[] iv, Cipher cipher) throws Exception {
45-
var sKeySpec = new SecretKeySpec(secretKey, "AES");
46-
var params = AlgorithmParameters.getInstance("AES");
47-
params.init(new IvParameterSpec(iv));
48-
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, params);
49-
var result = cipher.doFinal(payload);
55+
private static String aesDecrypt(byte[] payload, byte[] secretKey, Cipher cipher) throws Exception {
56+
SecretKeySpec sKeySpec = new SecretKeySpec(secretKey, "AES");
57+
58+
cipher.init(Cipher.DECRYPT_MODE, sKeySpec, AES_PARAMS);
59+
byte[] result = cipher.doFinal(payload);
5060
return new String(result);
5161
}
5262

0 commit comments

Comments
 (0)