Example #1
0
                the server responds to the client and then closes the socket
                the client receives quit and closes his socket. '''

            if session_key == None:
                # if Diffie-Hellman didn't offer a key!
                print("No key derived! ")
                connectedClientSocket.close()
                exit(1)

            else:
                print("Now we can send encrypted messages! ")
                message1 = "Hi. This is the server. You can send me messages securely now and i will eco back! If you want to quit just type \'quit\'"
                MessageUtils.send_encrypted_message(connectedClientSocket, message1, session_key)

                while True:
                    receivedMessage = MessageUtils.receive_encrypted_message(connectedClientSocket, session_key)
                    if receivedMessage != 'quit':
                        MessageUtils.send_encrypted_message(connectedClientSocket, receivedMessage, session_key)
                    else:
                        print("\'quit\' detected, closing connection!")
                        MessageUtils.send_encrypted_message(connectedClientSocket, receivedMessage, session_key)
                        connectedClientSocket.close()
                        break

        break

    break



Example #2
0
    session_key = ExchangeDHCli.exchangeDHCli(tcpSocket)

    # this is all that's needed to derive the key!
    ''' as an example is brought a simple echo program.
        The user inputs a message, the client encrypts it with the session key and then sends it to the server.
        The server decrypts it checks if it equals to 'quit' and if so it closes the connection.
        If the message is not quit it echoes the message to the client!
        The client checks if the message says 'quit' and if so it closes the socket. If not it just asks the
        user to send another message! '''

    if session_key is None:
        print("No key was derived! ")
        exit(1)
    else:
        print("Now we can send encrypted messages\n\n")
        message = MessageUtils.receive_encrypted_message(tcpSocket, session_key)
        print("Server: " + message)

        while True:
            mess = input("Type a message: ")
            if mess == 'quit':
                print("\'quit\' detected, closing connection ... ")
                MessageUtils.send_encrypted_message(tcpSocket, mess, session_key)
                tcpSocket.close()
                break
            else:
                MessageUtils.send_encrypted_message(tcpSocket, mess, session_key)
                response = MessageUtils.receive_encrypted_message(tcpSocket, session_key)
                print("Server: ", response)

    break