# Practical 5 – Encrypted File Transfer: Client
# Install: pip install cryptography
import os
import socket
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

KEY = bytes.fromhex("054f0c5c692ffa3e0d0fa7d156966812")  # Match server demo key
filename = "sample.txt"

with open(filename, "rb") as file:
    data = file.read()
nonce = os.urandom(12)
encrypted = AESGCM(KEY).encrypt(nonce, data, None)

with socket.socket() as client:
    client.connect(("127.0.0.1", 8888))
    client.sendall(b"alice|secret_password\n")
    if client.makefile("rb").readline().strip() != b"AUTH_OK":
        print("Login failed")
    else:
        client.sendall(nonce + filename.encode() + b"\n"
                       + str(len(encrypted)).encode() + b"\n" + encrypted)
        print("Server:", client.recv(1024).decode())

# What this code does: authenticates to the local file server, encrypts
# sample.txt with AES-GCM, and sends the encrypted file.
