testing
This commit is contained in:
14
Diffie-Hellman-Key-Exchange/README.md
Normal file
14
Diffie-Hellman-Key-Exchange/README.md
Normal 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.
|
||||
27
Diffie-Hellman-Key-Exchange/build/Makefile
Normal file
27
Diffie-Hellman-Key-Exchange/build/Makefile
Normal 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
|
||||
BIN
Diffie-Hellman-Key-Exchange/build/diffie_hellman
Executable file
BIN
Diffie-Hellman-Key-Exchange/build/diffie_hellman
Executable file
Binary file not shown.
7
Diffie-Hellman-Key-Exchange/include/diffie_hellman.h
Normal file
7
Diffie-Hellman-Key-Exchange/include/diffie_hellman.h
Normal 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
|
||||
BIN
Diffie-Hellman-Key-Exchange/obj/diffie_hellman.o
Normal file
BIN
Diffie-Hellman-Key-Exchange/obj/diffie_hellman.o
Normal file
Binary file not shown.
BIN
Diffie-Hellman-Key-Exchange/obj/main.o
Normal file
BIN
Diffie-Hellman-Key-Exchange/obj/main.o
Normal file
Binary file not shown.
21
Diffie-Hellman-Key-Exchange/src/diffie_hellman.c
Normal file
21
Diffie-Hellman-Key-Exchange/src/diffie_hellman.c
Normal 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;
|
||||
}
|
||||
42
Diffie-Hellman-Key-Exchange/src/main.c
Normal file
42
Diffie-Hellman-Key-Exchange/src/main.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user