document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/python
  2.  
  3. import socket
  4. import random
  5. from rsa_math import *
  6.  
  7. def msg_num(message_e):
  8.     np_e = 0
  9.     for char in message_s:
  10.         np_s <<= 8
  11.         np_s += ord(char)
  12.     return np_s
  13.  
  14. def num_msg(np_r):
  15.     nm = []
  16.     while np_r > 0:
  17.         nm.insert(0, chr(np_r & 255))
  18.         np_r >>= 8
  19.     message_r = "".join(nm)
  20.     return message_r
  21.  
  22. def encrypt(np, e_r, n_r):
  23.     ne_e = pow(np, e_r) % n_r
  24.     return ne_e
  25.  
  26. def decrypt(ne_r, d, n):
  27.     np_r = pow(ne_r, d) % n
  28.     return np_r
  29.  
  30. def extended_gcd(a, b):
  31.     x, ant_x = 0, 1
  32.     y, ant_y = 1, 0
  33.     coc=0
  34.     while b:
  35.         coc = a // b
  36.         a, b = b, a % b
  37.         x, ant_x = ant_x - coc*x, x
  38.         y, ant_y = ant_y - coc*y, y
  39.  
  40.     return (ant_x, ant_y, a)
  41.  
  42. def send(holaa, client):
  43.     client.send(str(holaa))
  44.     return None
  45.  
  46. def received(client):
  47.     algo = client.recv(1024)  
  48.     return int(hola)
  49.  
  50. def main():
  51.     client = socket.socket()  
  52.     client.connect(("localhost",1122))  
  53.     print "Connected"
  54.    
  55.     p = 13
  56.     q = 11
  57.     r = p*q
  58.     phi = (p - 1)*(q - 1)
  59.     e = 23
  60.     gcd = 0
  61.    
  62.     while (gcd != 1):  
  63.         gcd = extended_gcd(e, phi)  
  64.         d = mod_inver(e, phi)  
  65.    
  66.     n_r = received(client)
  67.     e_r = received(client)
  68.     print "Key received"
  69.     print n_r, e_r
  70.  
  71.     send(n, client)
  72.     send(e, client)
  73.     print "Sent key"
  74.     print n, e
  75.  
  76.     mensaje_r = " "
  77.     print "Starting"
  78.     while  (message_r != "End") :
  79.  
  80.         ne_r = received(client)
  81.         print ne_r
  82.         np_r = modex(ne_r, d, n)
  83.         print np_r
  84.         message_r = num_msg(np_r)
  85.         print "Received:  ", message_r
  86.  
  87.         message_s = "cristhian"
  88.         np_e = msg_num(message_s)
  89.         ne_e = modex(np_e, e_r, n_r)
  90.         send(ne_e, client)
  91.         print ne_e
  92.         print "Send"
  93.  
  94.     client.close()
  95.        
  96. main() 
  97. ()
');