diff --git a/communication/client.py b/communication/client.py index 5ba841e23586c2de8d04b1fe8368c298457c65d1..ae8e6cb740eb33eff21ce727eb45e1e0eb914c06 100644 --- a/communication/client.py +++ b/communication/client.py @@ -7,55 +7,21 @@ from Crypto.PublicKey import RSA from Crypto.Util.Padding import pad from Crypto.Random import get_random_bytes import base64 +from ../setup/logging.py import configure_logging, user_input -# Add USER INPUT logging to system -USER_INPUT = 25 -logging.addLevelName(USER_INPUT, "USER INPUT") - -def user_input(msg, *args, **kwargs): - logging.log(USER_INPUT, msg, *args, **kwargs) - -# Configure colored logging -log_formatter = ColoredFormatter( - "%(asctime)s - %(log_color)s%(levelname)-12s%(reset)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", - log_colors={ - 'DEBUG': 'cyan', - 'INFO': 'green', - 'WARNING': 'yellow', - 'ERROR': 'red', - 'CRITICAL': 'magenta', - 'USER INPUT': 'blue', - } -) - -# Show logs on terminal -console_handler = logging.StreamHandler() -console_handler.setFormatter(log_formatter) - -logging.basicConfig(level=logging.DEBUG, handlers=[console_handler]) +# Load the RSA public key to encrypt the key +def load_public_key(): + with open('../keys/public.pem', 'rb') as public_file: + public_key = RSA.import_key(public_file.read()) + return public_key -# Send data to server -def send_message_to_server(host, port, algorithm, key, key_length, encrypted_message, encrypted_key): - data = { - 'algorithm': algorithm, - 'key_length': key_length, - 'encrypted_key': encrypted_key, - 'encrypted_message': encrypted_message - } - - # Start connection to server and send data - logging.info("Connecting to server...") - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: - try: - s.connect((host, port)) - logging.info(f"Connected to server at {host}:{port}") - s.sendall(json.dumps(data).encode()) - logging.info("Data sent to server.") - except Exception as e: - logging.error(f"Connection error: {e}") +# Encrypt the key for secure key exchange using RSA public key +def encrypt_key_with_rsa(public_key, key): + cipher = PKCS1_OAEP.new(public_key) + encrypted_key = cipher.encrypt(key) + return base64.b64encode(encrypted_key).decode() -# Encrypt user message according to selected algorithm and key length +# Encrypt entered user message according to selected algorithm and key length def encrypt_message(algorithm, key, message): logging.info(f"Starting encryption with algorithm: {algorithm}") @@ -84,23 +50,36 @@ def encrypt_message(algorithm, key, message): logging.error("Unsupported encryption algorithm.") raise ValueError("Unsupported encryption algorithm") -# Encrypt the key using RSA public key -def encrypt_key_with_rsa(public_key, key): - cipher = PKCS1_OAEP.new(public_key) - encrypted_key = cipher.encrypt(key) - return base64.b64encode(encrypted_key).decode() - -# Load the RSA public key (for encrypting the symmetric key) -def load_public_key(): - with open('../keys/public.pem', 'rb') as public_file: - public_key = RSA.import_key(public_file.read()) - return public_key +# Send data to server +def send_message_to_server(host, port, algorithm, key, key_length, encrypted_message, encrypted_key): + # Configure data to send + data = { + 'algorithm': algorithm, + 'key_length': key_length, + 'encrypted_key': encrypted_key, + 'encrypted_message': encrypted_message + } + + # Start connection to server and send data + logging.info("Connecting to server...") + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + try: + s.connect((host, port)) + logging.info(f"Connected to server at {host}:{port}") + s.sendall(json.dumps(data).encode()) + logging.info("Data sent to server.") + except Exception as e: + logging.error(f"Connection error: {e}") def main(): - host = '192.168.178.63' + # configure colored logging + configure_logging() + + # server information + host = '127.0.0.1' port = 12345 - # Load the public RSA key (for encrypting the symmetric key) + # Load RSA public key (for encrypting the key) public_key = load_public_key() while True: @@ -163,6 +142,7 @@ def main(): # Get user message message = input("Message: ") + # Encrypt message logging.info(f"Selected: {algorithm}, Key length: {key_length}") encrypted_message = encrypt_message(algorithm, key, message) @@ -172,7 +152,7 @@ def main(): # Send the data to the server send_message_to_server(host, port, algorithm, key, key_length, encrypted_message, encrypted_key) - # Connection open or closed + # Reopen or close connection again = input("Do you want to send another message? (y/n): ").strip().lower() if again != 'y': logging.info("Exiting client...") diff --git a/communication/server.py b/communication/server.py index 54cbe13556ead1a4e986e1bf0935cd1c0ce61138..80f79f5a051d07cdf1135cca251883b6333b914f 100644 --- a/communication/server.py +++ b/communication/server.py @@ -6,92 +6,22 @@ from Crypto.Cipher import DES, Blowfish, ARC4, PKCS1_OAEP from Crypto.PublicKey import RSA from Crypto.Util.Padding import unpad import base64 +from ../setup/logging.py import configure_logging, user_input -# Add USER INPUT logging to system -USER_INPUT = 25 -logging.addLevelName(USER_INPUT, "USER INPUT") - -def user_input(msg, *args, **kwargs): - logging.log(USER_INPUT, msg, *args, **kwargs) - -# Configure colored logging -log_formatter = ColoredFormatter( - "%(asctime)s - %(log_color)s%(levelname)-12s%(reset)s - %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", - log_colors={ - 'DEBUG': 'cyan', - 'INFO': 'green', - 'WARNING': 'yellow', - 'ERROR': 'red', - 'CRITICAL': 'magenta', - 'USER INPUT': 'blue', - } -) - -# Show logs on terminal -console_handler = logging.StreamHandler() -console_handler.setFormatter(log_formatter) - -logging.basicConfig(level=logging.DEBUG, handlers=[console_handler]) - -# Load RSA private key (for decrypting the symmetric key) +# Load the RSA private key to decrypt the key def load_private_key(): with open('../keys/private.pem', 'rb') as private_file: private_key = RSA.import_key(private_file.read()) return private_key -# Load RSA public key (for encrypting the symmetric key) -def load_public_key(): - with open('../keys/public.pem', 'rb') as public_file: - public_key = RSA.import_key(public_file.read()) - return public_key - -# Manage client connection -def handle_client(conn, addr, private_key): - logging.info(f"Handling client {addr}") - - # Receive up to 1024 Bytes data from client - data = conn.recv(1024).decode() - logging.info(f"Received data from client {addr}.") - - # Manage received JSON data and extract information - try: - data = json.loads(data) - algorithm = data['algorithm'] - key_length = data['key_length'] - encrypted_key = base64.b64decode(data['encrypted_key']) - encrypted_message = data['encrypted_message'] - logging.info(f"Algorithm: {algorithm}, Key Length: {key_length}") - user_input(f"Received data from client {addr}: Algorithm: {algorithm}, Key Length: {key_length}, Encrypted message: {encrypted_message}") - except json.JSONDecodeError as e: - logging.error(f"Error decoding data: {e}") - conn.close() - return - - # Decrypt the symmetric key using RSA - key = decrypt_symmetric_key(private_key, encrypted_key) - logging.info(f"Decrypted symmetric key from client {addr}.") - - # Decrypt the message using the symmetric key - decrypted_message = decrypt_message(algorithm, key, encrypted_message) - logging.info(f"Decrypted message from client {addr}: {decrypted_message}") - - # Respond to the client - response = { - "status": "success", - "decrypted_message": decrypted_message - } - conn.sendall(json.dumps(response).encode()) - logging.info(f"Sent response to client {addr}") - -# Decrypt the symmetric key using RSA private key -def decrypt_symmetric_key(private_key, encrypted_key): +# Decrypt the received key using RSA private key +def decrypt_key_with_rsa(private_key, encrypted_key): cipher = PKCS1_OAEP.new(private_key) key = cipher.decrypt(encrypted_key) - logging.info(f"Decrypted symmetric key: {key.hex()}") + logging.info(f"Decrypted key: {key.hex()}") return key -# Decrypt received message using the symmetric key +# Decrypt received message using the key, depending on algorithm def decrypt_message(algorithm, key, encrypted_message): logging.info(f"Starting decryption with algorithm: {algorithm}") @@ -126,12 +56,54 @@ def decrypt_message(algorithm, key, encrypted_message): logging.error("Unsupported algorithm.") raise ValueError("Unsupported decryption algorithm") +# Manage client connection +def handle_client(conn, addr, private_key): + logging.info(f"Handling client {addr}") + + # Receive up to 1024 Bytes data from client + data = conn.recv(1024).decode() + logging.info(f"Received data from client {addr}.") + + # Manage received JSON data and extract information + try: + data = json.loads(data) + algorithm = data['algorithm'] + key_length = data['key_length'] + encrypted_key = base64.b64decode(data['encrypted_key']) + encrypted_message = data['encrypted_message'] + logging.info(f"Algorithm: {algorithm}, Key Length: {key_length}") + user_input(f"Received data from client {addr}: Algorithm: {algorithm}, Key Length: {key_length}, Encrypted message: {encrypted_message}") + except json.JSONDecodeError as e: + logging.error(f"Error decoding data: {e}") + conn.close() + return + + # Decrypt the key using RSA + key = decrypt_key_with_rsa(private_key, encrypted_key) + logging.info(f"Decrypted key from client {addr}.") + + # Decrypt the message using the key + decrypted_message = decrypt_message(algorithm, key, encrypted_message) + logging.info(f"Decrypted message from client {addr}: {decrypted_message}") + + # Respond to the client + response = { + "status": "success", + "decrypted_message": decrypted_message + } + conn.sendall(json.dumps(response).encode()) + logging.info(f"Sent response to client {addr}") + # Define server configuration def main(): - host = '192.168.178.63' + # configure colored logging + configure_logging() + + # server information + host = '127.0.0.1' port = 12345 - # Load RSA private key + # Load RSA private key (for decrypting the key) private_key = load_private_key() # Wait for incoming connections @@ -158,4 +130,4 @@ def main(): continue if __name__ == '__main__': - main() + main() \ No newline at end of file diff --git a/decryption/blowfish_128.py b/decryption/blowfish_128.py new file mode 100644 index 0000000000000000000000000000000000000000..3ee0ddf15ef560a2d805455361af9752ff955fa5 --- /dev/null +++ b/decryption/blowfish_128.py @@ -0,0 +1,32 @@ +import logging +from Crypto.Cipher import Blowfish +from Crypto.Util.Padding import unpad +import itertools +import base64 +from logger import setup_logging # Import logging configuration + +def brute_force_blowfish_128(ciphertext_base64, iv_hex): + ciphertext = base64.b64decode(ciphertext_base64) + iv = bytes.fromhex(iv_hex) + + logging.info("Starting brute-force for Blowfish (128-bit key)...") + for key in itertools.product(range(256), repeat=16): # 128-bit key + key = bytes(key) + try: + cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv) + plaintext = unpad(cipher.decrypt(ciphertext), Blowfish.block_size) + logging.info(f"Decrypted successfully: {plaintext.decode()} with key {key.hex()}") + return plaintext.decode() + except Exception: + continue + logging.error("No matching key found.") + return None + +def main(): + setup_logging() # Set up logging + ciphertext_base64 = input("Enter the Blowfish-encrypted text (Base64): ").strip() + iv_hex = input("Enter the initialization vector (IV) in hexadecimal (16 characters): ").strip() + brute_force_blowfish_128(ciphertext_base64, iv_hex) + +if __name__ == "__main__": + main() diff --git a/decryption/blowfish_64.py b/decryption/blowfish_64.py new file mode 100644 index 0000000000000000000000000000000000000000..cd2f19351ceb602ca682dfa8684f9a2b5da60a24 --- /dev/null +++ b/decryption/blowfish_64.py @@ -0,0 +1,32 @@ +import logging +from Crypto.Cipher import Blowfish +from Crypto.Util.Padding import unpad +import itertools +import base64 +from logger import setup_logging # Import logging configuration + +def brute_force_blowfish_64(ciphertext_base64, iv_hex): + ciphertext = base64.b64decode(ciphertext_base64) + iv = bytes.fromhex(iv_hex) + + logging.info("Starting brute-force for Blowfish (64-bit key)...") + for key in itertools.product(range(256), repeat=8): + key = bytes(key) + try: + cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv) + plaintext = unpad(cipher.decrypt(ciphertext), Blowfish.block_size) + logging.info(f"Decrypted successfully: {plaintext.decode()} with key {key.hex()}") + return plaintext.decode() + except Exception: + continue + logging.error("No matching key found.") + return None + +def main(): + setup_logging() # Set up logging + ciphertext_base64 = input("Enter the Blowfish-encrypted text (Base64): ").strip() + iv_hex = input("Enter the initialization vector (IV) in hexadecimal (16 characters): ").strip() + brute_force_blowfish_64(ciphertext_base64, iv_hex) + +if __name__ == "__main__": + main() diff --git a/decryption/des_64.py b/decryption/des_64.py new file mode 100644 index 0000000000000000000000000000000000000000..6049cd3a6492894eb9f50b78a5a0d6f2102ab6f5 --- /dev/null +++ b/decryption/des_64.py @@ -0,0 +1,31 @@ +import logging +from Crypto.Cipher import DES +from Crypto.Util.Padding import unpad +import itertools +import base64 + +def brute_force_des(ciphertext_base64, iv_hex): + ciphertext = base64.b64decode(ciphertext_base64) + iv = bytes.fromhex(iv_hex) + + logging.info("Starting brute-force for DES (64-bit key)...") + for key in itertools.product(range(256), repeat=8): + key = bytes(key) + try: + cipher = DES.new(key, DES.MODE_CBC, iv) + plaintext = unpad(cipher.decrypt(ciphertext), DES.block_size) + logging.info(f"Decrypted successfully: {plaintext.decode()} with key {key.hex()}") + return plaintext.decode() + except Exception: + continue + logging.error("No matching key found.") + return None + +def main(): + setup_logging() + ciphertext_base64 = input("Enter the DES-encrypted text (Base64): ").strip() + iv_hex = input("Enter the initialization vector (IV) in hexadecimal (16 characters): ").strip() + brute_force_des(ciphertext_base64, iv_hex) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/decryption/rc4_128.py b/decryption/rc4_128.py new file mode 100644 index 0000000000000000000000000000000000000000..31754210dc529a5ebb122e310cb8ca66da4136b6 --- /dev/null +++ b/decryption/rc4_128.py @@ -0,0 +1,29 @@ +import logging +from Crypto.Cipher import ARC4 +import itertools +import base64 +from logger import setup_logging # Import logging configuration + +def brute_force_rc4_128(ciphertext_base64): + ciphertext = base64.b64decode(ciphertext_base64) + + logging.info("Starting brute-force for RC4 (128-bit key)...") + for key in itertools.product(range(256), repeat=16): # 128-bit key + key = bytes(key) + try: + cipher = ARC4.new(key) + plaintext = cipher.decrypt(ciphertext) + logging.info(f"Decrypted successfully: {plaintext.decode()} with key {key.hex()}") + return plaintext.decode() + except Exception: + continue + logging.error("No matching key found.") + return None + +def main(): + setup_logging() # Set up logging + ciphertext_base64 = input("Enter the RC4-encrypted text (Base64): ").strip() + brute_force_rc4_128(ciphertext_base64) + +if __name__ == "__main__": + main() diff --git a/decryption/rc4_64.py b/decryption/rc4_64.py new file mode 100644 index 0000000000000000000000000000000000000000..5fbb0f3c7ef45ebdc04255e309cf91c541ddb3cc --- /dev/null +++ b/decryption/rc4_64.py @@ -0,0 +1,29 @@ +import logging +from Crypto.Cipher import ARC4 +import itertools +import base64 +from logger import setup_logging # Import logging configuration + +def brute_force_rc4(ciphertext_base64): + ciphertext = base64.b64decode(ciphertext_base64) + + logging.info("Starting brute-force for RC4 (64-bit key)...") + for key in itertools.product(range(256), repeat=8): # 64-bit key + key = bytes(key) + try: + cipher = ARC4.new(key) + plaintext = cipher.decrypt(ciphertext) + logging.info(f"Decrypted successfully: {plaintext.decode()} with key {key.hex()}") + return plaintext.decode() + except Exception: + continue + logging.error("No matching key found.") + return None + +def main(): + setup_logging() # Set up logging + ciphertext_base64 = input("Enter the RC4-encrypted text (Base64): ").strip() + brute_force_rc4(ciphertext_base64) + +if __name__ == "__main__": + main() diff --git a/keys/generateKeys.py b/keys/generateKeys.py index 492dd8e0f876eeec85241709503d2dca4988d6b7..77d85456f1cfeb91d2c6e128bbd61b7da223b461 100644 --- a/keys/generateKeys.py +++ b/keys/generateKeys.py @@ -1,19 +1,19 @@ from Crypto.PublicKey import RSA from Crypto.Random import get_random_bytes -# Generiere RSA-Schlüssel und speichere sie +# Generate RSA-keys to en-/decrypt key for save transfer to server def main(): private_key = RSA.generate(2048) public_key = private_key.publickey() - # Speichern der privaten und öffentlichen Schlüssel + # Save as files with open('private.pem', 'wb') as private_file: private_file.write(private_key.export_key()) with open('public.pem', 'wb') as public_file: public_file.write(public_key.export_key()) - print("RSA-Schlüssel wurden generiert und gespeichert.") + print("RSA-keys generated and saved.") if __name__ == '__main__': - main() + main() \ No newline at end of file diff --git a/setup/logging.py b/setup/logging.py new file mode 100644 index 0000000000000000000000000000000000000000..480c48c8a72ea239fa404ef5096caff157c3cc4e --- /dev/null +++ b/setup/logging.py @@ -0,0 +1,30 @@ +import logging +from colorlog import ColoredFormatter + +# Add USER INPUT logging option to system +USER_INPUT = 25 +logging.addLevelName(USER_INPUT, "USER INPUT") + +def user_input(msg, *args, **kwargs): + logging.log(USER_INPUT, msg, *args, **kwargs) + +# Configure colored logging +log_formatter = ColoredFormatter( + "%(asctime)s - %(log_color)s%(levelname)-12s%(reset)s - %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + log_colors={ + 'DEBUG': 'cyan', + 'INFO': 'green', + 'WARNING': 'yellow', + 'ERROR': 'red', + 'CRITICAL': 'magenta', + 'USER INPUT': 'blue', + } +) + +# Show logs on terminal +console_handler = logging.StreamHandler() +console_handler.setFormatter(log_formatter) + +def configure_logging(): + logging.basicConfig(level=logging.DEBUG, handlers=[console_handler]) \ No newline at end of file