Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 22, 2025

The WeChat Pay V3 API notification parsing was failing with "Invalid AES key length: 33 bytes" error due to inconsistent charset encoding when converting strings to byte arrays.

Problem

The AesUtils.decryptToString() method was using String.getBytes() without specifying the charset, which caused platform-dependent behavior:

// Problematic code - charset dependent
SecretKeySpec key = new SecretKeySpec(apiV3Key.getBytes(), "AES");
GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce.getBytes());
cipher.updateAAD(associatedData.getBytes());

When the platform default charset differed from UTF-8, or when the API key contained non-ASCII characters, the byte array length could exceed the required 32 bytes for AES-256 encryption, causing the InvalidKeyException: Invalid AES key length: 33 bytes error.

Solution

Updated all getBytes() calls in the decryptToString() method to explicitly use UTF-8 encoding:

// Fixed code - consistent UTF-8 encoding
SecretKeySpec key = new SecretKeySpec(apiV3Key.getBytes(StandardCharsets.UTF_8), "AES");
GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce.getBytes(StandardCharsets.UTF_8));
cipher.updateAAD(associatedData.getBytes(StandardCharsets.UTF_8));

This ensures consistent behavior across all platforms and environments, matching the pattern already used in the HMACSHA256() method within the same class.

Impact

  • Fixes the "解析报文异常" (parse message exception) error in WeChat Pay V3 notification handling
  • Ensures predictable behavior regardless of platform default charset
  • Maintains backward compatibility with existing valid configurations
  • Follows consistent coding patterns within the codebase

Fixes #3698.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] 微信支付报错:解析报文异常 Fix AES key charset encoding issue in WxPay decryptToString method Sep 22, 2025
@Copilot Copilot AI requested a review from binarywang September 22, 2025 16:38
Copilot finished work on behalf of binarywang September 22, 2025 16:38
@binarywang binarywang marked this pull request as ready for review September 24, 2025 13:42
@binarywang binarywang merged commit c0edd9f into develop Sep 24, 2025
1 check passed
@binarywang binarywang added this to the 4.7.8 milestone Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

微信支付报错:解析报文异常

2 participants