Skip to content

Commit 5fad646

Browse files
author
璀境石
authored
🎨 #3216 【企业微信】修复 sun.security.util 在高版本 java 中无法访问的问题,改为通过 bouncycastle 库解析私钥
1 parent 7745791 commit 5fad646

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

Diff for: weixin-java-cp/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@
8181
<groupId>org.projectlombok</groupId>
8282
<artifactId>lombok</artifactId>
8383
</dependency>
84+
<dependency>
85+
<groupId>org.bouncycastle</groupId>
86+
<artifactId>bcprov-jdk18on</artifactId>
87+
<version>1.77</version>
88+
</dependency>
8489

8590
<dependency>
8691
<groupId>org.assertj</groupId>

Diff for: weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
55
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
66
import org.apache.commons.lang3.StringUtils;
7-
import sun.security.util.DerInputStream;
8-
import sun.security.util.DerValue;
7+
import org.bouncycastle.asn1.pkcs.RSAPrivateKey;
98

109
import javax.crypto.Cipher;
1110
import java.nio.charset.StandardCharsets;
@@ -105,11 +104,18 @@ public static String decryptPriKeyByPKCS1(String encryptRandomKey, String msgAud
105104
.replace(" ", "");
106105

107106
byte[] keyBytes = Base64.getDecoder().decode(privateKey);
108-
DerValue[] seq = new DerInputStream(keyBytes).getSequence(0);
109-
RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(seq[1].getBigInteger(), seq[2].getBigInteger(),
110-
seq[3].getBigInteger(), seq[4].getBigInteger(),
111-
seq[5].getBigInteger(), seq[6].getBigInteger(),
112-
seq[7].getBigInteger(), seq[8].getBigInteger());
107+
// Java 8 以后 sun.security.util.DerInputStream 和 sun.security.util.DerValue 无法使用
108+
// 因此改为通过 org.bouncycastle:bcprov-jdk18on 来完成 ASN1 编码数据解析
109+
final RSAPrivateKey key = RSAPrivateKey.getInstance(keyBytes);
110+
final RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(
111+
key.getModulus(),
112+
key.getPublicExponent(),
113+
key.getPrivateExponent(),
114+
key.getPrime1(),
115+
key.getPrime2(),
116+
key.getExponent1(),
117+
key.getExponent2(),
118+
key.getCoefficient());
113119

114120
PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
115121
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

0 commit comments

Comments
 (0)