#!/usr/bin/env python3 """ Debug script to analyze the encryption/decryption issue between agent and server. """ import os import sys from pathlib import Path # Add paths for imports sys.path.insert(0, str(Path(__file__).parent / "agent_v0")) sys.path.insert(0, str(Path(__file__).parent / "server")) def debug_encryption_issue(): """Debug the encryption/decryption mismatch.""" print("=== Debugging Encryption Issue ===") # Load environment env_local_path = Path(".env.local") if env_local_path.exists(): with open(env_local_path, 'r') as f: for line in f: line = line.strip() if line and not line.startswith('#') and '=' in line: key, value = line.split('=', 1) os.environ[key.strip()] = value.strip() password = os.getenv("ENCRYPTION_PASSWORD") print(f"Using password: {password[:16]}...") # Find the most recent .enc file agent_sessions_dir = Path("agent_v0/sessions") enc_files = list(agent_sessions_dir.glob("*.enc")) if not enc_files: print("❌ No .enc files found in agent_v0/sessions/") return False # Get the most recent .enc file latest_enc = max(enc_files, key=lambda p: p.stat().st_mtime) print(f"Testing with file: {latest_enc}") print(f"File size: {latest_enc.stat().st_size} bytes") # Test with agent's decryption function print("\n--- Testing Agent's Decryption ---") try: from storage_encrypted import decrypt_session_file as agent_decrypt agent_result = agent_decrypt(str(latest_enc), password) print(f"✅ Agent decryption successful: {agent_result}") # Check if it's a valid ZIP import zipfile with zipfile.ZipFile(agent_result, 'r') as zf: files = zf.namelist() print(f" ZIP contains {len(files)} files") except Exception as e: print(f"❌ Agent decryption failed: {e}") return False # Test with server's decryption function print("\n--- Testing Server's Decryption ---") try: # Import server's version import importlib.util spec = importlib.util.spec_from_file_location("server_storage", "server/storage_encrypted.py") server_storage = importlib.util.module_from_spec(spec) spec.loader.exec_module(server_storage) server_result = server_storage.decrypt_session_file(str(latest_enc), password) print(f"✅ Server decryption successful: {server_result}") # Check if it's a valid ZIP with zipfile.ZipFile(server_result, 'r') as zf: files = zf.namelist() print(f" ZIP contains {len(files)} files") except Exception as e: print(f"❌ Server decryption failed: {e}") print(f" Error type: {type(e).__name__}") print(f" Error details: {str(e)}") # Let's analyze the file structure print("\n--- Analyzing File Structure ---") try: with open(latest_enc, 'rb') as f: salt = f.read(16) iv = f.read(16) ciphertext = f.read() print(f"Salt length: {len(salt)} bytes") print(f"IV length: {len(iv)} bytes") print(f"Ciphertext length: {len(ciphertext)} bytes") print(f"Total file size: {16 + 16 + len(ciphertext)} bytes") if len(ciphertext) % 16 != 0: print(f"⚠️ Ciphertext length not multiple of 16: {len(ciphertext) % 16} remainder") else: print("✅ Ciphertext length is multiple of 16") except Exception as e2: print(f"❌ Error analyzing file: {e2}") return False print("\n✅ Both agent and server decryption work!") return True if __name__ == "__main__": success = debug_encryption_issue() sys.exit(0 if success else 1)