mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 18:46:10 +08:00 
			
		
		
		
	#788 批量修改所有使用字符串的getBytes方法的地方,显式使用utf-8编码,以免某些场景下出问题
This commit is contained in:
		| @ -1,6 +1,7 @@ | ||||
| package com.github.binarywang.wxpay.bean.notify; | ||||
|  | ||||
| import java.io.Serializable; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import javax.crypto.Cipher; | ||||
| import javax.crypto.spec.SecretKeySpec; | ||||
|  | ||||
| @ -48,7 +49,7 @@ public class WxPayRefundNotifyResult extends BaseWxPayResult implements Serializ | ||||
|     String reqInfoString = result.getReqInfoString(); | ||||
|     try { | ||||
|       final String keyMd5String = DigestUtils.md5Hex(mchKey).toLowerCase(); | ||||
|       SecretKeySpec key = new SecretKeySpec(keyMd5String.getBytes(), "AES"); | ||||
|       SecretKeySpec key = new SecretKeySpec(keyMd5String.getBytes(StandardCharsets.UTF_8), "AES"); | ||||
|  | ||||
|       Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | ||||
|       cipher.init(Cipher.DECRYPT_MODE, key); | ||||
|  | ||||
| @ -4,6 +4,7 @@ import java.io.ByteArrayInputStream; | ||||
| import java.io.IOException; | ||||
| import java.io.Serializable; | ||||
| import java.math.BigDecimal; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import javax.xml.parsers.DocumentBuilderFactory; | ||||
| @ -192,7 +193,7 @@ public abstract class BaseWxPayResult implements Serializable { | ||||
|       this.xmlDoc = DocumentBuilderFactory | ||||
|         .newInstance() | ||||
|         .newDocumentBuilder() | ||||
|         .parse(new ByteArrayInputStream(this.xmlString.getBytes("UTF-8"))); | ||||
|         .parse(new ByteArrayInputStream(this.xmlString.getBytes(StandardCharsets.UTF_8))); | ||||
|       return xmlDoc; | ||||
|     } catch (SAXException | IOException | ParserConfigurationException e) { | ||||
|       throw new RuntimeException("非法的xml文本内容:" + this.xmlString); | ||||
|  | ||||
| @ -3,6 +3,7 @@ package com.github.binarywang.wxpay.service.impl; | ||||
| import java.io.File; | ||||
| import java.io.FileReader; | ||||
| import java.io.IOException; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import java.nio.file.Files; | ||||
| import java.nio.file.Path; | ||||
| import java.security.PublicKey; | ||||
| @ -126,7 +127,7 @@ public class EntPayServiceImpl implements EntPayService { | ||||
|           .getPublicKey((SubjectPublicKeyInfo) reader.readObject()); | ||||
|  | ||||
|         cipher.init(Cipher.ENCRYPT_MODE, publicKey); | ||||
|         byte[] encrypt = cipher.doFinal(srcString.getBytes()); | ||||
|         byte[] encrypt = cipher.doFinal(srcString.getBytes(StandardCharsets.UTF_8)); | ||||
|         return Base64.encodeBase64String(encrypt); | ||||
|       } | ||||
|     } catch (Exception e) { | ||||
| @ -138,7 +139,7 @@ public class EntPayServiceImpl implements EntPayService { | ||||
|     try { | ||||
|       String publicKeyStr = this.getPublicKey(); | ||||
|       Path tmpFile = Files.createTempFile("payToBank", ".pem"); | ||||
|       Files.write(tmpFile, publicKeyStr.getBytes()); | ||||
|       Files.write(tmpFile, publicKeyStr.getBytes(StandardCharsets.UTF_8)); | ||||
|       return tmpFile.toFile(); | ||||
|     } catch (Exception e) { | ||||
|       throw new WxPayException("生成加密公钥文件时发生异常", e); | ||||
| @ -162,7 +163,7 @@ public class EntPayServiceImpl implements EntPayService { | ||||
|       "p7kM7BoaY2goFgYAe4DsI8Fh33dCOiKyVwIDAQAB\n" + | ||||
|       "-----END RSA PUBLIC KEY-----"; | ||||
|     Path tmpFile = Files.createTempFile("payToBank", ".pem"); | ||||
|     Files.write(tmpFile, key.getBytes()); | ||||
|     Files.write(tmpFile, key.getBytes(StandardCharsets.UTF_8)); | ||||
|     System.out.println(new EntPayServiceImpl(null).encryptRSA(tmpFile.toFile(), "111111")); | ||||
|   } | ||||
|  | ||||
|  | ||||
| @ -1,19 +1,20 @@ | ||||
| package com.github.binarywang.wxpay.bean.notify; | ||||
|  | ||||
| import java.math.BigInteger; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import java.security.MessageDigest; | ||||
| import javax.crypto.Cipher; | ||||
| import javax.crypto.spec.SecretKeySpec; | ||||
| import javax.inject.Inject; | ||||
|  | ||||
| import org.apache.commons.codec.binary.Base64; | ||||
| import org.testng.annotations.*; | ||||
|  | ||||
| import com.github.binarywang.wxpay.config.WxPayConfig; | ||||
| import com.github.binarywang.wxpay.exception.WxPayException; | ||||
| import com.github.binarywang.wxpay.testbase.ApiTestModule; | ||||
| import org.apache.commons.codec.binary.Base64; | ||||
| import org.testng.annotations.Guice; | ||||
| import org.testng.annotations.Test; | ||||
|  | ||||
| import javax.crypto.Cipher; | ||||
| import javax.crypto.spec.SecretKeySpec; | ||||
| import javax.inject.Inject; | ||||
| import java.math.BigInteger; | ||||
| import java.security.MessageDigest; | ||||
|  | ||||
| import static org.testng.Assert.assertNotNull; | ||||
| import static org.testng.Assert.*; | ||||
|  | ||||
| /** | ||||
|  * <pre> | ||||
| @ -71,10 +72,10 @@ public class WxPayRefundNotifyResultTest { | ||||
|  | ||||
|     Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); | ||||
|     final MessageDigest md5 = MessageDigest.getInstance("MD5"); | ||||
|     md5.update(this.wxPayConfig.getMchKey().getBytes()); | ||||
|     md5.update(this.wxPayConfig.getMchKey().getBytes(StandardCharsets.UTF_8)); | ||||
|     final String keyMd5String = new BigInteger(1, md5.digest()).toString(16).toLowerCase(); | ||||
|     SecretKeySpec key = new SecretKeySpec(keyMd5String.getBytes(), "AES"); | ||||
|     SecretKeySpec key = new SecretKeySpec(keyMd5String.getBytes(StandardCharsets.UTF_8), "AES"); | ||||
|     cipher.init(Cipher.ENCRYPT_MODE, key); | ||||
|     System.out.println(Base64.encodeBase64String(cipher.doFinal(xml.getBytes()))); | ||||
|     System.out.println(Base64.encodeBase64String(cipher.doFinal(xml.getBytes(StandardCharsets.UTF_8)))); | ||||
|   } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang