diff --git a/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/util/encrypt/AesUtils.java b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/util/encrypt/AesUtils.java index 77a3b4733..abe43560e 100644 --- a/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/util/encrypt/AesUtils.java +++ b/cf-framework/cf-common/src/main/java/com/cf/imes/framework/common/util/encrypt/AesUtils.java @@ -1,13 +1,12 @@ package com.cf.imes.framework.common.util.encrypt; -import cn.hutool.core.util.CharsetUtil; import lombok.extern.slf4j.Slf4j; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; -import javax.crypto.spec.IvParameterSpec; -import java.security.GeneralSecurityException; +import javax.crypto.spec.GCMParameterSpec; +import javax.crypto.spec.SecretKeySpec; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Base64; @@ -23,7 +22,7 @@ public class AesUtils { private static final String AES_ALG = "AES"; - private static final String AES_CBC_PCK_ALG = "AES/CBC/PKCS5Padding"; + private static final String DEFAULT_CIPHER_ALGORITHM = "AES/GCM/NoPadding"; /** @@ -34,14 +33,14 @@ public class AesUtils { * @return * @throws Exception */ - public static String decrypt(String content, String aesKey) { + public static String decrypt(String base64Content, String aesKey) { try { - Cipher cipher = Cipher.getInstance(AES_CBC_PCK_ALG); - IvParameterSpec iv = new IvParameterSpec(initIv(AES_CBC_PCK_ALG)); - cipher.init(Cipher.DECRYPT_MODE, generateKey(aesKey.getBytes()), iv); - - byte[] cleanBytes = cipher.doFinal(Base64.getDecoder().decode(content.getBytes())); - return new String(cleanBytes, CharsetUtil.UTF_8); + byte[] content = Base64.getDecoder().decode(base64Content); + GCMParameterSpec params = new GCMParameterSpec(128, content, 0, 12); + Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); + cipher.init(Cipher.DECRYPT_MODE, getSecretKey(aesKey), params); + byte[] decryptData = cipher.doFinal(content, 12, content.length - 12); + return new String(decryptData); } catch (Exception e) { log.error("[AesUtils][decrypt]解密失败:{}", e.getMessage(), e); // nacos热发布配置,空字符串会导致nacos轮训刷新配置 @@ -56,44 +55,16 @@ public class AesUtils { * @return * @throws NoSuchAlgorithmException */ - private static SecretKey generateKey(byte[] bytes) throws NoSuchAlgorithmException { + private static SecretKey getSecretKey(String key) throws NoSuchAlgorithmException { // 生成随机数 SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); // 设置随机数种子 - random.setSeed(bytes); + random.setSeed(key.getBytes()); // aes算法生成器 KeyGenerator keyGen = KeyGenerator.getInstance(AES_ALG); keyGen.init(128, random); // 使用128位的密钥 - return keyGen.generateKey(); - } - - - /** - * 初始向量的方法, 全部为0. 这里的写法适合于其它算法,针对AES算法的话,IV值一定是128位的(16字节). - * - * @param fullAlg - * @return - * @throws GeneralSecurityException - */ - private static byte[] initIv(String fullAlg) { - - try { - Cipher cipher = Cipher.getInstance(fullAlg); - int blockSize = cipher.getBlockSize(); - byte[] iv = new byte[blockSize]; - for (int i = 0; i < blockSize; ++i) { - iv[i] = 0; - } - return iv; - } catch (Exception e) { - - int blockSize = 16; - byte[] iv = new byte[blockSize]; - for (int i = 0; i < blockSize; ++i) { - iv[i] = 0; - } - return iv; - } + SecretKey secretKey = keyGen.generateKey(); + return new SecretKeySpec(secretKey.getEncoded(), AES_ALG); } } diff --git a/cf-framework/cf-spring-boot-starter-biz-pay/src/main/java/com/cf/imes/framework/pay/core/client/impl/alipay/AbstractAlipayPayClient.java b/cf-framework/cf-spring-boot-starter-biz-pay/src/main/java/com/cf/imes/framework/pay/core/client/impl/alipay/AbstractAlipayPayClient.java index fd49fef79..6de95159a 100644 --- a/cf-framework/cf-spring-boot-starter-biz-pay/src/main/java/com/cf/imes/framework/pay/core/client/impl/alipay/AbstractAlipayPayClient.java +++ b/cf-framework/cf-spring-boot-starter-biz-pay/src/main/java/com/cf/imes/framework/pay/core/client/impl/alipay/AbstractAlipayPayClient.java @@ -4,7 +4,7 @@ import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.MapUtil; -import cn.hutool.core.util.StrUtil; +import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.http.HttpUtil; import com.cf.imes.framework.common.util.json.JsonUtils; import com.cf.imes.framework.common.util.object.ObjectUtils; @@ -98,7 +98,7 @@ public abstract class AbstractAlipayPayClient extends AbstractPayClient) () -> { - throw new IllegalArgumentException(StrUtil.format("body({}) 的 trade_status 不正确", body)); + throw new IllegalArgumentException(CharSequenceUtil.format("body({}) 的 trade_status 不正确", body)); }); return PayOrderRespDTO.of(status, bodyObj.get("trade_no"), bodyObj.get("seller_id"), parseTime(params.get("gmt_payment")), bodyObj.get("out_trade_no"), body); @@ -126,7 +126,7 @@ public abstract class AbstractAlipayPayClient extends AbstractPayClient { - throw new IllegalArgumentException(StrUtil.format("body({}) 的 trade_status 不正确", response.getBody())); + throw new IllegalArgumentException(CharSequenceUtil.format("body({}) 的 trade_status 不正确", response.getBody())); }); return PayOrderRespDTO.of(status, response.getTradeNo(), response.getBuyerUserId(), LocalDateTimeUtil.of(response.getSendPayDate()), outTradeNo, response); @@ -248,11 +248,11 @@ public abstract class AbstractAlipayPayClient extends AbstractPayClient { + private static final String UNKOWN_API_VERSION_NOTIFICATION = "未知的 API 版本(%s)"; + + private static final String SUCCESS_CODE = "SUCCESS"; + + private static final String PROCESSING_STATUS_CODE = "PROCESSING"; + protected WxPayService client; public AbstractWxPayClient(Long channelId, String channelCode, WxPayClientConfig config) { @@ -63,10 +69,10 @@ public abstract class AbstractWxPayClient extends AbstractPayClient implem * @return URL 访问地址 */ protected String formatFileUrl(String domain, String path) { - return StrUtil.format("{}/admin-api/infra/file/{}/get/{}", domain, getId(), path); + return CharSequenceUtil.format("{}/admin-api/infra/file/{}/get/{}", domain, getId(), path); } } diff --git a/cf-framework/cf-spring-boot-starter-websocket/src/main/java/com/cf/imes/framework/websocket/core/sender/AbstractWebSocketMessageSender.java b/cf-framework/cf-spring-boot-starter-websocket/src/main/java/com/cf/imes/framework/websocket/core/sender/AbstractWebSocketMessageSender.java index f2dd00143..8ec8f756a 100644 --- a/cf-framework/cf-spring-boot-starter-websocket/src/main/java/com/cf/imes/framework/websocket/core/sender/AbstractWebSocketMessageSender.java +++ b/cf-framework/cf-spring-boot-starter-websocket/src/main/java/com/cf/imes/framework/websocket/core/sender/AbstractWebSocketMessageSender.java @@ -1,7 +1,7 @@ package com.cf.imes.framework.websocket.core.sender; import cn.hutool.core.collection.CollUtil; -import cn.hutool.core.util.StrUtil; +import cn.hutool.core.text.CharSequenceUtil; import com.cf.imes.framework.common.util.json.JsonUtils; import com.cf.imes.framework.websocket.core.message.JsonWebSocketMessage; import com.cf.imes.framework.websocket.core.session.WebSocketSessionManager; @@ -53,7 +53,7 @@ public abstract class AbstractWebSocketMessageSender implements WebSocketMessage public void send(String sessionId, Integer userType, Long userId, String messageType, String messageContent) { // 1. 获得 Session 列表 List sessions = Collections.emptyList(); - if (StrUtil.isNotEmpty(sessionId)) { + if (CharSequenceUtil.isNotEmpty(sessionId)) { WebSocketSession session = sessionManager.getSession(sessionId); if (session != null) { sessions = Collections.singletonList(session);