import socket
from rsa_operations import *
import math
import time
from sys import argv
def search_private_key(hostname):
f = open("%s.dat"%hostname, "r")
for line in f.readlines():
n, d = line.split(" ")
return int(d), int(n)
hostname = argv[1]
port = int(argv[2])
rsa_math = RSAMath()
d, n = search_private_key(hostname)
s = socket.socket()
#print "d = %s"%d
#print "n = %s"%n
s.connect(("127.0.0.1", port))
s.send("%s connected"%hostname)
time.sleep(.2)
while True:
rec = s.recv(1024)
if rec == "r":
r = s.recv(1024)
#print "Recieved r: %s"%r
y = rsa_math.function(int(r))
#print "Normal y: %s"%y
m = rsa_math.exp(y, d)%n
#print "Encrypted y: %s"%m
s.send("c")
time.sleep(.2)
s.send(str(m))
if rec == "ok":
print "Correct Authentication"
print "Welcome!. You are now in the chat"
while(True):
msg = raw_input()
print "%s says: %s"%(hostname, msg)
s.send("%s says: %s"%(hostname, msg))
s.close()