FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

【企业微信】修复 sun.security.util 在高版本 java 中无法访问的问题,改为通过 bouncycastle 库解析私钥 by Demonese · Pull Request #3216 · binarywang/WxJava · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (1) .xml  (1) All 2 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 5 additions & 0 deletions weixin-java-cp/pom.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.77</version>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import org.apache.commons.lang3.StringUtils;
import sun.security.util.DerInputStream;
import sun.security.util.DerValue;
import org.bouncycastle.asn1.pkcs.RSAPrivateKey;

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

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

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

Back | FazBrowse Home | New Git URL