From 4e4248feb59e7a39a6e3461cf00f30d8f3de5563 Mon Sep 17 00:00:00 2001 From: aaron <> Date: Sun, 26 Jan 2025 00:40:12 +0800 Subject: [PATCH] update --- app/core/wechat.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/app/core/wechat.py b/app/core/wechat.py index 6151448..225a5f4 100644 --- a/app/core/wechat.py +++ b/app/core/wechat.py @@ -271,6 +271,12 @@ class WeChatClient: dict: 解密后的数据 """ try: + # API v3密钥转换为32字节 + key_bytes = self.api_v3_key.encode('utf-8') + if len(key_bytes) != 32: + print(f"API v3密钥长度不正确: {len(key_bytes)} bytes") + raise ValueError("API v3密钥必须是32字节") + # Base64解码密文和随机串 ciphertext_bytes = base64.b64decode(ciphertext) nonce_bytes = base64.b64decode(nonce) @@ -278,10 +284,14 @@ class WeChatClient: # 处理附加数据 associated_data_bytes = associated_data.encode('utf-8') if associated_data else b'' - # 使用 API v3 密钥进行解密 - key_bytes = self.api_v3_key.encode('utf-8') aesgcm = AESGCM(key_bytes) + print("解密参数:") + print(f"密钥长度: {len(key_bytes)} bytes") + print(f"Nonce长度: {len(nonce_bytes)} bytes") + print(f"密文长度: {len(ciphertext_bytes)} bytes") + print(f"附加数据: {associated_data_bytes}") + # 解密数据 decrypted_data = aesgcm.decrypt( nonce_bytes, @@ -289,12 +299,16 @@ class WeChatClient: associated_data_bytes ) + print(f"解密后的原始数据: {decrypted_data}") # 解析JSON数据 return json.loads(decrypted_data) except Exception as e: - print(f"解密回调数据失败: {str(e)}") - raise Exception(f"解密回调数据失败: {str(e)}") + error_msg = f"解密回调数据失败: {str(e)}" + print(error_msg) + import traceback + print("详细错误信息:", traceback.format_exc()) + raise Exception(error_msg) async def verify_payment_notify(self, request: Request) -> dict: """验证支付回调通知"""