Skip to content
Snippets Groups Projects
Commit 6e86d540 authored by Dominik Fuhrmann's avatar Dominik Fuhrmann
Browse files

removed rsa

parent 81e580b6
No related branches found
No related tags found
No related merge requests found
......@@ -2,50 +2,37 @@ import socket
import json
import logging
from colorlog import ColoredFormatter
from Crypto.Cipher import DES, Blowfish, ARC4, PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Util.Padding import pad
from Crypto.Cipher import DES, Blowfish, ARC4
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad
import base64
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'setup')))
from custom_logging import configure_logging, user_input
# 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
# 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()
from custom_logging import configure_logging
# Encrypt entered user message according to selected algorithm and key length
# Verschlüsselung der Nachricht nach gewähltem Algorithmus
def encrypt_message(algorithm, key, message):
logging.info(f"Starting encryption with algorithm: {algorithm}")
if algorithm == 'DES':
cipher = DES.new(key, DES.MODE_CBC)
ciphertext = cipher.encrypt(pad(message.encode(), DES.block_size))
encrypted_message = base64.b64encode(cipher.iv + ciphertext).decode()
encrypted_message = (cipher.iv + ciphertext).hex()
logging.info("DES encryption successful.")
return encrypted_message
elif algorithm == 'Blowfish':
cipher = Blowfish.new(key, Blowfish.MODE_CBC)
ciphertext = cipher.encrypt(pad(message.encode(), Blowfish.block_size))
encrypted_message = base64.b64encode(cipher.iv + ciphertext).decode()
encrypted_message = (cipher.iv + ciphertext).hex()
logging.info("Blowfish encryption successful.")
return encrypted_message
elif algorithm == 'RC4':
cipher = ARC4.new(key)
ciphertext = cipher.encrypt(message.encode())
encrypted_message = base64.b64encode(ciphertext).decode()
encrypted_message = ciphertext.hex()
logging.info("RC4 encryption successful.")
return encrypted_message
......@@ -53,17 +40,17 @@ def encrypt_message(algorithm, key, message):
logging.error("Unsupported encryption algorithm.")
raise ValueError("Unsupported encryption algorithm")
# Send data to server
def send_message_to_server(host, port, algorithm, key, key_length, encrypted_message, encrypted_key):
# Configure data to send
# Funktion zum Senden der Daten an den Server
def send_message_to_server(host, port, algorithm, key, key_length, encrypted_message):
# Konfiguriere die Daten zum Senden
data = {
'algorithm': algorithm,
'key_length': key_length,
'encrypted_key': encrypted_key,
'key': base64.b64encode(key).decode('utf-8'), # Symmetrischer Schlüssel als base64 (Klartext)
'encrypted_message': encrypted_message
}
# Start connection to server and send data
# Verbindung zum Server herstellen und Daten senden
logging.info("Connecting to server...")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
......@@ -79,12 +66,9 @@ def main():
configure_logging()
# server information
host = '192.168.178.51'
host = '192.168.178.63'
port = 12345
# Load RSA public key (for encrypting the key)
public_key = load_public_key()
while True:
# User input for encryption settings
logging.info("Starting the client...")
......@@ -149,11 +133,8 @@ def main():
logging.info(f"Selected: {algorithm}, Key length: {key_length}")
encrypted_message = encrypt_message(algorithm, key, message)
# Encrypt the key with the RSA public key
encrypted_key = encrypt_key_with_rsa(public_key, key)
# Send the data to the server
send_message_to_server(host, port, algorithm, key, key_length, encrypted_message, encrypted_key)
send_message_to_server(host, port, algorithm, key, key_length, encrypted_message)
# Reopen or close connection
again = input("Do you want to send another message? (y/n): ").strip().lower()
......@@ -162,4 +143,4 @@ def main():
break
if __name__ == '__main__':
main()
\ No newline at end of file
main()
......@@ -2,34 +2,20 @@ import socket
import json
import logging
from colorlog import ColoredFormatter
from Crypto.Cipher import DES, Blowfish, ARC4, PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Cipher import DES, Blowfish, ARC4
from Crypto.Util.Padding import unpad
import base64
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'setup')))
from custom_logging import configure_logging, user_input
from custom_logging import configure_logging
# 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
# 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 key: {key.hex()}")
return key
# Decrypt received message using the key, depending on algorithm
# Entschlüsselung der empfangenen Nachricht mit dem symmetrischen Schlüssel
def decrypt_message(algorithm, key, encrypted_message):
logging.info(f"Starting decryption with algorithm: {algorithm}")
# Transform received message into binaries
encrypted_message = base64.b64decode(encrypted_message)
# Umwandlung der empfangenen Nachricht von Hex zu Binär
encrypted_message = bytes.fromhex(encrypted_message)
logging.info(f"Encrypted message: {encrypted_message.hex()}")
if algorithm == 'DES':
......@@ -59,8 +45,8 @@ 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):
# Handle client connection
def handle_client(conn, addr):
logging.info(f"Handling client {addr}")
# Receive up to 1024 Bytes data from client
......@@ -72,19 +58,14 @@ def handle_client(conn, addr, private_key):
data = json.loads(data)
algorithm = data['algorithm']
key_length = data['key_length']
encrypted_key = base64.b64decode(data['encrypted_key'])
key = base64.b64decode(data['key']) # Symmetrischer Schlüssel als base64 (Klartext)
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}")
......@@ -106,9 +87,6 @@ def main():
host = '192.168.178.63'
port = 12345
# Load RSA private key (for decrypting the key)
private_key = load_private_key()
# Wait for incoming connections
logging.info("Starting server...")
......@@ -125,12 +103,13 @@ def main():
while True:
try:
conn, addr = s.accept()
logging.info("\n")
logging.info(f"Connection from {addr}")
handle_client(conn, addr, private_key)
handle_client(conn, addr)
conn.close()
except Exception as e:
logging.error(f"Error handling connection: {e}")
continue
if __name__ == '__main__':
main()
\ No newline at end of file
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment