Skip to content

Commit 29b4dbd

Browse files
committed
🎨 #1610 换用guava的相应方法实现base64解码,避免因commons-codec版本问题导致解码异常
1 parent a81550f commit 29b4dbd

File tree

8 files changed

+45
-37
lines changed

8 files changed

+45
-37
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import javax.xml.parsers.DocumentBuilderFactory;
1313
import javax.xml.parsers.ParserConfigurationException;
1414

15+
import com.google.common.base.CharMatcher;
16+
import com.google.common.io.BaseEncoding;
1517
import org.apache.commons.codec.binary.Base64;
1618
import org.w3c.dom.Document;
1719
import org.w3c.dom.Element;
@@ -64,7 +66,7 @@ public WxCryptUtil() {
6466
public WxCryptUtil(String token, String encodingAesKey, String appidOrCorpid) {
6567
this.token = token;
6668
this.appidOrCorpid = appidOrCorpid;
67-
this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
69+
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
6870
}
6971

7072
private static String extractEncryptPart(String xml) {
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
1-
/**
2-
* 对公众平台发送给公众账号的消息加解密示例代码.
3-
*
4-
* @copyright Copyright (c) 1998-2014 Tencent Inc.
5-
* <p>
6-
* 针对org.apache.commons.codec.binary.Base64,
7-
* 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本)
8-
* 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
9-
* <p>
10-
* 针对org.apache.commons.codec.binary.Base64,
11-
* 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本)
12-
* 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
13-
*/
14-
15-
// ------------------------------------------------------------------------
16-
17-
/**
18-
* 针对org.apache.commons.codec.binary.Base64,
19-
* 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本)
20-
* 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
21-
*/
221
package me.chanjar.weixin.cp.util.crypto;
232

3+
import com.google.common.base.CharMatcher;
4+
import com.google.common.io.BaseEncoding;
245
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
256
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
26-
import org.apache.commons.codec.binary.Base64;
277

288
public class WxCpCryptUtil extends WxCryptUtil {
29-
30-
/**
31-
* 构造函数
32-
*
33-
* @param wxCpConfigStorage
34-
*/
359
public WxCpCryptUtil(WxCpConfigStorage wxCpConfigStorage) {
3610
/*
3711
* @param token 公众平台上,开发者设置的token
@@ -44,8 +18,7 @@ public WxCpCryptUtil(WxCpConfigStorage wxCpConfigStorage) {
4418

4519
this.token = token;
4620
this.appidOrCorpid = corpId;
47-
this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
21+
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
4822
}
4923

50-
5124
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpTpCryptUtil.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package me.chanjar.weixin.cp.util.crypto;
22

3+
import com.google.common.base.CharMatcher;
4+
import com.google.common.io.BaseEncoding;
35
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
46
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
5-
import org.apache.commons.codec.binary.Base64;
67

78
/**
89
* @author someone
@@ -23,7 +24,7 @@ public WxCpTpCryptUtil(WxCpTpConfigStorage wxCpTpConfigStorage) {
2324

2425
this.token = token;
2526
this.appidOrCorpid = corpId;
26-
this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
27+
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
2728
}
2829

2930

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package me.chanjar.weixin.cp.util.crypto;
2+
3+
import com.google.common.base.CharMatcher;
4+
import com.google.common.io.BaseEncoding;
5+
import org.apache.commons.codec.binary.Base64;
6+
import org.testng.annotations.Test;
7+
8+
import static org.testng.Assert.assertEquals;
9+
10+
/**
11+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
12+
* @date 2020-06-11
13+
*/
14+
public class WxCpCryptUtilTest {
15+
@Test
16+
public void test() {
17+
String encodingAesKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C";
18+
final byte[] commonsCodec = Base64.decodeBase64(encodingAesKey + "=");
19+
final byte[] guava = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
20+
final byte[] guava1 = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey + "="));
21+
assertEquals(commonsCodec, guava);
22+
assertEquals(guava1, guava);
23+
}
24+
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/crypt/WxMaCryptUtils.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import javax.crypto.spec.IvParameterSpec;
1111
import javax.crypto.spec.SecretKeySpec;
1212

13+
import com.google.common.base.CharMatcher;
14+
import com.google.common.io.BaseEncoding;
1315
import org.apache.commons.codec.binary.Base64;
1416
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1517

@@ -25,7 +27,7 @@ public class WxMaCryptUtils extends me.chanjar.weixin.common.util.crypto.WxCrypt
2527
public WxMaCryptUtils(WxMaConfig config) {
2628
this.appidOrCorpid = config.getAppid();
2729
this.token = config.getToken();
28-
this.aesKey = Base64.decodeBase64(config.getAesKey() + "=");
30+
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(config.getAesKey()));
2931
}
3032

3133
/**

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/crypto/WxMpCryptUtil.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package me.chanjar.weixin.mp.util.crypto;
1919

20+
import com.google.common.base.CharMatcher;
21+
import com.google.common.io.BaseEncoding;
2022
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
2123
import org.apache.commons.codec.binary.Base64;
2224

@@ -39,7 +41,7 @@ public WxMpCryptUtil(WxMpConfigStorage wxMpConfigStorage) {
3941

4042
this.token = token;
4143
this.appidOrCorpid = appId;
42-
this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
44+
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
4345
}
4446

4547
}

weixin-java-open/src/main/java/me/chanjar/weixin/open/util/WxOpenCryptUtil.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.chanjar.weixin.open.util;
22

3+
import com.google.common.base.CharMatcher;
4+
import com.google.common.io.BaseEncoding;
35
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
46
import org.apache.commons.codec.binary.Base64;
57

@@ -24,6 +26,6 @@ public WxOpenCryptUtil(WxOpenConfigStorage wxOpenConfigStorage) {
2426

2527
this.token = token;
2628
this.appidOrCorpid = appId;
27-
this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
29+
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
2830
}
2931
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/AesUtils.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.binarywang.wxpay.v3.util;
22

3+
import com.google.common.base.CharMatcher;
4+
import com.google.common.io.BaseEncoding;
35
import org.apache.commons.lang3.StringUtils;
46

57
import java.io.IOException;
@@ -41,7 +43,7 @@ public String decryptToString(byte[] associatedData, byte[] nonce, String cipher
4143
cipher.init(Cipher.DECRYPT_MODE, key, spec);
4244
cipher.updateAAD(associatedData);
4345

44-
return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8");
46+
return new String(cipher.doFinal(BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(ciphertext))), "utf-8");
4547
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
4648
throw new IllegalStateException(e);
4749
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {

0 commit comments

Comments
 (0)