diff --git a/src/main/java/com/ynxbd/common/helper/common/RSAHelper.java b/src/main/java/com/ynxbd/common/helper/common/RSAHelper.java index ff901e4..d37a5b6 100644 --- a/src/main/java/com/ynxbd/common/helper/common/RSAHelper.java +++ b/src/main/java/com/ynxbd/common/helper/common/RSAHelper.java @@ -2,8 +2,10 @@ package com.ynxbd.common.helper.common; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.ObjectUtils; import javax.crypto.Cipher; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.*; import java.security.interfaces.RSAPrivateKey; @@ -100,6 +102,8 @@ public class RSAHelper { return null; } + + /** * RSA私钥解密 * @@ -132,11 +136,44 @@ public class RSAHelper { return new String(cipher.doFinal(inputByte)); } catch (Exception e) { e.printStackTrace(); - log.error("[RSA]解密失败:[{}]" + e.getMessage()); + log.error("[RSA]解密失败:[{}]", e.getMessage()); + } + return null; + } + + + public static String URLEncode(String data) { + try { + if (ObjectUtils.isEmpty(data)) return null; + return URLEncoder.encode(data, String.valueOf(StandardCharsets.UTF_8)); + } catch (Exception e) { + e.printStackTrace(); } return null; } + // 加密方法(PKCS#1 v1.5填充) + public static String enPKCS1Padding(String plaintext, PublicKey publicKey) throws Exception { + Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); + cipher.init(Cipher.ENCRYPT_MODE, publicKey); + byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8)); + return java.util.Base64.getEncoder().encodeToString(encryptedBytes); + } + + // 解密方法 + public static String dePKCS1Padding(String encryptedText, PrivateKey privateKey) throws Exception { + Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); + cipher.init(Cipher.DECRYPT_MODE, privateKey); + byte[] decryptedBytes = cipher.doFinal(java.util.Base64.getDecoder().decode(encryptedText)); + return new String(decryptedBytes, StandardCharsets.UTF_8); + } + + // 去除空格、制表符、换行符等连续或多个空白符 + public static String replaceKey(String key) { + if (key == null) return null; + return key.replaceAll("\\s+", ""); + } + public static void main(String[] args) throws Exception { for (int i = 0; i < 100; i++) { diff --git a/src/main/java/com/ynxbd/wx/wxfactory/WxMedHelper.java b/src/main/java/com/ynxbd/wx/wxfactory/WxMedHelper.java index 9e2e296..8217be7 100644 --- a/src/main/java/com/ynxbd/wx/wxfactory/WxMedHelper.java +++ b/src/main/java/com/ynxbd/wx/wxfactory/WxMedHelper.java @@ -36,9 +36,9 @@ public class WxMedHelper { log.info("[医保]获取用户信息 openid={}, qrCode={}", openid, qrCode); MedicalUserInfo info = WxFactory.Medical.Common().getUserInfo(WxMedConfig.PARTNER_URL, openid, qrCode); if (info == null || !info.isSuccess()) { - String message = info == null ? "授权失败" : info.getMessage(); + String message = info == null ? "" : info.getMessage(); log.info("[医保授权]失败 {}", message); - throw new ServiceException(message); + throw new ServiceException("[医保授权]失败:" + message); } if (cardNo != null) {