Professional Documents
Culture Documents
p = 4
# keep generating while the primality test fail
while not is_prime(p, 128):
p = generate_prime_candidate(length)
return p
g = generate_prime_number()
p = generate_prime_number()
class A:
def __init__(self):
self.n = random.randint(1, p)
def publish(self):
return (g**self.n)%p
HOST = "127.0.0.1"
PORT = 65432
print("p: ", p)
print("g: ", g)
s.sendall(str(g).encode("utf-8") +
str(p).encode("utf-8"))
#s.sendall()
alice = A()
bob = A()
s.sendall(str(ga).encode("utf-8"))
s.sendall(str(gb).encode("utf-8"))
while gea=="b''":
gea = str(s.recv(1024))
geb = str(s.recv(1024))
gea = int(gea[2:-1])
geb = int(geb[2:-1])
sa = alice.compute_secret(gea)
sb = bob.compute_secret(geb)
HOST = "127.0.0.1"
PORT = 65432
class B:
def __init__(self):
self.a = random.randint(1, p)
self.b = random.randint(1, p)
self.arr = [self.a,self.b]
inp = str(conn.recv(1024))
g = int(inp[2:6])
p = int(inp[6:-1])
print("p =", p)
print("g =", g)
ga = str(conn.recv(1024))
gb = str(conn.recv(1024))
darth = B()
gea = darth.publish(0)
geb = darth.publish(1)
conn.sendall(str(gea).encode("utf-8"))
conn.sendall(str(geb).encode("utf-8"))
ga = int(ga[2:-1])
gb = int(gb[2:-1])
seb = darth.compute_secret(gb,1)
Output:
Sender:
Receiver: