|
4 | 4 | import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
|
5 | 5 | import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
6 | 6 | 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; |
9 | 8 |
|
10 | 9 | import javax.crypto.Cipher;
|
11 | 10 | import java.nio.charset.StandardCharsets;
|
@@ -105,11 +104,18 @@ public static String decryptPriKeyByPKCS1(String encryptRandomKey, String msgAud
|
105 | 104 | .replace(" ", "");
|
106 | 105 |
|
107 | 106 | 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()); |
113 | 119 |
|
114 | 120 | PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
|
115 | 121 | Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
|
|
0 commit comments