(the names of my variables are different than those we saw in class because I based the variable names on the book "Discrete Mathematics, Richard Johonsonbaugh"
)
- Create 3 scripts, one that generate public and private keys, another that is client.py and one for the server.py.
- For keys first generate a list of primes where I chose the p and q
- After, the n in my program is part of the public key. I chose the prime number as n. I noticed that phi is not a multiple of n.
- Immediately implements the Euclidean algorithm to get the s(private key)
- and finally create two key files, one public.txt and one private.txt
- I create the client.py and server.py and follow the next steps:
RSA:
This file contains 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
#!/usr/bin/python | |
import random | |
def genera_primos(): | |
"""hago una lista de los primero 10 numeros primos | |
para despues de ahi elegir p,q, y n | |
""" | |
numero = 2 | |
total = 0 | |
primos = list() | |
while total <= 10: | |
div = 0 | |
cont = 1 | |
while cont <= numero: | |
res = numero % cont | |
if res == 0: | |
div = div + 1 | |
cont = cont + 1 | |
if div <= 2: | |
total = total + 1 | |
primos.append(numero) | |
numero = numero + 1 | |
return primos | |
def escoge_p_q(): | |
"""eligo p y q al azar decidi que no se repitieran | |
""" | |
primos = genera_primos() | |
p = random.choice(primos) | |
primos.remove(p) | |
q = random.choice(primos) | |
return p, q | |
def escoge_n(o): | |
"""escogo n al azar de la lista de numeros primos | |
la escogo prima para que pueda cumplir mas facil las caracteristicas | |
de ser coprima con o=(p-1)(q-1) | |
""" | |
primos = genera_primos() | |
n = random.choice(primos) | |
if o%n == 0 or n>o: | |
return escoge_n(o) | |
return n | |
def euclides(n, o): | |
"""en esta funcion obtengo la s, | |
que hace que (s*n mod o) sea igual a 1 | |
""" | |
a = n | |
b = o | |
s, t, s1, t1 = 1, 0, 0, 1 | |
while b != 0: | |
q = a/b | |
r = a%b | |
a, s, t, b, s1, t1 = b, s1, t1, r, s-(s1*q), t-t1*q | |
s = s%o | |
return s | |
def escribo_clave_publica(): | |
usuarios = ['cecy', 'lupita', 'pedrito'] | |
f = open("claves_publicas.txt", "w") | |
datos = {} | |
for i in usuarios: | |
p, q = escoge_p_q() | |
print "La p y la q son:" | |
print p, q | |
z = p * q | |
print "La z es:" | |
print z | |
o = (p-1)*(q-1) | |
print "La o es:" | |
print o | |
n = escoge_n(o) | |
print "La n es:" | |
print n | |
clave_publica = z, n | |
print "La clave publica es:" | |
print clave_publica | |
f.write(""+str(i)+", "+str(n) + ", "+str(z)+"\n") | |
datos[i] = [p, q, z, o, n] | |
f.close() | |
return datos | |
def escribo_clave_privada(datos): | |
usuarios = ['cecy', 'lupita', 'pedrito'] | |
f = open("claves_privadas.txt", "w") | |
for i in datos: | |
numeros = datos[i] | |
print numeros | |
p = numeros[0] | |
q = numeros[1] | |
z = numeros[2] | |
o = numeros[3] | |
n = numeros[4] | |
s = euclides(n, o) | |
print "La s encontrada es:" | |
print s | |
x = int(random.random()*100) | |
print "la x es:" | |
print x | |
f.write(""+str(i)+", "+str(s) + ", "+str(z)+"\n") | |
def main(): | |
datos = escribo_clave_publica() | |
print datos | |
escribo_clave_privada(datos) | |
main() |
Server:
This file contains 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
#!/usr/bin/python | |
import socket | |
import random | |
def f(x): | |
y = x*29 | |
return y | |
def leer_claves(): | |
f = open("claves_publicas.txt") | |
usuarios = {} | |
for l in f.readlines(): | |
linea = l.split("\n")[0] | |
linea = linea.split(",") | |
#print linea | |
usuarios[linea[0]] = [linea[1], linea[2]] | |
return usuarios | |
def main(): | |
usuarios = leer_claves() | |
socket_1 = socket.socket() | |
socket_1.bind(("localhost", 9999)) | |
print "Servidor iniciado" | |
socket_1.listen(1) | |
socket_2, addr = socket_1.accept() | |
x = int(random.random()*100) | |
socket_2.send(str(x)) | |
datos = socket_2.recv(512) | |
datos = datos.split(",") | |
print datos | |
usuario = datos[0] | |
r = int(datos[1]) | |
if usuario in usuarios: | |
print usuario | |
clave = usuarios[usuario] | |
n = clave[0] | |
z = clave[1] | |
y = (int(r)**int(n))%int(z) | |
if f(x) == y: | |
print "Bienvenido al sistema" | |
else: | |
print "Algo esta mal con las igualdades" | |
else: | |
print "Algo esta mal con el nombre" | |
socket_1.close() | |
socket_2.close() | |
main() |
Client:
This file contains 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
#!/usr/bin/python | |
import socket | |
def leer_claves(): | |
f = open("claves_privadas.txt") | |
usuarios = {} | |
for l in f.readlines(): | |
linea = l.split("\n")[0] | |
linea = linea.split(",") | |
usuarios[linea[0]] = [linea[1], linea[2]] | |
return usuarios | |
def f(x): | |
y = x*29 | |
return y | |
def main(): | |
usuarios = leer_claves() | |
socket_c = socket.socket() | |
socket_c.connect(("localhost", 9999)) | |
print "Servidor iniciado" | |
x = socket_c.recv(512) | |
x = int(x) | |
y = f(x) | |
usuario = raw_input("Usuario: ") | |
print usuario | |
if usuario not in usuarios: | |
print "Algo esta mal" | |
socket_c.close() | |
return | |
datos = usuarios[usuario] | |
s = int(datos[0]) | |
z = int(datos[1]) | |
r = (y**s)%z | |
enviar = ""+usuario+","+str(r) | |
socket_c.send(enviar) | |
socket_c.close() | |
main() |
Corrects users and keys:
Incorrect user:
Extra points
For y = (int(r)**int(n))%int(z), you lose a point. Avoid elevating to large exponents in this way in modular arithmetic. 6 pts.
ResponderEliminarThanks for the above tutorial. You explained the today's chapter so effectively.Its too much lengthy but all the necessary details explained in your chapter.You did a good job.
ResponderEliminardigital signature software