def recv(s, keys): global serverPublic global RUNNING temp = s.recv(READ_SIZE) tempPublic = temp.split(',') serverPublic[0] = int(tempPublic[0]) serverPublic[1] = int(tempPublic[1]) print "Public key Received" connDets = s.getpeername() remoteIP = connDets[0] remotePort = str(connDets[1]) print "connected to " + remoteIP + ":" + remotePort time.sleep(.1)#sleep used to clean up printing errors due to thread order. print "Sending the server's public" print serverPublic serverPrivate = bruteforce.findPrivate(serverPublic) print "Brute force cracked the server's private key!" print "Server's private d equals " + str(serverPrivate) while RUNNING: message = s.recv(READ_SIZE) if message == "": break msg_list =[] for i in range(0, len(message), BLOCK_SIZE): msg_list.append(message[i: i + BLOCK_SIZE]) decrypted_msg = "" for msg in msg_list: decrypted_msg += rsa.decrypt(int(msg), keys) if decrypted_msg == "message: quit\n\n": break print remoteIP + ":" + remotePort + ": " + decrypted_msg if decrypted_msg.lower() == "quit": RUNNING = False s.close()
def recv(conn, private_keys): """Called by the recv thread. This function receives the client's public key. It then enters a loop to read from the socket and write to the screen. It decrypts the message using its own public key.""" global RUNNING global PUBLIC temp_public_key = "" temp_public_key += conn.recv(READ_SIZE) k1 = temp_public_key.split(",") PUBLIC.append(int(k1[0])) PUBLIC.append(int(k1[1])) print "Public key Received" time.sleep(.1) connDets = conn.getpeername() remoteIP = connDets[0] remotePort = str(connDets[1]) print remoteIP + ":" + remotePort + " is connected" #Use brute force method to crack the client's private key clientPrivate = bruteforce.findPrivate(PUBLIC) print "Brute force cracked the client's private key!" print "Client's private d equals " + str(clientPrivate) #While the client is connected, wait for messages while RUNNING: try: message = conn.recv(READ_SIZE) except socket.error: print "Disconnecting... Goodbye." conn.close() #Parse out the decoded message into preset block size msg_list =[] for i in range(0, len(message), BLOCK_SIZE): msg_list.append(message[i: i + BLOCK_SIZE]) decrypted_msg = "" #Decode the message. Note that decrypt works in blocksize chunks for msg in msg_list: decrypted_msg += rsa.decrypt(int(msg), private_keys) print remoteIP + ":" + remotePort + ": " + decrypted_msg if decrypted_msg.lower() == "quit": print "Client exited. Type quit to close the server." RUNNING = False conn.close() #Close the socket if we managed to get out of while loop without closing conn.close()