S.listen() Conn, Addr = S.accept() with Conn: #Accepts a file name from the client and makes them repeat it if they entered #an incorrect filename while True: Filename = Conn.recv(4096) Filename = MyIO.TrimSocket(Filename) FileText = MyIO.ReadFile(Filename) if(FileText == ""): Conn.sendall(bytes("bad", "utf-8")) else: Conn.sendall(bytes("good", "utf-8")) break #Sets the private key of the server MyPrivKey = Encryption.PrivateKey(d, n) #Sends the public key Conn.send(bytes(str(e) + " " + str(n), "utf-8")) #Sets the client's public key publickey = MyIO.TrimSocket(Conn.recv(4096)) ce = int(publickey.split()[0]) cn = int(publickey.split()[1]) MyPubKey = Encryption.PublicKey(ce, cn) #Signs the message being sent to the client SignedMessage = MyPrivKey.DigitallySign(FileText) #Encrypts the message with the client's public key EncryptedMessage = MyPubKey.Encrypt(SignedMessage) #Sends the signed message to the client Conn.sendall(bytes(EncryptedMessage, "utf-8"))
DestinationFilename = MyIO.GetNewFile("Enter the name of the file you want to store the results in: ") with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as S: S.connect((Host, Port)) #Gets the file to request from the server while True: Filename = input("Enter the name of the file you want to get from the server: ") S.sendall(bytes(Filename, "utf-8")) Good = S.recv(4096) Good = MyIO.TrimSocket(Good) if(Good == "good"): break else: print("You've entered an invalid file name. Please try again.") #Sets the server's public key publickey = MyIO.TrimSocket(S.recv(4096)) e = int(publickey.split()[0]) n = int(publickey.split()[1]) MyPubKey = Encryption.PublicKey(e, n) #Sets the client's private key MyPrivKey = Encryption.PrivateKey(MyD, MyN) #Sends the client's public key S.sendall(bytes(str(MyE) + " " + str(MyN), "utf-8")) #Receives the signed and encrypted message Message = MyIO.TrimSocket(S.recv(1048576)) #Decrypts the message Message = MyPrivKey.Decrypt(Message) #Decrypts the hash, verifies it, and stores it in a file SignedHash = Message.split("Hash:")[1] Message = Message.split("Hash:")[0] MyPubKey.DecryptHash(Message, SignedHash, DestinationFilename)