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

hex instead of binaries

parent 9f33c832
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ from Crypto.Cipher import DES, Blowfish, ARC4, PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Util.Padding import pad
from Crypto.Random import get_random_bytes
import base64
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'setup')))
......@@ -21,7 +22,7 @@ def load_public_key():
def encrypt_key_with_rsa(public_key, key):
cipher = PKCS1_OAEP.new(public_key)
encrypted_key = cipher.encrypt(key)
return encrypted_key # return as raw bytes, not base64 encoded
return base64.b64encode(encrypted_key).decode()
# Encrypt entered user message according to selected algorithm and key length
def encrypt_message(algorithm, key, message):
......@@ -30,17 +31,23 @@ def encrypt_message(algorithm, key, message):
if algorithm == 'DES':
cipher = DES.new(key, DES.MODE_CBC)
ciphertext = cipher.encrypt(pad(message.encode(), DES.block_size))
return cipher.iv + ciphertext # return raw bytes (iv + ciphertext)
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))
return cipher.iv + ciphertext # return raw bytes (iv + ciphertext)
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())
return ciphertext # return raw bytes of the encrypted message
encrypted_message = ciphertext.hex()
logging.info("RC4 encryption successful.")
return encrypted_message
else:
logging.error("Unsupported encryption algorithm.")
......@@ -48,12 +55,12 @@ def encrypt_message(algorithm, key, message):
# Send data to server
def send_message_to_server(host, port, algorithm, key, key_length, encrypted_message, encrypted_key):
# Configure data to send (we no longer encode the encrypted data in base64)
# Configure data to send
data = {
'algorithm': algorithm,
'key_length': key_length,
'encrypted_key': encrypted_key, # raw bytes
'encrypted_message': encrypted_message # raw bytes
'encrypted_key': encrypted_key,
'encrypted_message': encrypted_message
}
# Start connection to server and send data
......@@ -62,7 +69,6 @@ def send_message_to_server(host, port, algorithm, key, key_length, encrypted_mes
try:
s.connect((host, port))
logging.info(f"Connected to server at {host}:{port}")
# Send the raw data (no base64 encoding)
s.sendall(json.dumps(data).encode())
logging.info("Data sent to server.")
except Exception as e:
......@@ -156,4 +162,4 @@ def main():
break
if __name__ == '__main__':
main()
main()
\ No newline at end of file
......@@ -27,8 +27,9 @@ def decrypt_key_with_rsa(private_key, encrypted_key):
def decrypt_message(algorithm, key, encrypted_message):
logging.info(f"Starting decryption with algorithm: {algorithm}")
# No base64 decoding here since we're working with raw bytes
logging.info(f"Encrypted message (raw bytes): {encrypted_message.hex()}")
# Transform received message from hex to binary
encrypted_message = bytes.fromhex(encrypted_message)
logging.info(f"Encrypted message: {encrypted_message.hex()}")
if algorithm == 'DES':
iv = encrypted_message[:8]
......@@ -61,17 +62,17 @@ def decrypt_message(algorithm, key, encrypted_message):
def handle_client(conn, addr, private_key):
logging.info(f"Handling client {addr}")
# Receive data from client
data = conn.recv(1024)
# 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.decode())
data = json.loads(data)
algorithm = data['algorithm']
key_length = data['key_length']
encrypted_key = data['encrypted_key'] # Now it's raw bytes, no base64 encoding
encrypted_message = data['encrypted_message'] # Raw bytes
encrypted_key = base64.b64decode(data['encrypted_key']) # Assuming encrypted_key is still in Base64
encrypted_message = data['encrypted_message']
logging.info(f"Algorithm: {algorithm}, Key Length: {key_length}")
except json.JSONDecodeError as e:
logging.error(f"Error decoding data: {e}")
......@@ -131,4 +132,4 @@ def main():
continue
if __name__ == '__main__':
main()
main()
\ No newline at end of file
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