As of right now, I have my program working where it encrypts the message in the server program and then sends it to the client which is a separate program. The client then decrypts the message and prints the plaintext message to the terminal. Currently, it is only set up to do this once, but I was wondering how I could do this to have it run 100 times. The library for the cryptography algorithms is the python cryptography library. My end goal would be if the user decides to enter "2" which is the choice where the program would do encryption and decryption 100 times. The server would be able to send each encrypted message to the client and the client would decrypt it, doing that each time for 100 times.
AES_alice.py (server code)
import os, socket, threading
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
PORT = 7777
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
def generate_key_AES():
# generate secret key and write key to shared file
secret_key = os.urandom(16)
return secret_key
def encrypt_AES(plaintext_msg, key):
# add padding bits to message if message is too long or too short (not multiple of key)
padder = padding.PKCS7(128).padder()
plaintext_msg = padder.update(plaintext_msg)
plaintext_msg += padder.finalize()
# create random initilization vector
iv = os.urandom(16)
# begin the AES encryption
AES_cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
AES_encryptor = AES_cipher.encryptor()
AES_ciphertext = AES_encryptor.update(plaintext_msg) + AES_encryptor.finalize()
return iv, AES_ciphertext
# establish connection with client and accept request
server = socket.socket()
server.bind(ADDR)
server.listen()
plaintext_msg = ""
while True:
user_choice = input("Enter 1 for Encryption --- Enter 2 for AES time testing --- Enter 0 for Program Quit: ")
if int(user_choice) == 1:
while len(plaintext_msg) != 18:
plaintext_msg = input("Enter plaintext message to be encrypted (must be 18 bytes): ")
plaintext_msg = bytes(plaintext_msg, 'utf-8')
key = generate_key_AES()
iv, ciphertext = encrypt_AES(plaintext_msg, key)
client, addr = server.accept()
client.send(key)
client.send(iv)
client.send(ciphertext)
elif int(user_choice) == 2:
while len(plaintext_msg) != 7:
plaintext_msg = input("Enter plaintext message to be encrypted (must be 7 bytes): ")
plaintext_msg = bytes(plaintext_msg, 'utf-8')
key = generate_key_AES()
iv, ciphertext = encrypt_AES(plaintext_msg, key)
client, addr = server.accept()
client.send(key)
client.send(iv)
client.send(ciphertext)
elif int(user_choice) == 0:
server.close()
exit()
AES_bob.py (client code)
import socket, threading
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
PORT = 7777
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
def decrypt_AES(key, iv, ciphertext_msg):
# begin the AES decryption
AES_cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
AES_decryptor = AES_cipher.decryptor()
AES_plaintext = AES_decryptor.update(ciphertext_msg)
# unpad the resulting ciphertext to only leave the original message
unpadder = padding.PKCS7(128).unpadder()
AES_plaintext = unpadder.update(AES_plaintext)
AES_plaintext += unpadder.finalize()
print(AES_plaintext.decode())
# establish connection with server and save values sent from server
client = socket.socket()
client.connect(ADDR)
key = client.recv(16)
iv = client.recv(16)
ciphertext = client.recv(32)
# call the decrypt function and pass in correct values
print("Message decrypted using AES decryption:")
decrypt_AES(key, iv, ciphertext)
client.close()