document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import socket
  2. from rsa_operations import *
  3. from random import randint
  4. from sys import argv
  5. import time
  6.  
  7. port = int(argv[1])
  8.  
  9. def search_public_key(host):
  10.   f = open("users.dat", "r")
  11.   for line in f.readlines():
  12.     name, e, n = line.split(" ")
  13.     if host == name:
  14.       return int(e), int(n)
  15.   return -1, -1
  16.  
  17.  
  18. rsa_math = RSAMath()
  19. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  20. s.bind(("", port))
  21. s.listen(3)
  22. sc, sddr = s.accept()
  23. m = randint(5, 20)
  24. e, n = 0, 0
  25. rec = sc.recv(1024)
  26. #print rec
  27. if "connected" in rec:
  28.   hostname, tmp = rec.split(" ")
  29.   r = randint(1, 100)
  30.   my_y = rsa_math.function(r)
  31.   sc.send("r")
  32.   time.sleep(0.2)
  33.   sc.send(str(r))
  34.  
  35. #print "Random %s"%r
  36.  
  37. print "RSA Server"
  38. while True:
  39.   rec = sc.recv(1024)
  40.   #print rec
  41.   if "says:" in rec:
  42.     print rec
  43.   if rec == "c":
  44.     c = int(sc.recv(1024))
  45.     e, n = search_public_key(hostname)
  46.     if e == -1 and n == -1:
  47.       print "Public Key not found.Finishing..."
  48.       sc.close()
  49.       s.close()
  50.     else:
  51.       client_y = rsa_math.exp(c, e) % n
  52.       #print client_y, my_y
  53.       if client_y == my_y:
  54.  print "Welcome to the RSA Server %s"%hostname
  55.         sc.send("ok")
  56.       else:
  57.  print "Authentication Error. Finishing..."
  58.         sc.close()
  59.         s.close()
  60. sc.close()
  61. s.close()
');