# Practical 3 – Secure Communication: Sender
# Install: pip install cryptography
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import padding

with open("receiver_public_key.pem", "rb") as file:
    receiver_key = serialization.load_pem_public_key(file.read())
with open("sender_private_key.pem", "rb") as file:
    sender_key = serialization.load_pem_private_key(file.read(), password=None)

message = input("Message (keep short for RSA): ").encode()
encrypted = receiver_key.encrypt(message, padding.OAEP(
    mgf=padding.MGF1(hashes.SHA256()), algorithm=hashes.SHA256(), label=None
))
signature = sender_key.sign(message, padding.PSS(
    mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH
), hashes.SHA256())

with open("encrypted_message.bin", "wb") as file:
    file.write(encrypted)
with open("signature.bin", "wb") as file:
    file.write(signature)
print("Message encrypted and signed.")

# What this code does: encrypts a short message for the receiver and signs it
# so the receiver can verify who sent it and whether it changed.
