This project demonstrates a simple Diffie-Hellman key exchange implementation in Go, with a client and server communicating over TCP. The shared secret generated can be used for symmetric encryption.
client/main.go– Connects to the server, sends its public key, and computes the shared key.server/main.go– Listens on a TCP port, receives the client's public key, sends its own, and computes the shared key.
- Both client and server generate a private key (random number modulo
p). - They generate a public key:
$publicKey = g^{privateKey} \mod p$ - The client sends its public key to the server.
- The server sends its public key back.
- Both compute the shared secret:
$sharedKey = (otherPartyPublicKey)^{privateKey} \mod p$ - Both sides now have the same shared key without transmitting it directly.
The code uses the RFC 3526 2048-bit MODP Group prime (p) and generator (g = 2) for key generation.
- Start the server:
go run ./server/main.go
- Start the client:
go run ./client/main.go