uft-8 改为 StandardCharsets.UTF_8

This commit is contained in:
2023-12-15 16:06:21 +08:00
parent ff9f4faecb
commit 517320e714
11 changed files with 33 additions and 25 deletions

View File

@@ -2175,7 +2175,7 @@ public class SpringBootTestController {
// 解密data // 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(data1), byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(data1),
operatorSecret.getBytes(), operatorSecret.getBytes()); operatorSecret.getBytes(), operatorSecret.getBytes());
String dataStr = new String(plainText, "UTF-8"); String dataStr = new String(plainText, StandardCharsets.UTF_8);
Map<String, String> resultMap = (Map<String, String>) JSON.parse(dataStr); Map<String, String> resultMap = (Map<String, String>) JSON.parse(dataStr);
} }

View File

@@ -1,6 +1,7 @@
package com.jsowell.common.util.wxplatform; package com.jsowell.common.util.wxplatform;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
/** /**
@@ -10,7 +11,7 @@ import java.util.Arrays;
* @Date 2023/7/27 16:18 * @Date 2023/7/27 16:18
*/ */
public class PKCS7Encoder { public class PKCS7Encoder {
static Charset CHARSET = Charset.forName("utf-8"); static Charset CHARSET = StandardCharsets.UTF_8;
static int BLOCK_SIZE = 32; static int BLOCK_SIZE = 32;
/** /**

View File

@@ -6,16 +6,16 @@ import org.w3c.dom.Element;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;
/** /**
* 提供接收和推送给公众平台消息的加解密接口(UTF8编码的字符串). * 提供接收和推送给公众平台消息的加解密接口(UTF8编码的字符串).
@@ -32,7 +32,7 @@ import javax.xml.parsers.DocumentBuilderFactory;
* </ol> * </ol>
*/ */
public class WXBizMsgCrypt { public class WXBizMsgCrypt {
static Charset CHARSET = Charset.forName("utf-8"); static Charset CHARSET = StandardCharsets.UTF_8;
Base64 base64 = new Base64(); Base64 base64 = new Base64();
byte[] aesKey; byte[] aesKey;
String token; String token;

View File

@@ -23,6 +23,7 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -47,7 +48,7 @@ public class WXXmlToMapUtil {
Map<String, String> data = new HashMap<>(); Map<String, String> data = new HashMap<>();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
InputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8")); InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
org.w3c.dom.Document doc = documentBuilder.parse(stream); org.w3c.dom.Document doc = documentBuilder.parse(stream);
doc.getDocumentElement().normalize(); doc.getDocumentElement().normalize();
NodeList nodeList = doc.getDocumentElement().getChildNodes(); NodeList nodeList = doc.getDocumentElement().getChildNodes();

View File

@@ -7,6 +7,7 @@ import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException; import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/** /**
* Redis使用FastJson序列化 * Redis使用FastJson序列化
@@ -14,7 +15,7 @@ import java.nio.charset.Charset;
* @author jsowell * @author jsowell
*/ */
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> { public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
private Class<T> clazz; private Class<T> clazz;

View File

@@ -5,6 +5,7 @@ import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec; import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException; import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
@@ -35,7 +36,7 @@ public class AesUtil {
cipher.init(Cipher.DECRYPT_MODE, key, spec); cipher.init(Cipher.DECRYPT_MODE, key, spec);
cipher.updateAAD(associatedData); cipher.updateAAD(associatedData);
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) { } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) { } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {

View File

@@ -6,6 +6,7 @@ import com.jsowell.wxpay.common.WeChatPayParameter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
@@ -36,7 +37,7 @@ public class WechatPayUtils {
*/ */
public static PrivateKey getPrivateKey(String filename) throws IOException { public static PrivateKey getPrivateKey(String filename) throws IOException {
// System.out.println("filename:" + filename); // System.out.println("filename:" + filename);
String content = new String(Files.readAllBytes(Paths.get(filename)), "utf-8"); String content = new String(Files.readAllBytes(Paths.get(filename)), StandardCharsets.UTF_8);
try { try {
String privateKey = content.replace("-----BEGIN PRIVATE KEY-----", "") String privateKey = content.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "") .replace("-----END PRIVATE KEY-----", "")
@@ -64,7 +65,7 @@ public class WechatPayUtils {
String nonceStr = getNonceStr(); String nonceStr = getNonceStr();
long timestamp = System.currentTimeMillis() / 1000; long timestamp = System.currentTimeMillis() / 1000;
String message = buildMessage(method, url, timestamp, nonceStr, body); String message = buildMessage(method, url, timestamp, nonceStr, body);
String signature = sign(message.getBytes("utf-8")); String signature = sign(message.getBytes(StandardCharsets.UTF_8));
return "WECHATPAY2-SHA256-RSA2048 " + "mchid=\"" + WeChatPayParameter.mchId + "\"," return "WECHATPAY2-SHA256-RSA2048 " + "mchid=\"" + WeChatPayParameter.mchId + "\","
+ "nonce_str=\"" + nonceStr + "\"," + "nonce_str=\"" + nonceStr + "\","
@@ -86,7 +87,7 @@ public class WechatPayUtils {
String nonceStr = getNonceStr(); String nonceStr = getNonceStr();
long timestamp = System.currentTimeMillis() / 1000; long timestamp = System.currentTimeMillis() / 1000;
String message = buildMessage(method, url, timestamp, nonceStr, body); String message = buildMessage(method, url, timestamp, nonceStr, body);
String signature = sign(message.getBytes("utf-8")); String signature = sign(message.getBytes(StandardCharsets.UTF_8));
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("timeStamp", String.valueOf(timestamp)); map.put("timeStamp", String.valueOf(timestamp));
@@ -170,7 +171,7 @@ public class WechatPayUtils {
//证书内容转成证书对象 //证书内容转成证书对象
CertificateFactory cf = CertificateFactory.getInstance("X509"); CertificateFactory cf = CertificateFactory.getInstance("X509");
X509Certificate x509Cert = (X509Certificate) cf.generateCertificate( X509Certificate x509Cert = (X509Certificate) cf.generateCertificate(
new ByteArrayInputStream(certStr.getBytes("utf-8")) new ByteArrayInputStream(certStr.getBytes(StandardCharsets.UTF_8))
); );
String serial_no = data.get(i).get("serial_no").toString().replaceAll("\"", ""); String serial_no = data.get(i).get("serial_no").toString().replaceAll("\"", "");
certificateMap.put(serial_no, x509Cert); certificateMap.put(serial_no, x509Cert);
@@ -222,7 +223,7 @@ public class WechatPayUtils {
//签名使用字段appId、timeStamp、nonceStr、package计算得出的签名值 //签名使用字段appId、timeStamp、nonceStr、package计算得出的签名值
String message = buildMessageTwo(appId, timestamp, nonceStr, packagestr); String message = buildMessageTwo(appId, timestamp, nonceStr, packagestr);
//获取对应的签名 //获取对应的签名
String signature = sign(message.getBytes("utf-8")); String signature = sign(message.getBytes(StandardCharsets.UTF_8));
// 组装返回 // 组装返回
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put("timeStamp", String.valueOf(timestamp)); map.put("timeStamp", String.valueOf(timestamp));

View File

@@ -14,6 +14,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.*; import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;
@@ -180,7 +181,7 @@ public class AMapUtils {
Signature signature = Signature.getInstance("SHA256WithRSA"); Signature signature = Signature.getInstance("SHA256WithRSA");
signature.initSign(priKey); signature.initSign(priKey);
signature.update(signContent.getBytes("UTF-8")); signature.update(signContent.getBytes(StandardCharsets.UTF_8));
byte[] signed = signature.sign(); byte[] signed = signature.sign();
return new String(Base64.getEncoder().encode(signed)); return new String(Base64.getEncoder().encode(signed));
} }
@@ -232,7 +233,7 @@ public class AMapUtils {
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
Signature signature = Signature.getInstance("SHA256WithRSA"); Signature signature = Signature.getInstance("SHA256WithRSA");
signature.initVerify(pubKey); signature.initVerify(pubKey);
signature.update(content.getBytes("UTF-8")); signature.update(content.getBytes(StandardCharsets.UTF_8));
return signature.verify(Base64.getDecoder().decode(sign.getBytes())); return signature.verify(Base64.getDecoder().decode(sign.getBytes()));
} }

View File

@@ -862,7 +862,7 @@ public class LianLianServiceImpl implements LianLianService {
String dataJson = JSONUtil.toJsonStr(data); String dataJson = JSONUtil.toJsonStr(data);
// 加密 // 加密
byte[] encryptText = Cryptos.aesEncrypt(dataJson.getBytes("UTF-8"), byte[] encryptText = Cryptos.aesEncrypt(dataJson.getBytes(StandardCharsets.UTF_8),
dataSecret.getBytes(), dataSecretIv.getBytes()); dataSecret.getBytes(), dataSecretIv.getBytes());
String strData = Encodes.encodeBase64(encryptText); String strData = Encodes.encodeBase64(encryptText);
@@ -886,7 +886,7 @@ public class LianLianServiceImpl implements LianLianService {
// 解密data // 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64((String) result.getData()), byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64((String) result.getData()),
dataSecret.getBytes(), dataSecretIv.getBytes()); dataSecret.getBytes(), dataSecretIv.getBytes());
String dataStr = new String(plainText, "UTF-8"); String dataStr = new String(plainText, StandardCharsets.UTF_8);
Map<String, String> resultMap = (Map<String, String>) JSON.parse(dataStr); Map<String, String> resultMap = (Map<String, String>) JSON.parse(dataStr);
token = resultMap.get("AccessToken"); token = resultMap.get("AccessToken");
// logger.info("token: {}", token); // logger.info("token: {}", token);
@@ -1616,7 +1616,7 @@ public class LianLianServiceImpl implements LianLianService {
String dataString = checkResultMap.get("Data"); String dataString = checkResultMap.get("Data");
// 解密data // 解密data
byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), dataSecret.getBytes(), dataSecretIv.getBytes()); byte[] plainText = Cryptos.aesDecrypt(Encodes.decodeBase64(dataString), dataSecret.getBytes(), dataSecretIv.getBytes());
String dataStr = new String(plainText, "UTF-8"); String dataStr = new String(plainText, StandardCharsets.UTF_8);
Map<String, String> resMap = (Map<String, String>) JSON.parse(dataStr); Map<String, String> resMap = (Map<String, String>) JSON.parse(dataStr);
String secret = resMap.get("OperatorSecret"); String secret = resMap.get("OperatorSecret");

View File

@@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory;
import javax.crypto.Mac; import javax.crypto.Mac;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
public class GBSignUtils { public class GBSignUtils {
@@ -42,7 +43,7 @@ public class GBSignUtils {
* @throws Exception * @throws Exception
*/ */
public static byte[] HmacMD5Encrypt(String encryptText, String encryptKey) throws Exception { public static byte[] HmacMD5Encrypt(String encryptText, String encryptKey) throws Exception {
byte[] data = encryptKey.getBytes("UTF-8"); byte[] data = encryptKey.getBytes(StandardCharsets.UTF_8);
// 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称 // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
SecretKey secretKey = new SecretKeySpec(data, "HmacMD5"); SecretKey secretKey = new SecretKeySpec(data, "HmacMD5");
// 生成一个指定 Mac 算法 的 Mac 对象 // 生成一个指定 Mac 算法 的 Mac 对象
@@ -50,7 +51,7 @@ public class GBSignUtils {
// 用给定密钥初始化 Mac 对象 // 用给定密钥初始化 Mac 对象
mac.init(secretKey); mac.init(secretKey);
byte[] text = encryptText.getBytes("UTF-8"); byte[] text = encryptText.getBytes(StandardCharsets.UTF_8);
// 完成 Mac 操作 // 完成 Mac 操作
return mac.doFinal(text); return mac.doFinal(text);
} }

View File

@@ -7,6 +7,7 @@ import com.jsowell.common.util.DateUtils;
import javax.crypto.Mac; import javax.crypto.Mac;
import javax.crypto.SecretKey; import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Locale; import java.util.Locale;
/** /**
@@ -28,10 +29,10 @@ public class LianLianUtils {
* @throws Exception * @throws Exception
*/ */
public static String encryptionHMAC(String source) throws Exception { public static String encryptionHMAC(String source) throws Exception {
SecretKey secretKey = new SecretKeySpec(MAC_KEY.getBytes("UTF-8"), ALGORITHM_MAC); SecretKey secretKey = new SecretKeySpec(MAC_KEY.getBytes(StandardCharsets.UTF_8), ALGORITHM_MAC);
Mac mac = Mac.getInstance(ALGORITHM_MAC); Mac mac = Mac.getInstance(ALGORITHM_MAC);
mac.init(secretKey); mac.init(secretKey);
mac.update(source.getBytes("UTF-8")); mac.update(source.getBytes(StandardCharsets.UTF_8));
return BytesUtil.binary(mac.doFinal(), 16).toUpperCase(Locale.ROOT); return BytesUtil.binary(mac.doFinal(), 16).toUpperCase(Locale.ROOT);
} }