diff --git a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java index d44791eb1..f02d087b7 100755 --- a/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java +++ b/weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java @@ -5,6 +5,7 @@ import lombok.AllArgsConstructor; import lombok.Data; import me.chanjar.weixin.common.error.WxRuntimeException; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.StringUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; @@ -65,7 +66,7 @@ public class WxCryptUtil { public WxCryptUtil(String token, String encodingAesKey, String appidOrCorpid) { this.token = token; this.appidOrCorpid = appidOrCorpid; - this.aesKey = Base64.decodeBase64(CharMatcher.whitespace().removeFrom(encodingAesKey)); + this.aesKey = Base64.decodeBase64(StringUtils.remove(encodingAesKey, " ")); } private static String extractEncryptPart(String xml) { diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java index 7b09bf4ad..35b5946ed 100755 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java @@ -1,10 +1,10 @@ package me.chanjar.weixin.cp.util.crypto; -import com.google.common.base.CharMatcher; import com.google.common.io.BaseEncoding; import me.chanjar.weixin.common.error.WxErrorException; 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; @@ -28,7 +28,7 @@ public class WxCpCryptUtil extends WxCryptUtil { this.token = token; this.appidOrCorpid = corpId; - this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey)); + this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " ")); } /** diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpTpCryptUtil.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpTpCryptUtil.java index 900f7ea8a..b9c5f2a6d 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpTpCryptUtil.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpTpCryptUtil.java @@ -4,6 +4,9 @@ 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.lang3.StringUtils; + +import java.util.Base64; /** * @author someone @@ -24,7 +27,7 @@ public class WxCpTpCryptUtil extends WxCryptUtil { this.token = token; this.appidOrCorpid = corpId; - this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey)); + this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " ")); } diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtilTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtilTest.java index 62fbce13b..7ec2477b4 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtilTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtilTest.java @@ -1,8 +1,7 @@ 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.apache.commons.lang3.StringUtils; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; @@ -16,8 +15,8 @@ public class WxCpCryptUtilTest { 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 + "=")); + final byte[] guava = java.util.Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " ")); + final byte[] guava1 = java.util.Base64.getDecoder().decode(StringUtils.remove(encodingAesKey + "=", " ")); assertEquals(commonsCodec, guava); assertEquals(guava1, guava); } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/crypt/WxMaCryptUtils.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/crypt/WxMaCryptUtils.java index f1a05d001..0222265e4 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/crypt/WxMaCryptUtils.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/crypt/WxMaCryptUtils.java @@ -14,6 +14,7 @@ import com.google.common.base.CharMatcher; import com.google.common.io.BaseEncoding; import me.chanjar.weixin.common.error.WxRuntimeException; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.StringUtils; import org.bouncycastle.jce.provider.BouncyCastleProvider; import cn.binarywang.wx.miniapp.config.WxMaConfig; @@ -28,7 +29,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 = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(config.getAesKey())); + this.aesKey = java.util.Base64.getDecoder().decode(StringUtils.remove(config.getAesKey(), " ")); } /** diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/crypto/WxMpCryptUtil.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/crypto/WxMpCryptUtil.java index 9f22dbed3..b14023ef9 100755 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/crypto/WxMpCryptUtil.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/crypto/WxMpCryptUtil.java @@ -20,6 +20,9 @@ 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.lang3.StringUtils; + +import java.util.Base64; public class WxMpCryptUtil extends me.chanjar.weixin.common.util.crypto.WxCryptUtil { @@ -40,7 +43,7 @@ public class WxMpCryptUtil extends me.chanjar.weixin.common.util.crypto.WxCryptU this.token = token; this.appidOrCorpid = appId; - this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey)); + this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " ")); } } diff --git a/weixin-java-open/src/main/java/me/chanjar/weixin/open/util/WxOpenCryptUtil.java b/weixin-java-open/src/main/java/me/chanjar/weixin/open/util/WxOpenCryptUtil.java index b507e0daa..e6c8ce992 100644 --- a/weixin-java-open/src/main/java/me/chanjar/weixin/open/util/WxOpenCryptUtil.java +++ b/weixin-java-open/src/main/java/me/chanjar/weixin/open/util/WxOpenCryptUtil.java @@ -3,6 +3,9 @@ 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.lang3.StringUtils; + +import java.util.Base64; /** * @author 007 @@ -25,6 +28,6 @@ public class WxOpenCryptUtil extends me.chanjar.weixin.common.util.crypto.WxCryp this.token = token; this.appidOrCorpid = appId; - this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey)); + this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " ")); } } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/AesUtils.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/AesUtils.java index 2c8c40252..b4a97ba88 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/AesUtils.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/AesUtils.java @@ -1,7 +1,5 @@ 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 javax.crypto.Cipher; @@ -10,6 +8,7 @@ import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; @@ -58,7 +57,7 @@ public class AesUtils { } public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext) - throws GeneralSecurityException, IOException { + throws GeneralSecurityException, IOException { try { Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); @@ -68,7 +67,7 @@ public class AesUtils { cipher.init(Cipher.DECRYPT_MODE, key, spec); cipher.updateAAD(associatedData); - return new String(cipher.doFinal(BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(ciphertext))), "utf-8"); + return new String(cipher.doFinal(Base64.getDecoder().decode(StringUtils.remove(ciphertext, " "))), StandardCharsets.UTF_8); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { throw new IllegalStateException(e); } catch (InvalidKeyException | InvalidAlgorithmParameterException e) { @@ -76,7 +75,7 @@ public class AesUtils { } } - public static String decryptToString(String associatedData, String nonce, String ciphertext,String apiV3Key) + public static String decryptToString(String associatedData, String nonce, String ciphertext, String apiV3Key) throws GeneralSecurityException, IOException { try { Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding"); @@ -87,7 +86,7 @@ public class AesUtils { cipher.init(Cipher.DECRYPT_MODE, key, spec); cipher.updateAAD(associatedData.getBytes()); - return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8"); + return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), StandardCharsets.UTF_8); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { throw new IllegalStateException(e); } catch (InvalidKeyException | InvalidAlgorithmParameterException e) { @@ -116,9 +115,9 @@ public class AesUtils { public static String HMACSHA256(String data, String key) { try { Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); - SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"); + SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); sha256_HMAC.init(secret_key); - byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8")); + byte[] array = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); for (byte item : array) { sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));