Skip to content

Fix AES key charset encoding issue in WxPay decryptToString method - #3710

Merged
binarywang merged 2 commits into
developfrom
copilot/fix-3698
Sep 24, 2025
Merged

Fix AES key charset encoding issue in WxPay decryptToString method#3710
binarywang merged 2 commits into
developfrom
copilot/fix-3698

Conversation

CopilotAI commented Sep 22, 2025

Copy link
Copy Markdown
Contributor

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 dependentSecretKeySpeckey = newSecretKeySpec(apiV3Key.getBytes(), "AES");
GCMParameterSpecspec = newGCMParameterSpec(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 encodingSecretKeySpeckey = newSecretKeySpec(apiV3Key.getBytes(StandardCharsets.UTF_8), "AES");
GCMParameterSpecspec = newGCMParameterSpec(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>
CopilotAI changed the title [WIP] 微信支付报错:解析报文异常Fix AES key charset encoding issue in WxPay decryptToString methodSep 22, 2025
@binarywang
binarywang marked this pull request as ready for review September 24, 2025 13:42
@binarywang
binarywang merged commit c0edd9f into developSep 24, 2025
1 check passed
@binarywangbinarywang added this to the 4.7.8 milestone Sep 24, 2025
@binarywang
binarywang deleted the copilot/fix-3698 branch March 10, 2026 03:51
Sign up for freeto 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

@binarywang