From 672a12d7b7fc1475c38b5456166ad8301ee7d54a Mon Sep 17 00:00:00 2001 From: zhoujiang <12345678> Date: Mon, 25 Mar 2024 09:26:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=81=E9=92=A5base64=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=81=9A=E4=B8=A4=E6=AC=A1decode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../binarywang/wxpay/config/WxPayConfig.java | 3 +++ .../wxpay/config/WxPayConfigTest.java | 25 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java index b87b3168a..877688735 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java @@ -263,6 +263,9 @@ public class WxPayConfig { throw new WxPayException("请确保apiV3Key值已设置"); } + if(StringUtils.isNotBlank(this.getPrivateKeyString())){ + this.setPrivateKeyString(Base64.getEncoder().encodeToString(this.getPrivateKeyString().getBytes())); + } InputStream keyInputStream = this.loadConfigInputStream(this.getPrivateKeyString(), this.getPrivateKeyPath(), this.privateKeyContent, "privateKeyPath"); try { diff --git a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java index 5a506e72f..72750e01c 100644 --- a/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java +++ b/weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java @@ -1,7 +1,16 @@ package com.github.binarywang.wxpay.config; +import com.github.binarywang.wxpay.exception.WxPayException; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.pqc.jcajce.provider.util.KeyUtil; import org.testng.annotations.Test; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.SecureRandom; +import java.security.Security; +import java.util.Base64; + /** *
* Created by BinaryWang on 2017/6/18. @@ -44,4 +53,20 @@ public class WxPayConfigTest { payConfig.setKeyString("MIIKmgIBAzCCCmQGCS..."); payConfig.initSSLContext(); } + + + @Test + public void testInitApiV3HttpClient() throws Exception { + Security.addProvider(new BouncyCastleProvider()); + KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA","BC"); + keyPairGenerator.initialize(2048,new SecureRandom()); + KeyPair keyPair = keyPairGenerator.genKeyPair(); + byte[] encoded = keyPair.getPrivate().getEncoded(); + // 模拟用户配置 + String privateKeyString = Base64.getEncoder().encodeToString(encoded); + payConfig.setPrivateKeyString(privateKeyString); + payConfig.setApiV3Key("Test"); + payConfig.initApiV3HttpClient(); + } + } -- Gitee