@@ -333,14 +333,28 @@ public String decrypt(String cipherText) {
333333 byte [] bytes = PKCS7Encoder .decode (original );
334334
335335 // 分离16位随机字符串,网络字节序和AppId
336+ if (bytes == null || bytes .length < 20 ) {
337+ throw new WxRuntimeException ("解密后数据长度异常,可能为错误的密文或EncodingAESKey" );
338+ }
336339 byte [] networkOrder = Arrays .copyOfRange (bytes , 16 , 20 );
337340
338341 int xmlLength = bytesNetworkOrder2Number (networkOrder );
339342
340- xmlContent = new String (Arrays .copyOfRange (bytes , 20 , 20 + xmlLength ), CHARSET );
341- fromAppid = new String (Arrays .copyOfRange (bytes , 20 + xmlLength , bytes .length ), CHARSET );
343+ // 长度边界校验,避免非法长度导致的越界/参数异常
344+ int startIndex = 20 ;
345+ int endIndex = startIndex + xmlLength ;
346+ if (xmlLength < 0 || endIndex > bytes .length ) {
347+ throw new WxRuntimeException ("解密后数据格式非法:消息长度不正确,可能为错误的密文或EncodingAESKey" );
348+ }
349+
350+ xmlContent = new String (Arrays .copyOfRange (bytes , startIndex , endIndex ), CHARSET );
351+ fromAppid = new String (Arrays .copyOfRange (bytes , endIndex , bytes .length ), CHARSET );
342352 } catch (Exception e ) {
343- throw new WxRuntimeException (e );
353+ if (e instanceof WxRuntimeException ) {
354+ throw (WxRuntimeException ) e ;
355+ } else {
356+ throw new WxRuntimeException (e );
357+ }
344358 }
345359
346360 // appid不相同的情况 暂时忽略这段判断
0 commit comments