Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import socket
- import random
- from rsa_math import *
- def msg_num(message_e):
- np_e = 0
- for char in message_s:
- np_s <<= 8
- np_s += ord(char)
- return np_s
- def num_msg(np_r):
- nm = []
- while np_r > 0:
- nm.insert(0, chr(np_r & 255))
- np_r >>= 8
- message_r = "".join(nm)
- return message_r
- def encrypt(np, e_r, n_r):
- ne_e = pow(np, e_r) % n_r
- return ne_e
- def decrypt(ne_r, d, n):
- np_r = pow(ne_r, d) % n
- return np_r
- def extended_gcd(a, b):
- x, ant_x = 0, 1
- y, ant_y = 1, 0
- coc=0
- while b:
- coc = a // b
- a, b = b, a % b
- x, ant_x = ant_x - coc*x, x
- y, ant_y = ant_y - coc*y, y
- return (ant_x, ant_y, a)
- def send(holaa, client):
- client.send(str(holaa))
- return None
- def received(client):
- algo = client.recv(1024)
- return int(hola)
- def main():
- client = socket.socket()
- client.connect(("localhost",1122))
- print "Connected"
- p = 13
- q = 11
- r = p*q
- phi = (p - 1)*(q - 1)
- e = 23
- gcd = 0
- while (gcd != 1):
- gcd = extended_gcd(e, phi)
- d = mod_inver(e, phi)
- n_r = received(client)
- e_r = received(client)
- print "Key received"
- print n_r, e_r
- send(n, client)
- send(e, client)
- print "Sent key"
- print n, e
- mensaje_r = " "
- print "Starting"
- while (message_r != "End") :
- ne_r = received(client)
- print ne_r
- np_r = modex(ne_r, d, n)
- print np_r
- message_r = num_msg(np_r)
- print "Received: ", message_r
- message_s = "cristhian"
- np_e = msg_num(message_s)
- ne_e = modex(np_e, e_r, n_r)
- send(ne_e, client)
- print ne_e
- print "Send"
- client.close()
- main()
- ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement