#788 批量修改所有使用字符串的getBytes方法的地方,显式使用utf-8编码,以免某些场景下出问题

This commit is contained in:
Binary Wang
2018-10-12 20:22:16 +08:00
parent ec5cc69abb
commit 34cea664ba
8 changed files with 72 additions and 73 deletions

View File

@@ -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"));
}