Skip to content

Instantly share code, notes, and snippets.

@callingmedic911
Created May 6, 2023 07:41
Show Gist options
  • Save callingmedic911/60c0949930edf420fa032fdb7b6c4e23 to your computer and use it in GitHub Desktop.
Save callingmedic911/60c0949930edf420fa032fdb7b6c4e23 to your computer and use it in GitHub Desktop.
# Prime number and generator for Diffie-Hellman
p = 23
g = 5
# Alice and Bob generate their own secret keys
a = 6
b = 15
# Alice and Bob calculate their public keys
A = (g ** a) % p
B = (g ** b) % p
# Alice and Bob exchange their public keys
# (this could be done over an insecure channel)
# In this example, we'll just assign the values manually
A_received = B
B_received = A
# Alice and Bob calculate the shared secret key
alice_shared_secret = (B_received ** a) % p
bob_shared_secret = (A_received ** b) % p
# Both shared secrets should be the same
print("Shared secret calculated by Alice: ", alice_shared_secret)
print("Shared secret calculated by Bob: ", bob_shared_secret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment