import socket
from rsa_operations import *
from sys import argv
import time
filename = "passwords"
port = argv[1]
def search_password(host, pswd):
pswd = str(pswd)
f = open(filename, "r")
for line in f.readlines():
a, b = line.split(" ")
a, b = a.replace("\\n", ""), b.replace("\\n", "")
if a == host and b == pswd:
return True
return False
rsa_math = RSAMath()
p, q = rsa_math.random_primes(MAX)
n = p*q
phi_n = (p-1)*(q-1)
e = rsa_math.random_e(phi_n)
d = rsa_math.modinv(e, phi_n)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", int(port)))
s.listen(3)
sc, sddr = s.accept()
rec = sc.recv(1024)
if "connected" in rec:
host, tmp = rec.split(" ")
sc.send("e")
time.sleep(0.2)
sc.send(str(e))
time.sleep(0.2)
sc.send("n")
time.sleep(0.2)
sc.send(str(n))
m = 0
print "RSA Server"
print "%s: Connected."%host
while True:
rec = sc.recv(1024)
if rec == "c":
c = int(sc.recv(1024))
print "Recieved c: %s"%c
m = rsa_math.exp(c, d)%n
print "Decrypted m: %s"%m
if m!= 0 and search_password(host, m):
print "Host %s can be trusted"%host
else:
print "Host %s cannot be trusted. Terminating..."%host
time.sleep(1)
break
sc.close()
s.close()