Quellcode durchsuchen

:art: #1610 换用guava的相应方法实现base64解码,避免因commons-codec版本问题导致解码异常

Binary Wang vor 5 Jahren
Ursprung
Commit
29b4dbd601

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

@@ -12,6 +12,8 @@ import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
+import com.google.common.base.CharMatcher;
+import com.google.common.io.BaseEncoding;
 import org.apache.commons.codec.binary.Base64;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
@@ -64,7 +66,7 @@ public class WxCryptUtil {
   public WxCryptUtil(String token, String encodingAesKey, String appidOrCorpid) {
     this.token = token;
     this.appidOrCorpid = appidOrCorpid;
-    this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
+    this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
   }
 
   private static String extractEncryptPart(String xml) {

+ 3 - 30
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java

@@ -1,37 +1,11 @@
-/**
- * 对公众平台发送给公众账号的消息加解密示例代码.
- *
- * @copyright Copyright (c) 1998-2014 Tencent Inc.
- * <p>
- * 针对org.apache.commons.codec.binary.Base64,
- * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本)
- * 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
- * <p>
- * 针对org.apache.commons.codec.binary.Base64,
- * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本)
- * 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
- */
-
-// ------------------------------------------------------------------------
-
-/**
- * 针对org.apache.commons.codec.binary.Base64,
- * 需要导入架包commons-codec-1.9(或commons-codec-1.8等其他版本)
- * 官方下载地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi
- */
 package me.chanjar.weixin.cp.util.crypto;
 
+import com.google.common.base.CharMatcher;
+import com.google.common.io.BaseEncoding;
 import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
 import me.chanjar.weixin.cp.config.WxCpConfigStorage;
-import org.apache.commons.codec.binary.Base64;
 
 public class WxCpCryptUtil extends WxCryptUtil {
-
-  /**
-   * 构造函数
-   *
-   * @param wxCpConfigStorage
-   */
   public WxCpCryptUtil(WxCpConfigStorage wxCpConfigStorage) {
     /*
      * @param token          公众平台上,开发者设置的token
@@ -44,8 +18,7 @@ public class WxCpCryptUtil extends WxCryptUtil {
 
     this.token = token;
     this.appidOrCorpid = corpId;
-    this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
+    this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
   }
 
-
 }

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

@@ -1,8 +1,9 @@
 package me.chanjar.weixin.cp.util.crypto;
 
+import com.google.common.base.CharMatcher;
+import com.google.common.io.BaseEncoding;
 import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
 import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
-import org.apache.commons.codec.binary.Base64;
 
 /**
  * @author someone
@@ -23,7 +24,7 @@ public class WxCpTpCryptUtil extends WxCryptUtil {
 
     this.token = token;
     this.appidOrCorpid = corpId;
-    this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
+    this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
   }
 
 

+ 24 - 0
weixin-java-cp/src/test/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtilTest.java

@@ -0,0 +1,24 @@
+package me.chanjar.weixin.cp.util.crypto;
+
+import com.google.common.base.CharMatcher;
+import com.google.common.io.BaseEncoding;
+import org.apache.commons.codec.binary.Base64;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+/**
+ * @author <a href="https://github.com/binarywang">Binary Wang</a>
+ * @date 2020-06-11
+ */
+public class WxCpCryptUtilTest {
+  @Test
+  public void test() {
+    String encodingAesKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C";
+    final byte[] commonsCodec = Base64.decodeBase64(encodingAesKey + "=");
+    final byte[] guava = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
+    final byte[] guava1 = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey + "="));
+    assertEquals(commonsCodec, guava);
+    assertEquals(guava1, guava);
+  }
+}

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

@@ -10,6 +10,8 @@ import javax.crypto.Cipher;
 import javax.crypto.spec.IvParameterSpec;
 import javax.crypto.spec.SecretKeySpec;
 
+import com.google.common.base.CharMatcher;
+import com.google.common.io.BaseEncoding;
 import org.apache.commons.codec.binary.Base64;
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 
@@ -25,7 +27,7 @@ public class WxMaCryptUtils extends me.chanjar.weixin.common.util.crypto.WxCrypt
   public WxMaCryptUtils(WxMaConfig config) {
     this.appidOrCorpid = config.getAppid();
     this.token = config.getToken();
-    this.aesKey = Base64.decodeBase64(config.getAesKey() + "=");
+    this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(config.getAesKey()));
   }
 
   /**

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

@@ -17,6 +17,8 @@
  */
 package me.chanjar.weixin.mp.util.crypto;
 
+import com.google.common.base.CharMatcher;
+import com.google.common.io.BaseEncoding;
 import me.chanjar.weixin.mp.config.WxMpConfigStorage;
 import org.apache.commons.codec.binary.Base64;
 
@@ -39,7 +41,7 @@ public class WxMpCryptUtil extends me.chanjar.weixin.common.util.crypto.WxCryptU
 
     this.token = token;
     this.appidOrCorpid = appId;
-    this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
+    this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
   }
 
 }

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

@@ -1,5 +1,7 @@
 package me.chanjar.weixin.open.util;
 
+import com.google.common.base.CharMatcher;
+import com.google.common.io.BaseEncoding;
 import me.chanjar.weixin.open.api.WxOpenConfigStorage;
 import org.apache.commons.codec.binary.Base64;
 
@@ -24,6 +26,6 @@ public class WxOpenCryptUtil extends me.chanjar.weixin.common.util.crypto.WxCryp
 
     this.token = token;
     this.appidOrCorpid = appId;
-    this.aesKey = Base64.decodeBase64(encodingAesKey + "=");
+    this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
   }
 }

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

@@ -1,5 +1,7 @@
 package com.github.binarywang.wxpay.v3.util;
 
+import com.google.common.base.CharMatcher;
+import com.google.common.io.BaseEncoding;
 import org.apache.commons.lang3.StringUtils;
 
 import java.io.IOException;
@@ -41,7 +43,7 @@ public class AesUtils {
       cipher.init(Cipher.DECRYPT_MODE, key, spec);
       cipher.updateAAD(associatedData);
 
-      return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8");
+      return new String(cipher.doFinal(BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(ciphertext))), "utf-8");
     } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
       throw new IllegalStateException(e);
     } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {