This commit is contained in:
klein panic
2025-02-01 16:14:45 -05:00
commit 3aff0baacf
82 changed files with 1694 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
# Diffie-Hellman Key Exchange
## Description
This project implements a basic version of the Diffie-Hellman key exchange algorithm, allowing two parties (Alice and Bob) to agree on a shared secret over an insecure channel.
## How to Build
1. Navigate to the `build` directory.
2. Run `make` to compile the project.
## How to Run
1. After building, run the `diffie_hellman` executable using `./diffie_hellman`.
## How to Clean
- Run `make clean` to remove all compiled files.

View File

@@ -0,0 +1,27 @@
# Variables
CC = gcc
CFLAGS = -I../include -Wall -Wextra
OBJDIR = ../obj
SRCDIR = ../src
BINDIR = ../build
# Source files
SOURCES = $(SRCDIR)/main.c $(SRCDIR)/diffie_hellman.c
OBJECTS = $(OBJDIR)/main.o $(OBJDIR)/diffie_hellman.o
# Build target
TARGET = diffie_hellman
# Rules
all: $(BINDIR)/$(TARGET)
$(BINDIR)/$(TARGET): $(OBJECTS)
$(CC) $(OBJECTS) -o $(BINDIR)/$(TARGET)
$(OBJDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJDIR)/*.o $(BINDIR)/$(TARGET)
.PHONY: all clean

Binary file not shown.

View File

@@ -0,0 +1,7 @@
#ifndef DIFFIE_HELLMAN_H
#define DIFFIE_HELLMAN_H
unsigned long long generate_public_key(unsigned long long base, unsigned long long private_key, unsigned long long prime);
unsigned long long generate_shared_secret(unsigned long long received_public_key, unsigned long long private_key, unsigned long long prime);
#endif // DIFFIE_HELLMAN_H

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,21 @@
#include "diffie_hellman.h"
#include <stdio.h>
#include <math.h>
// Generate the public key: A = g^a mod p
unsigned long long generate_public_key(unsigned long long base, unsigned long long private_key, unsigned long long prime) {
unsigned long long result = 1;
for (unsigned long long i = 0; i < private_key; i++) {
result = (result * base) % prime;
}
return result;
}
// Generate the shared secret: s = B^a mod p
unsigned long long generate_shared_secret(unsigned long long received_public_key, unsigned long long private_key, unsigned long long prime) {
unsigned long long result = 1;
for (unsigned long long i = 0; i < private_key; i++) {
result = (result * received_public_key) % prime;
}
return result;
}

View File

@@ -0,0 +1,42 @@
#include "diffie_hellman.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
// Publicly agreed upon base (g) and prime (p)
unsigned long long base = 5;
unsigned long long prime = 23;
// Private keys chosen by Alice and Bob (should be random in practice)
unsigned long long alice_private_key, bob_private_key;
srand(time(NULL));
alice_private_key = rand() % (prime - 1) + 1; // Random private key for Alice
bob_private_key = rand() % (prime - 1) + 1; // Random private key for Bob
printf("Alice's private key: %llu\n", alice_private_key);
printf("Bob's private key: %llu\n", bob_private_key);
// Generate public keys
unsigned long long alice_public_key = generate_public_key(base, alice_private_key, prime);
unsigned long long bob_public_key = generate_public_key(base, bob_private_key, prime);
printf("Alice's public key: %llu\n", alice_public_key);
printf("Bob's public key: %llu\n", bob_public_key);
// Exchange public keys and generate shared secrets
unsigned long long alice_shared_secret = generate_shared_secret(bob_public_key, alice_private_key, prime);
unsigned long long bob_shared_secret = generate_shared_secret(alice_public_key, bob_private_key, prime);
printf("Alice's shared secret: %llu\n", alice_shared_secret);
printf("Bob's shared secret: %llu\n", bob_shared_secret);
// Verify if shared secrets match
if (alice_shared_secret == bob_shared_secret) {
printf("Key exchange successful! Shared secret: %llu\n", alice_shared_secret);
} else {
printf("Key exchange failed.\n");
}
return 0;
}