Skip to content

Commit 1a2b44f

Browse files
committed
Diffie-Hellman algo
1 parent e18557a commit 1a2b44f

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

Diffie-Hellman/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "Diffie-Hellman"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
rand = "0.8"

Diffie-Hellman/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Diffie-Hellman Key Exchange - Rust
2+
3+
This is a simple implementation of the **Diffie-Hellman key exchange protocol**
4+
in Rust. The program demonstrates how two parties (Alice and Bob) can securely
5+
share a secret key over an insecure channel using public-key cryptography.
6+
7+
## Features
8+
- **Prime Number (`p`) and Base (`g`) input**: Users can input the prime number and base for the Diffie-Hellman protocol.
9+
- **Private Key input**: Users can input the private keys for Alice and Bob.
10+
- **Key Exchange**: The program computes the public keys for Alice and Bob, and then calculates the shared secret key for both.
11+
- **Security**: The Diffie-Hellman protocol allows both parties to compute the same shared secret without directly transmitting it.
12+
13+
## Prerequisites
14+
```toml
15+
[dependencies]
16+
rand = "0.8"
17+
```
18+
19+
## Usage
20+
21+
when you run the program, you will be prompted to enter the following inputs:
22+
23+
1. **Prime number (`p`)**: A prime number that is publicly agreed upon by both parties.
24+
2. **Base (`g`)**: A base number used for modular exponentiation.
25+
3. **Alice's private key**: A secret number chosen by Alice.
26+
4. **Bob's private key**: A secret number chosen by Bob.
27+
28+
### Example Interaction
29+
30+
```
31+
Enter a prime number (p): 23
32+
Enter the base (g): 5
33+
Enter Alice's private key: 6
34+
Enter Bob's private key: 15
35+
36+
Prime number (p): 23
37+
Base (g): 5
38+
Alice's private key: 6
39+
Bob's private key: 15
40+
Alice's public key: 8
41+
Bob's public key: 19
42+
43+
Exchanging public keys...
44+
45+
Alice's computed shared secret: 2
46+
Bob's computed shared secret: 2
47+
48+
The shared secret is the same! Key exchange successful.
49+
```
50+
51+
## Code Explanation
52+
53+
- **Modular Exponentiation**: The core of the Diffie-Hellman key exchange uses modular exponentiation to compute public and shared keys. This is implemented in the `mod_exp` function.
54+
- **Private Key Generation**: Private keys are randomly generated for Alice and Bob within the range `[1, p)`.
55+
- **Public Key Calculation**: The public keys `A` and `B` are calculated using the formula:
56+
57+
```
58+
A = g^a mod p
59+
B = g^b mod p
60+
```
61+
62+
- **Shared Secret Calculation**: Alice and Bob each calculate the shared secret using the other’s public key and their own private key:
63+
64+
```
65+
Shared Secret = B^a mod p (for Alice)
66+
Shared Secret = A^b mod p (for Bob)
67+
```

Diffie-Hellman/diffie.png

102 KB
Loading

Diffie-Hellman/src/main.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
extern crate rand;
2+
use std::io::{self, Write};
3+
4+
fn mod_exp(base: u64, exp: u64, modulus: u64) -> u64 {
5+
let mut result = 1;
6+
let mut base = base % modulus;
7+
let mut exp = exp;
8+
9+
while exp > 0 {
10+
if exp % 2 == 1 {
11+
result = (result * base) % modulus;
12+
}
13+
exp = exp >> 1;
14+
base = (base * base) % modulus;
15+
}
16+
result
17+
}
18+
19+
fn read_input(prompt: &str) -> u64 {
20+
print!("{}", prompt);
21+
io::stdout().flush().unwrap(); // Ensure prompt is printed before reading
22+
let mut input = String::new();
23+
io::stdin().read_line(&mut input).unwrap();
24+
input.trim().parse().unwrap()
25+
}
26+
27+
fn main() {
28+
// Get the prime number (p) and base (g) from user input
29+
let p = read_input("Enter a prime number (p): ");
30+
let g = read_input("Enter the base (g): ");
31+
32+
// Get private keys for Alice and Bob
33+
let a = read_input("Enter Alice's private key: ");
34+
let b = read_input("Enter Bob's private key: ");
35+
36+
// Compute the public keys
37+
let avar = mod_exp(g, a, p); // Alice's public key
38+
let bvar = mod_exp(g, b, p); // Bob's public key
39+
40+
println!("\nPrime number (p): {}", p);
41+
println!("Base (g): {}", g);
42+
println!("Alice's private key: {}", a);
43+
println!("Bob's private key: {}", b);
44+
println!("Alice's public key: {}", avar);
45+
println!("Bob's public key: {}", bvar);
46+
47+
// Exchange public keys (this is done securely in a real-world scenario)
48+
println!("\nExchanging public keys...");
49+
50+
// Alice computes the shared secret using Bob's public key
51+
let shared_secret_alice = mod_exp(bvar, a, p);
52+
// Bob computes the shared secret using Alice's public key
53+
let shared_secret_bob = mod_exp(avar, b, p);
54+
55+
println!("\nAlice's computed shared secret: {}", shared_secret_alice);
56+
println!("Bob's computed shared secret: {}", shared_secret_bob);
57+
58+
// Verify that the shared secrets are the same
59+
if shared_secret_alice == shared_secret_bob {
60+
println!("\nThe shared secret is the same! Key exchange successful.");
61+
} else {
62+
println!("\nThe shared secrets do not match. Something went wrong.");
63+
}
64+
}

0 commit comments

Comments
 (0)