Skip to content

Instantly share code, notes, and snippets.

@eddypre
Created September 13, 2012 14:42
Bob :P
#!/usr/bin/python
#Carlos Triana
import socket #Library to use sockets
from random import randint
p = 13 #p and q must be primes
q = 17
n = p*q
Phin = (p-1)*(q-1)
d = 29
e = 53 # "e" value can't have the same factors of Phi(n), and 1<=e<=Phi(n)
print n
print Phin
s = socket.socket()
s.bind(("localhost", 9999)) #Ip Server
s.listen(1)
sc, addr = s.accept() #To accept connections
print "Conectado con IP: "+str(addr)
#x = randint(0, 20)
x = 2 #X value must be random, but in this case x = 2, because i calculated p,q,d and e in paper/pencil.
while True:
recibido = sc.recv(1024)
if recibido == "quit": #To exit the connection type quit
break #Close the connection
print "Recibido:", recibido #Printing the received
print ">>Sending public key(d) ..."
sc.send(str(e)) #Sending values
print ">>Sending n"
sc.send(str(n))
print ">>Sending a value x"
print ">>Sending x = "+str(x)
sc.send(str(x))
print ">>Recibing y ..."
recibingY = sc.recv(1024) #Receiving values
Y = int(recibingY)
print ">>Recibing r ..."
recibingR = sc.recv(1024)
R = int(recibingR)
MyY = (pow(R, d))%(n)
if MyY == Y: #If "y" calculated is equal to "y" received, send: "Access permited"
print "Access permited :)"
else: #If y's values are not equal, send: "Acces denied"
print "Access denied, get the fuck out of here"
print "Bye"
sc.close()
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment