From 7aad8da6f6f99b7411958a624e01151885ed5876 Mon Sep 17 00:00:00 2001 From: rainv123 <2148537152@qq.com> Date: Thu, 12 Mar 2026 17:34:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E7=A0=81=E9=AA=8C=E8=AF=81=E9=94=99=E8=AF=AF=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaozhi/common/utils/Sm2DecryptUtil.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/common/utils/Sm2DecryptUtil.java b/main/manager-api/src/main/java/xiaozhi/common/utils/Sm2DecryptUtil.java index 5dd1a312..332e9c80 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/utils/Sm2DecryptUtil.java +++ b/main/manager-api/src/main/java/xiaozhi/common/utils/Sm2DecryptUtil.java @@ -12,28 +12,29 @@ import xiaozhi.modules.sys.service.SysParamsService; * 封装了重复的SM2解密、验证码提取和验证逻辑 */ public class Sm2DecryptUtil { - + /** * 验证码长度 */ private static final int CAPTCHA_LENGTH = 5; - + /** * 解密SM2加密内容,提取验证码并验证 + * * @param encryptedPassword SM2加密的密码字符串 - * @param captchaId 验证码ID - * @param captchaService 验证码服务 - * @param sysParamsService 系统参数服务 + * @param captchaId 验证码ID + * @param captchaService 验证码服务 + * @param sysParamsService 系统参数服务 * @return 解密后的实际密码 */ - public static String decryptAndValidateCaptcha(String encryptedPassword, String captchaId, - CaptchaService captchaService, SysParamsService sysParamsService) { + public static String decryptAndValidateCaptcha(String encryptedPassword, String captchaId, + CaptchaService captchaService, SysParamsService sysParamsService) { // 获取SM2私钥 String privateKeyStr = sysParamsService.getValue(Constant.SM2_PRIVATE_KEY, true); if (StringUtils.isBlank(privateKeyStr)) { throw new RenException(ErrorCode.SM2_KEY_NOT_CONFIGURED); } - + // 使用SM2私钥解密密码 String decryptedContent; try { @@ -41,19 +42,20 @@ public class Sm2DecryptUtil { } catch (Exception e) { throw new RenException(ErrorCode.SM2_DECRYPT_ERROR); } - + // 分离验证码和密码:前5位是验证码,后面是密码 if (decryptedContent.length() > CAPTCHA_LENGTH) { String embeddedCaptcha = decryptedContent.substring(0, CAPTCHA_LENGTH); String actualPassword = decryptedContent.substring(CAPTCHA_LENGTH); - - // 验证嵌入的验证码是否正确 + boolean embeddedCaptchaValid = captchaService.validate(captchaId, embeddedCaptcha, true); if (!embeddedCaptchaValid) { throw new RenException(ErrorCode.SMS_CAPTCHA_ERROR); } - + return actualPassword; + } else if (decryptedContent.length() > 0) { + throw new RenException(ErrorCode.SMS_CAPTCHA_ERROR); } else { throw new RenException(ErrorCode.SM2_DECRYPT_ERROR); }