Created
July 4, 2017 04:52
-
-
Save itsecurityco/720dac7bd51876add743fa6ad4e79d49 to your computer and use it in GitHub Desktop.
Diffie-Hellman key exchange
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/python | |
| # @itseco | |
| # https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange | |
| import sys | |
| alice_private_key = int(sys.argv[1]) # should be large enough to be secure | |
| bob_private_key = int(sys.argv[2]) # should be large enough to be secure | |
| modulus_prime = 17 # should be large enough to be secure (2048 bits minimum) | |
| base_generator = 3 | |
| def public_key(base_generator, modulus_prime, priv, name): | |
| public = (base_generator ** priv) % modulus_prime | |
| print "%s public key = %d**%d %% %d = %d" % (name, base_generator, priv, modulus_prime, public) | |
| return public | |
| def shared_key(pub, priv, name): | |
| shared_key = (pub ** priv) % modulus_prime | |
| print "%s shared key = %d ** %d %% %d = %d" % (name, pub, priv, modulus_prime, shared_key) | |
| return shared_key | |
| def separator(value="mid"): | |
| repeat = 35; char = "-"; new_line = "\n"; | |
| position = { | |
| "top": char * repeat + new_line, | |
| "mid": char * repeat, | |
| "bot": new_line + char * repeat, | |
| } | |
| print position[value] | |
| print "Modulus (prime): %d" % modulus_prime | |
| print "Base (primitive root of Modulus): %d" % base_generator | |
| print "Alice private key: %d" % alice_private_key | |
| print "Bob private key: %d" % bob_private_key | |
| separator("bot") | |
| alice_public_key = public_key(base_generator, modulus_prime, alice_private_key, "alice") | |
| bob_public_key = public_key(base_generator, modulus_prime, bob_private_key, "bob") | |
| separator("mid") | |
| alice_shared_key = shared_key(bob_public_key, alice_private_key, "alice") | |
| bob_shared_key = shared_key(alice_public_key, bob_private_key, "bob") | |
| separator("top") | |
| print "Alice shared key: %d" % alice_shared_key | |
| print "Bob shared key: %d" % bob_shared_key | |
| """ | |
| root@kali:~# python diffiehellman.py 999 100 | |
| Modulus (prime): 17 | |
| Base (primitive root of Modulus): 3 | |
| Alice private key: 999 | |
| Bob private key: 100 | |
| ----------------------------------- | |
| alice public key = 3**999 % 17 = 11 | |
| bob public key = 3**100 % 17 = 13 | |
| ----------------------------------- | |
| alice shared key = 13 ** 999 % 17 = 4 | |
| bob shared key = 11 ** 100 % 17 = 4 | |
| ----------------------------------- | |
| Alice shared key: 4 | |
| Bob shared key: 4 | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment