Last active
December 19, 2015 05:49
-
-
Save billowdood/5907193 to your computer and use it in GitHub Desktop.
Diffie-Hellman
This file contains 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
#!/usr/bin/python | |
from sys import argv | |
from math import sqrt | |
from random import randint | |
def potMod(b,p,m): | |
bits = p | |
res = 1 | |
temp = b | |
while bits > 0: | |
if bits%2==1: | |
res = (res*temp)%m | |
temp = (temp*temp)%m | |
bits = bits >> 1 | |
return res | |
def secret(mod,base): | |
exp = randint(1,mod) | |
return potMod(base,exp,mod),exp | |
def eveHack(A,B,prime,generator): | |
for it in range(prime): | |
if potMod(generator,it,prime) == A: | |
alicePriv = it | |
if potMod(generator,it,prime) == B: | |
bobPriv = it | |
return alicePriv,bobPriv | |
def main(): | |
prime = int(argv[1]) | |
generator = int(argv[2]) | |
A,secrNumA = secret(prime,generator) | |
B,secrNumB = secret(prime,generator) | |
print 'Alice generated: %d, using %d ^ %d mod %d'%(A,generator,secrNumA,prime) | |
print 'Bob generated: %d, using %d ^ %d mod %d'%(B,generator,secrNumB,prime) | |
print 'Alice\'s key: %d ^ %d mod %d = %d'%(B,secrNumA,prime,potMod(B,secrNumA,prime)) | |
print 'Bob\'s key: %d ^ %d mod %d = %d'%(A,secrNumB,prime,potMod(A,secrNumB,prime)) | |
aPr,bPr = eveHack(A,B,prime,generator) | |
print 'Eve\'s key using bob\'s private number: %d'%(potMod(A,bPr,prime)) | |
print 'Eve\'s key using alice\'s private number: %d'%(potMod(B,aPr,prime)) | |
main() |
This file contains 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
$ python difhel.py 997 258 | |
Alice generated: 531, using 258 ^ 91 mod 997 | |
Bob generated: 550, using 258 ^ 46 mod 997 | |
Alice's key: 550 ^ 91 mod 997 = 682 | |
Bob's key: 531 ^ 46 mod 997 = 682 | |
Eve's key using bob's private number: 682 | |
Eve's key using alice's private number: 682 |
This file contains 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
def eveHack(A,B,prime,generator): | |
for it in range(prime): | |
if potMod(generator,it,prime) == A: | |
alicePriv = it | |
if potMod(generator,it,prime) == B: | |
bobPriv = it | |
return alicePriv,bobPriv |
This file contains 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
$ python difhel.py primo generador |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment