# Python TCP Client A import socket # Basic cryptography tools from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives.asymmetric import rsa # To format created key obj into text to be used/exported from cryptography.hazmat.primitives import serialization # Home made RSA Utils from utils.rsa_tenamortech_utils import * from utils.client_keys_manager import * host = socket.gethostname() port = 2004 BUFFER_SIZE = 2000 #MESSAGE = input("tcpClientA: Enter message/ Enter exit:").encode('utf-8') MESSAGE = "" tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcpClientA.connect((host, port)) got_pub_key_server = False (private_key,public_key) = check_for_existing_keys() public_key_pem = public_key_serializer(public_key) def de_serialize_pub_key(public_key_pem): return serialization.load_pem_public_key(public_key_pem,backend=default_backend()) while MESSAGE != 'exit': data = tcpClientA.recv(BUFFER_SIZE) if not got_pub_key_server: public_key_server = de_serialize_pub_key(data) got_pub_key_server = True # Now we have the pub key of the server, we will send our pub key too #encrypted_public_key = encrypt_msg(public_key_pem, public_key_server) encrypted_public_key = encrypt_msg(bytes("test123456789000000000000000000000000000iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii000", 'utf-8'), public_key_server) print("Server public key received") print("Sending current client public key [ ... ] ") tcpClientA.send(encrypted_public_key) print("Sending current client public key [ OK ] ") else: print(" Client2 received data:", data) MESSAGE = input("tcpClientA: Enter message to continue/ Enter exit:").encode('utf-8') tcpClientA.send(MESSAGE) tcpClientA.close()