from socket import * from struct import * from Cryptodome.PublicKey import RSA from Cryptodome.Random import get_random_bytes from Cryptodome.Cipher import AES, PKCS1_OAEP skt = socket(AF_INET, SOCK_STREAM) skt.connect(("isoptera.lcsc.edu", 5150)) # generate RSA public/private key pair key = RSA.generate(2048) public_key = key.publickey().export_key() # we won't bother to save it # send over the RSA public key skt.send(public_key) # Receive the encrypted session key enc_session_key = skt.recv(1024) # decrypt the encrypted session key cipher_rsa = PKCS1_OAEP.new(key) session_key = cipher_rsa.decrypt(enc_session_key) # encrypt our message with the session key cipher_aes = AES.new(session_key, AES.MODE_EAX) ciphertext, tag = cipher_aes.encrypt_and_digest("This is a top secret message. There is a gerbil in your house eating the wires to your dishwasher. The dishwasher won't work once it finishes. The gerbil may also be finished once it eats through the insulation".encode("utf-8")); # send the message to the server skt.send(cipher_aes.nonce) skt.send(tag) skt.send(ciphertext) skt.close()