# serwer_szyfrowany.py
# Odbiór wiadomości
import hashlib
import random
import os
from socket import *
from cryptography.fernet import Fernet

key = Fernet.generate_key()
f = Fernet(key)

print("Klucz:", str(key, 'utf-8'))
    
host = ""
port = 8080
buf = 1024
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(addr)
print("Oczekuję na wiadomość...")

def decrypt(ciphertext):
    try:
        msg = f.decrypt(ciphertext)
    except:
        msg = ciphertext
    return msg

while True:
    (data, addr) = UDPSock.recvfrom(buf)
    h = hashlib.md5(data)
    
    plaintext = decrypt(data)
    msg = str(plaintext, 'utf-8')
    print ("Wiadomość: " + msg)
    if msg == "exit":
        break
    if msg == "newkey":
        key = Fernet.generate_key()
        f = Fernet(key)
        print("Klucz:", str(key, 'utf-8'))

UDPSock.close()
os._exit(0)

