A full-stack web application that demonstrates the Diffie-Hellman Key Exchange protocol - the cryptographic method that allows two parties to establish a shared secret over an insecure channel.
- Overview
- How Diffie-Hellman Works
- Tech Stack
- Features
- API Documentation
- Getting Started
- Example Usage
- Project Structure
- Security Notes
The Diffie-Hellman Key Exchange is a fundamental protocol in modern cryptography. This simulator provides an interactive, visual demonstration of how two parties (conventionally called Alice and Bob) can agree on a shared secret key without ever directly transmitting it.
Key Concepts Demonstrated:
- Public parameter agreement (prime
pand generatorg) - Private key generation (secret values
aandb) - Public key computation and exchange
- Shared secret derivation by both parties
-
Public Parameters: Alice and Bob agree on two public numbers:
- A large prime number
p - A generator
g(where 2 ≤ g < p)
- A large prime number
-
Private Keys: Each party selects a secret number:
- Alice chooses private key
a - Bob chooses private key
b
- Alice chooses private key
-
Public Keys: Each party computes their public key:
- Alice computes:
A = g^a mod p - Bob computes:
B = g^b mod p
- Alice computes:
-
Exchange: Alice and Bob exchange their public keys
-
Shared Secret: Both parties compute the same shared secret:
- Alice computes:
s = B^a mod p - Bob computes:
s = A^b mod p
- Alice computes:
The magic lies in modular exponentiation properties:
s = B^a mod p
s = (g^b mod p)^a mod p
s = g^(ba) mod p
s = g^(ab) mod p
s = (g^a mod p)^b mod p
s = A^b mod p
Both parties arrive at the same shared secret without ever transmitting their private keys!
Alice Bob
| |
|-- p, g (public) ------------>|
|<-----------------------------|
| |
| a (private) | b (private)
| |
| A = g^a mod p | B = g^b mod p
| |
|-- A (public) --------------->|
|<---- B (public) -------------|
| |
| s = B^a mod p | s = A^b mod p
| = (g^b)^a mod p | = (g^a)^b mod p
| = g^(ab) mod p | = g^(ab) mod p
| |
+---------- SAME! -------------+
| Component | Technology |
|---|---|
| Frontend | React 18, Vite, React-Bootstrap |
| Backend | Node.js, Express.js |
| Cryptography | BigInt-based modular exponentiation |
| Architecture | RESTful API |
- Interactive Simulation: Step-by-step visualization of the key exchange process
- Real-time Validation: Input validation on both frontend and backend
- Visual Feedback: Color-coded sections for Alice (blue) and Bob (green)
- Educational Notes: Inline explanations and formula displays
- Responsive Design: Works on desktop and mobile devices
- Error Handling: Descriptive error messages for invalid inputs
http://localhost:5000/api/dh
GET /testReturns: "server working"
POST /generate-public
Content-Type: application/json
{
"p": 23, // Prime number
"g": 5, // Generator (must be < p)
"a": 6, // Alice's private key
"b": 15 // Bob's private key
}Response (200 OK):
{
"A": "8",
"B": "19",
"info": {
"pIsPrime": true,
"isSafePrime": true
}
}Error Response (400 Bad Request):
{
"error": "Generator (g) must be less than prime (p)"
}POST /generate-secret
Content-Type: application/json
{
"p": 23, // Prime number
"a": 6, // Private key
"B": "19" // Other party's public key
}Response (200 OK):
{
"sharedSecret": "2"
}- Node.js (v14 or higher)
- npm or yarn
-
Clone the repository
git clone <repository-url> cd diffie-hellman-simulator
-
Install dependencies
npm install
Development Mode (hot-reload frontend):
# Terminal 1: Start the backend
npm start
# Server runs on http://localhost:5000
# Terminal 2: Start the Vite dev server
npm run dev
# Frontend runs on http://localhost:5173
# API requests proxy to http://localhost:5000Production Build:
# Build the React frontend
npm run build
# Output: client/dist/
# Start backend
npm start
# Serve client/dist/ as static files (configure as needed)Backend:
- Entry point:
server/server.js - API Routes:
server/routes/dhRoutes.js - Controllers:
server/controller/dhController.js - Math Utilities:
server/utils/dhMath.js
Frontend (React + Vite):
- App root:
client/src/App.jsx(state & orchestration) - Entry:
client/src/main.jsx(Vite bootstrap) - Step components:
client/src/components/(ParameterStep, PrivateKeysStep, ExchangeStep, SecretStep) - Reusable components:
client/src/components/PartyPanel.jsx,OutputCard.jsx - Validation logic:
client/src/utils/validation.js - Constants:
client/src/constants.js - Styles:
client/src/app.css
Using the classic small example for demonstration:
| Parameter | Value | Description |
|---|---|---|
| p | 23 | Prime number |
| g | 5 | Generator |
| a | 6 | Alice's private key |
| b | 15 | Bob's private key |
Results:
- Alice's public key (A):
5^6 mod 23 = 8 - Bob's public key (B):
5^15 mod 23 = 19 - Shared secret:
19^6 mod 23 = 8^15 mod 23 = 2
# Generate public keys
curl -X POST http://localhost:5000/api/dh/generate-public \
-H "Content-Type: application/json" \
-d '{"p":23,"g":5,"a":6,"b":15}'
# Generate shared secret
curl -X POST http://localhost:5000/api/dh/generate-secret \
-H "Content-Type: application/json" \
-d '{"p":23,"a":6,"B":"19"}'- Create a POST request to
http://localhost:5000/api/dh/generate-public - Set body type to
rawandJSON - Enter parameters:
{"p":23,"g":5,"a":6,"b":15} - Send and verify A=8, B=19
- Create another POST request to
http://localhost:5000/api/dh/generate-secret - Enter:
{"p":23,"a":6,"B":"19"} - Send and verify sharedSecret=2
diffie-hellman-simulator/
├── client/
│ ├── index.html # Vite entry point
│ ├── src/
│ │ ├── main.jsx # React bootstrap
│ │ ├── App.jsx # Main app component & state
│ │ ├── app.css # Global styles
│ │ ├── constants.js # Shared data (primes, API URL)
│ │ ├── components/
│ │ │ ├── ParameterStep.jsx
│ │ │ ├── PrivateKeysStep.jsx
│ │ │ ├── ExchangeStep.jsx
│ │ │ ├── SecretStep.jsx
│ │ │ ├── PartyPanel.jsx
│ │ │ └── OutputCard.jsx
│ │ └── utils/
│ │ └── validation.js # Input validation & parsing
│ ├── assets/ # Static assets
│ └── vite.config.mjs # Vite config with API proxy
├── server/
│ ├── server.js # Express server entry point
│ ├── routes/
│ │ └── dhRoutes.js # API route definitions
│ ├── controller/
│ │ └── dhController.js # Request handlers with validation
│ └── utils/
│ └── dhMath.js # Modular exponentiation & prime checking
├── package.json # Project dependencies & scripts
├── README.md # This file
└── .gitignore # Git ignore rules
This is an educational simulator and should NOT be used for actual cryptographic purposes.
- No Prime Validation: While the simulator checks if p is prime, it doesn't enforce cryptographically strong primes
- Small Number Support: The simulator allows small primes for educational purposes (real DH requires 2048+ bit primes)
- No Side-Channel Protection: Timing attacks are not mitigated
- No Entropy Source: Private keys are user-provided, not cryptographically random
- Educational Focus: Code prioritizes clarity over security
Use established cryptographic libraries:
- Node.js:
cryptomodule (built-in) ortweetnacl - Python:
cryptographylibrary - Java:
javax.crypto(built-in)
ISC
Built with ❤️ for educational purposes