# Practical 5 – RSA Public-Key Authentication: Client
# Install: pip install cryptography
import socket
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding

with open("client_private.pem", "rb") as file:
    private_key = serialization.load_pem_private_key(file.read(), password=None)

with socket.socket() as client:
    client.connect(("127.0.0.1", 7777))
    challenge = b""
    while len(challenge) < 32:
        challenge += client.recv(32 - len(challenge))
    signature = private_key.sign(challenge, padding.PKCS1v15(), hashes.SHA256())
    client.sendall(signature)
    print("Server:", client.recv(1024).decode())

# What this code does: signs the server's random challenge with the client's
# RSA private key to prove possession of that key.
