From 944546beb13bc856781a76000668d51c4b735a6c Mon Sep 17 00:00:00 2001 From: ylytdeng Date: Fri, 20 Mar 2026 14:32:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BB=9F=E4=B8=80=E6=89=80=E6=9C=89=20J?= =?UTF-8?q?SON=20=E6=96=87=E4=BB=B6=E8=AF=BB=E5=86=99=E4=B8=BA=20UTF-8=20?= =?UTF-8?q?=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Windows 中文环境默认编码为 GBK,未指定 encoding 会导致 config.json/all_keys.json 解析失败。修复 9 个文件共 17 处。 Closes #32 Co-Authored-By: Claude Opus 4.6 (1M context) --- config.py | 6 +++--- decrypt_db.py | 2 +- find_image_key.py | 4 ++-- find_image_key_monitor.py | 4 ++-- latency_test.py | 2 +- main.py | 4 ++-- mcp_server.py | 8 ++++---- monitor.py | 2 +- monitor_web.py | 2 +- 9 files changed, 17 insertions(+), 17 deletions(-) diff --git a/config.py b/config.py index 97efc03..295687d 100644 --- a/config.py +++ b/config.py @@ -169,7 +169,7 @@ def load_config(): cfg = {} if os.path.exists(CONFIG_FILE): try: - with open(CONFIG_FILE) as f: + with open(CONFIG_FILE, encoding="utf-8") as f: cfg = json.load(f) except json.JSONDecodeError: print(f"[!] {CONFIG_FILE} 格式损坏,将使用默认配置") @@ -181,12 +181,12 @@ def load_config(): if detected: print(f"[+] 自动检测到微信数据目录: {detected}") cfg = {**_DEFAULT, **cfg, "db_dir": detected} - with open(CONFIG_FILE, "w") as f: + with open(CONFIG_FILE, "w", encoding="utf-8") as f: json.dump(cfg, f, indent=4, ensure_ascii=False) print(f"[+] 已保存到: {CONFIG_FILE}") else: if not os.path.exists(CONFIG_FILE): - with open(CONFIG_FILE, "w") as f: + with open(CONFIG_FILE, "w", encoding="utf-8") as f: json.dump(_DEFAULT, f, indent=4, ensure_ascii=False) print(f"[!] 未能自动检测微信数据目录") print(f" 请手动编辑 {CONFIG_FILE} 中的 db_dir 字段") diff --git a/decrypt_db.py b/decrypt_db.py index bd8fa9f..ec1aad8 100644 --- a/decrypt_db.py +++ b/decrypt_db.py @@ -116,7 +116,7 @@ def main(): print("请先运行 find_all_keys.py") sys.exit(1) - with open(KEYS_FILE) as f: + with open(KEYS_FILE, encoding="utf-8") as f: keys = json.load(f) keys = strip_key_metadata(keys) diff --git a/find_image_key.py b/find_image_key.py index 6800696..afb9ad1 100644 --- a/find_image_key.py +++ b/find_image_key.py @@ -334,7 +334,7 @@ def verify_and_decrypt(attach_dir, aes_key_str, xor_key): def main(): config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.json') - with open(config_path) as f: + with open(config_path, encoding="utf-8") as f: config = json.load(f) db_dir = config['db_dir'] @@ -392,7 +392,7 @@ def main(): config['image_aes_key'] = aes_key if xor_key is not None: config['image_xor_key'] = xor_key - with open(config_path, 'w') as f: + with open(config_path, 'w', encoding="utf-8") as f: json.dump(config, f, indent=2, ensure_ascii=False) print(f"Saved to {config_path}", flush=True) diff --git a/find_image_key_monitor.py b/find_image_key_monitor.py index f81442b..437a47f 100644 --- a/find_image_key_monitor.py +++ b/find_image_key_monitor.py @@ -227,7 +227,7 @@ def verify_and_decrypt(attach_dir, aes_key_str, xor_key): def main(): config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.json') - with open(config_path) as f: + with open(config_path, encoding="utf-8") as f: config = json.load(f) db_dir = config['db_dir'] @@ -292,7 +292,7 @@ def main(): config['image_aes_key'] = aes_key if xor_key is not None: config['image_xor_key'] = xor_key - with open(config_path, 'w') as f: + with open(config_path, 'w', encoding="utf-8") as f: json.dump(config, f, indent=2, ensure_ascii=False) print(f"Saved to {config_path}", flush=True) diff --git a/latency_test.py b/latency_test.py index e5f2fae..62a670e 100644 --- a/latency_test.py +++ b/latency_test.py @@ -15,7 +15,7 @@ DB_DIR = _cfg["db_dir"] KEYS_FILE = _cfg["keys_file"] DECRYPTED = os.path.join(_cfg["decrypted_dir"], "session", "session.db") -with open(KEYS_FILE) as f: +with open(KEYS_FILE, encoding="utf-8") as f: keys = json.load(f) enc_key = bytes.fromhex(keys["session/session.db"]["enc_key"]) diff --git a/main.py b/main.py index 90b16ee..885f975 100644 --- a/main.py +++ b/main.py @@ -28,7 +28,7 @@ def ensure_keys(keys_file, db_dir): """确保密钥文件存在且匹配当前 db_dir,否则重新提取""" if os.path.exists(keys_file): try: - with open(keys_file) as f: + with open(keys_file, encoding="utf-8") as f: keys = json.load(f) except (json.JSONDecodeError, ValueError): keys = {} @@ -59,7 +59,7 @@ def ensure_keys(keys_file, db_dir): print("[!] 密钥提取失败") sys.exit(1) try: - with open(keys_file) as f: + with open(keys_file, encoding="utf-8") as f: keys = json.load(f) except (json.JSONDecodeError, ValueError): keys = {} diff --git a/mcp_server.py b/mcp_server.py index 8ef1f05..455cf8f 100644 --- a/mcp_server.py +++ b/mcp_server.py @@ -29,7 +29,7 @@ WAL_FRAME_HEADER_SZ = 24 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) CONFIG_FILE = os.path.join(SCRIPT_DIR, "config.json") -with open(CONFIG_FILE) as f: +with open(CONFIG_FILE, encoding="utf-8") as f: _cfg = json.load(f) for _key in ("keys_file", "decrypted_dir"): if _key in _cfg and not os.path.isabs(_cfg[_key]): @@ -52,7 +52,7 @@ if not DECODED_IMAGE_DIR: elif not os.path.isabs(DECODED_IMAGE_DIR): DECODED_IMAGE_DIR = os.path.join(SCRIPT_DIR, DECODED_IMAGE_DIR) -with open(KEYS_FILE) as f: +with open(KEYS_FILE, encoding="utf-8") as f: ALL_KEYS = strip_key_metadata(json.load(f)) # ============ 解密函数 ============ @@ -143,7 +143,7 @@ class DBCache: if not os.path.exists(self.MTIME_FILE): return try: - with open(self.MTIME_FILE) as f: + with open(self.MTIME_FILE, encoding="utf-8") as f: saved = json.load(f) except (json.JSONDecodeError, OSError): return @@ -172,7 +172,7 @@ class DBCache: for rel_key, (db_mt, wal_mt, path) in self._cache.items(): data[rel_key] = {"db_mt": db_mt, "wal_mt": wal_mt, "path": path} try: - with open(self.MTIME_FILE, 'w') as f: + with open(self.MTIME_FILE, 'w', encoding="utf-8") as f: json.dump(data, f) except OSError: pass diff --git a/monitor.py b/monitor.py index 0169b2d..7408684 100644 --- a/monitor.py +++ b/monitor.py @@ -149,7 +149,7 @@ def main(): print("=" * 60) # 加载密钥 - with open(KEYS_FILE) as f: + with open(KEYS_FILE, encoding="utf-8") as f: keys = strip_key_metadata(json.load(f)) session_key_info = get_key_info(keys, os.path.join("session", "session.db")) diff --git a/monitor_web.py b/monitor_web.py index e61ac86..db139a1 100644 --- a/monitor_web.py +++ b/monitor_web.py @@ -1883,7 +1883,7 @@ def main(): print(" 微信实时监听 (WAL增量 + SSE推送)", flush=True) print("=" * 60, flush=True) - with open(KEYS_FILE) as f: + with open(KEYS_FILE, encoding="utf-8") as f: keys = strip_key_metadata(json.load(f)) session_key_info = get_key_info(keys, os.path.join("session", "session.db"))