1
0
mirror of synced 2025-12-18 05:47:58 +08:00

🎨 #3756 【企业微信】修复企业微信 API 回调验签过程中 WxCryptUtil.decrypt 方法可能抛出异常的问题

This commit is contained in:
helloJetBase-tech
2025-11-12 07:44:18 +02:00
committed by GitHub
parent b94106fd57
commit 28fac4ec2e

View File

@@ -333,14 +333,28 @@ public class WxCryptUtil {
byte[] bytes = PKCS7Encoder.decode(original);
// 分离16位随机字符串,网络字节序和AppId
if (bytes == null || bytes.length < 20) {
throw new WxRuntimeException("解密后数据长度异常可能为错误的密文或EncodingAESKey");
}
byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);
int xmlLength = bytesNetworkOrder2Number(networkOrder);
xmlContent = new String(Arrays.copyOfRange(bytes, 20, 20 + xmlLength), CHARSET);
fromAppid = new String(Arrays.copyOfRange(bytes, 20 + xmlLength, bytes.length), CHARSET);
// 长度边界校验,避免非法长度导致的越界/参数异常
int startIndex = 20;
int endIndex = startIndex + xmlLength;
if (xmlLength < 0 || endIndex > bytes.length) {
throw new WxRuntimeException("解密后数据格式非法消息长度不正确可能为错误的密文或EncodingAESKey");
}
xmlContent = new String(Arrays.copyOfRange(bytes, startIndex, endIndex), CHARSET);
fromAppid = new String(Arrays.copyOfRange(bytes, endIndex, bytes.length), CHARSET);
} catch (Exception e) {
throw new WxRuntimeException(e);
if (e instanceof WxRuntimeException) {
throw (WxRuntimeException) e;
} else {
throw new WxRuntimeException(e);
}
}
// appid不相同的情况 暂时忽略这段判断