Skip to content

Latest commit

 

History

History
21 lines (17 loc) · 1.9 KB

File metadata and controls

21 lines (17 loc) · 1.9 KB

Secure Chat Client (script.py)

Overview

This script implements a secure chat client that enables two users to communicate over a network using AES-256 encryption. It is designed to encrypt and decrypt messages in real time and provides a mechanism for deterministic key generation.

How It Works

  • The script loads an OpenAI API key from a .env file. If the key is available, the script initializes an OpenAI client.
  • It uses the OpenAI language model (LLM) to deterministically generate a 32-byte encryption key (expressed as 64 hexadecimal characters) based on the username and a shared context. This key is then used for AES encryption/decryption.
  • If the LLM is not available (e.g., no API key or errors during key generation), the script falls back to using SHA-256 for key generation.
  • The client can either start a server to wait for a connection or connect to an existing chat server. Messages are always encrypted using a constant conversation key derived from a shared context, ensuring both parties can decrypt each other's messages.
  • The decryption process attempts multiple methods (using the conversation key, peer key, user key, and a SHA-256 fallback) to maximize the success of message decryption. After multiple failures, it automatically switches to SHA-256.

Role of the LLM

  • Key Generation: The LLM is queried to generate a deterministic 32-byte encryption key. A specific system prompt ensures that the output is exactly 64 hexadecimal characters. This allows for a unique yet consistent key per user based on their username.
  • Fallback Mechanism: If LLM key generation fails or an API key is not provided, the application falls back to using a standard SHA-256 hash mechanism.

Usage

  1. Run the script.
  2. Choose whether to start a new chat (server) or join an existing one.
  3. Enter your username and optionally force SHA-256 for key generation.
  4. Type messages to send encrypted communications.