A Python-based educational tool that demonstrates how the Diffie–Hellman (DH) key exchange algorithm works under the hood. The program walks through generating a large (safe)prime, deriving private/public keys for two parties (Alice and Bob), and computing a shared secret. This is done through an interactive command-line menu.
- Miller–Rabin primality testing
- Random large prime generation, with an optional safe-prime mode (p = 2q + 1)
- Pollard's rho factorization (used to factor p - 1 when finding a generator)
- Primitive root / generator discovery
- Private and public key generation
- Shared secret computation, with verification that both parties arrive at the same value
- Persists generated parameters and keys to JSON so steps can be run independently across sessions
This project is for learning and demonstration purposes only. It is not intended for securing real communications. In particular:
- The default 512-bit prime is far too small for real-world security, with modern DH implementations using 2048+ bit primes.
- The generated
dhParams.jsonanddhKeys.jsonfiles contain private key material and are excluded from version control - There's no authentication of the exchanged public keys, so this implementation is vulnerable to man-in-the-middle attacks, as is unauthenticated DH in general.
Python 3.8+ recommended. No external packages — only the standard library (see requirements.txt).
git clone https://github.com/colesjunge/dhKeyExchangeSimulator.git
cd dhKeyExchangeSimulator
No further setup is needed.
Run the program from the project directory:
python dhMain.py
You'll see an explanation of how DH works, followed by a menu:
DH Key Exchange - MAIN MENU
1. Generate Prime Number (512-bit) and Generator
2. Generate Alice and Bob's Private Keys
3. Generate Alice and Bob's Public Keys
4. Generate Shared Secret Key
5. Display DH Information
6. Exit
Steps are meant to be run in order (1 through 4), since each one depends on data saved by the previous step.
.
├── dhMain.py # Entry point / interactive menu
├── dhAlgorithmFunctions.py # Core DH math
├── dhDisplayGenFunctions.py # I/O and console display logic
├── dhParams.json # Generated at runtime (p, g) - not tracked in git
├── dhKeys.json # Generated at runtime (keys) - not tracked in git
├── requirements.txt
└── README.md
This project is licensed under the MIT License — see the LICENSE file for details.
Cole Junge