This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from Crypto.Util.number import getPrime | |
from random import uniform | |
from sys import argv | |
def test_prime(num): | |
for i in range(2,num): | |
if num % i == 0: | |
return False | |
return True | |
def generate_prime(): | |
while True: | |
num = int(uniform(10,100)) | |
if test_prime(num): | |
break | |
return num | |
#where gcd means greatest common denominator) | |
def gcd(phi,e): | |
if phi % e == 0: | |
return e | |
else: | |
return gcd(e, phi%e) | |
def generate_e(phi): | |
while True: | |
e=generate_prime() | |
if gcd(phi,e)==1: | |
return e | |
break | |
def gcde(a,b): | |
x, X = 0, 1 | |
y, Y = 1, 0 | |
while(b != 0): | |
q = a / b | |
(a, b) = (b, a%b) | |
(x, X) = (X - q * x, x) | |
(y, Y) = (Y - q * y, y) | |
return(X, Y, a) | |
def inverse_modulo(e,phi): | |
d,q,gcd =gcde(e,phi) | |
if gcd == 1: | |
return (d + phi) % phi | |
else: | |
return 0 | |
def keys(username,e,d,n): | |
r1=open('public_key.txt','w') | |
r2=open(str(username)+'.txt','w') | |
r1.write(username + ' ' + str(e) + ' ' + str(n) + '\n') | |
r2.write(username + ' ' + str(d) + ' ' + str(n) + '\n') | |
r1.close() | |
r2.close() | |
def main(): | |
try: | |
username=argv[1] | |
except: | |
username=raw_input("Username: ") | |
print 'username', username | |
#p and q are prime numbers | |
p=generate_prime() | |
q=generate_prime() | |
print 'p', p | |
print 'q', q | |
#calculate n | |
n=p*q | |
print 'n', n | |
#calculate the Euler function OB=phi | |
phi = (p-1)*(q-1) | |
print 'phi', phi | |
#We choose a positive integer less than phi | |
#e must be relatively prime to phi | |
e = generate_e(phi) | |
print 'e', e | |
d=inverse_modulo(e, phi) | |
print 'd', d | |
if d<0: | |
d = d+phi | |
keys(username,e,d,n) | |
main() |
Codigo del server:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
import socket,sys,pickle | |
from math import sqrt | |
def get_password(usuario): | |
file = open('public_key.txt','r') | |
for line in archivo.readlines(): | |
if len(line)==0: | |
continue | |
lin = line.split() | |
if lin[0] == usuario: | |
dates=[int(lin[1]),int(lin[2])] | |
return dates | |
else: | |
line +=1 | |
file.close() | |
return False | |
def function(x): | |
return (x*(xsqrt(4))+2) | |
def main(): | |
#Function socket.socket creates a socket and returns a socket descripto | |
#Propierties: | |
#Address Family: AF_INET(this is IP version 4) | |
#Type: SOCK_STREAM(this means connection oriented TCP protocol) | |
socket_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
print 'Socket Created' | |
host = 'localhost' | |
port = 8005 | |
socket_s.bind((host,port)) #Se crea la conexion al IP y puerto | |
print 'Socket bind complete' | |
socket_s.listen(1) | |
print 'Socket now listening' | |
print 'Esperando cliente' | |
while 1:#loop to serve customers | |
#wait to accept a connection - blocking call | |
socket_client, address_client = socket_s.accept() | |
print 'Connection established...' | |
print 'La direccion del cliente es: ', address_client | |
#send the public key over | |
x = str(randint(1,100)) | |
print 'x' ,x | |
usuario=socket_client.recv(512) | |
print 'usuario', usuario | |
socket_client.send(x) | |
#socket_client.send(pickle.dumps(rsapublickey)) | |
entrada=True | |
while entrada: | |
request = socket_client.recv(512) | |
dates=get_password(request) | |
if dates==False: | |
print 'No eres ' | |
socket_s.close() | |
client_socket.close() | |
e=dates[0] | |
n=dates[1] | |
y = (int(request)**e)%n | |
if function(x)==y: | |
socket_client.send('Welcome...') | |
else: | |
socket_client.send('Failure...') | |
socket_client.close() | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
from math import sqrt | |
def get_password(usuario): | |
file = open(usuario+'txt','r') | |
line=file.readlines() | |
usuario = line[0].split() | |
file.close() | |
return usuario | |
def function(x): | |
return (x*(xsqrt(4))+2) | |
def main(): | |
host = 'localhost' | |
port = 8005 | |
#Function socket.socket creates a socket and returns a socket descriptor | |
#Propierties: | |
#Address Family: AF_INET(this is IP version 4) | |
#Type: SOCK_STREAM(this means connection oriented TCP protocol) | |
socket_c = socket.socket (socket.AF_INET, socket.SOCK_STREAM) | |
socket_c.connect((host,port)) | |
print 'Client.' | |
x=socket_c.recv(512) | |
print 'x' | |
rx=int(x) | |
y=function(rx) | |
usuario = raw_input("Usuario: ") | |
dates=get_password(usuario) | |
if dates==False: | |
print 'No eres ' | |
socket_c.close() | |
client_socket.close() | |
d=dates[0] | |
n=dates[1] | |
r =str(pow(y,d) % n) | |
socket_c.send(usuario) | |
socket_c.send(r) | |
s.close() | |
main() |
Evita exponentes explícitos tipo y = (int(request)**e)%n y usa mejor un algoritmo logarítmico para eso. También hubiera sido bueno incluir un ejemplo de esto funcionando. Van 5 pts.
ResponderEliminar