diff --git a/.idea/misc.xml b/.idea/misc.xml index 5403b9e..f8a22e9 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index d843f34..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,4 +1,6 @@ - + + + \ No newline at end of file diff --git a/encryption_utils.py b/encryption_utils.py index 3b70510..92f0d6b 100644 --- a/encryption_utils.py +++ b/encryption_utils.py @@ -1,7 +1,7 @@ from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP, AES from Crypto.Random import get_random_bytes - +from Crypto.Hash import SHA256 class AsymmetricEncryption: def __init__(self): @@ -21,7 +21,6 @@ class AsymmetricEncryption: cipher_rsa = PKCS1_OAEP.new(self.key_pair) return cipher_rsa.decrypt(encrypted_data) - class SymmetricEncryption: def encrypt(self, data, key): cipher_aes = AES.new(key, AES.MODE_GCM) @@ -32,3 +31,7 @@ class SymmetricEncryption: def decrypt(self, nonce, ciphertext, tag, key): cipher_aes = AES.new(key, AES.MODE_GCM, nonce=nonce) return cipher_aes.decrypt_and_verify(ciphertext, tag) + +def calculate_file_hash(file_data): + hash_obj = SHA256.new(file_data) + return hash_obj.digest() diff --git a/example.txt b/example.txt index a47e663..76b02a3 100644 --- a/example.txt +++ b/example.txt @@ -1,2 +1,2 @@ 123456 -adgjl \ No newline at end of file +asdfghjkl \ No newline at end of file diff --git a/file_transfer.py b/file_transfer.py new file mode 100644 index 0000000..b6b664f --- /dev/null +++ b/file_transfer.py @@ -0,0 +1,78 @@ +import socket +import threading +from Crypto.PublicKey import RSA +from Crypto.Cipher import AES, PKCS1_OAEP +from Crypto.Random import get_random_bytes +from Crypto.Hash import SHA256 + +class FileTransferApp: + def __init__(self, host, port): + self.host = host + self.port = port + self.private_key = None + self.public_key = None + self.running = True + + def generate_keys(self): + key = RSA.generate(2048) + self.private_key = key.export_key() + self.public_key = key.publickey().export_key() + + def send_file(self, filepath, receiver_host): + try: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.connect((receiver_host, self.port)) + sender = Sender(filepath, receiver_public_key=self.public_key) + data_to_send = sender.send_file() + s.sendall(data_to_send) + print("File sent successfully.") + except Exception as e: + print(f"An error occurred while sending the file: {e}") + + def receive_file(self): + try: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((self.host, self.port)) + s.listen() + print(f"Server listening on {self.host}:{self.port}") + conn, addr = s.accept() + with conn: + print(f"Connected by {addr}") + data = b'' + while True: + packet = conn.recv(1024) + if not packet: + break + data += packet + receiver = Receiver(private_key=self.private_key) + decrypted_data = receiver.receive_file(data) + if decrypted_data: + # Save or process the decrypted file data + with open('received_file', 'wb') as f: + f.write(decrypted_data) + print("File received and decrypted successfully.") + except Exception as e: + print(f"An error occurred while receiving the file: {e}") + + def run(self): + while self.running: + command = input("Enter 'generate_keys' to generate keys, 'send' to send a file, 'receive' to receive a file, or 'exit' to quit: ").strip().lower() + if command == 'generate_keys': + self.generate_keys() + print("Keys generated successfully.") + elif command == 'send': + filepath = input("Enter the path of the file to send: ") + receiver_host = input("Enter the receiver's host (IP address): ") + threading.Thread(target=self.send_file, args=(filepath, receiver_host)).start() + elif command == 'receive': + threading.Thread(target=self.receive_file).start() + elif command == 'exit': + self.running = False + print("Exiting application.") + break + else: + print("Invalid command.") + +if __name__ == "__main__": + app = FileTransferApp(host='0.0.0.0', port=65432) + app.run()