kryptografia-semestr-8/serwer.py

52 lines
2.7 KiB
Python

# https://realpython.com/python-sockets/
# echo-server.py
import socket
import sys
from dsa import sig_DSA
byteorder = "big"
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
ip_info = socket.getaddrinfo(socket.gethostname(), PORT)
# print(ip_info)
HOST = ip_info[-1][-1][0]
print()
print("-"*200)
print("tu sie zaczyna program serwer.py")
print(HOST, ":", PORT)
klucz_publiczny_DSA, klucz_prywatny_DSA = (126220152782999491712146716573647923575210663342203877875997463151103734117618780085961998496900968641688295271738389523168262437046367952410100230715977561698699865420228930481833546803155826687224437601550687821059803843460614018330735329864730711512369415813709177511423225057391814747172616206699791431113, 1026153776632400528836961826077414494924816203371, 33135294496110223909298149780807071663246150710874084366253067988796622596843824622279842916377597978486097816392781318387311279773566894876369811406237208448656803682244786284714051489351162528336310611509464319257937304463246532853715133677606472539203807274116025362212599140301963520366587373345549232664, 57789326082328055343244183952945963065038001008054371988423611501774965584951836084636408462704147650194354237953178434916234787575914934345509256412623906774868805317306029570637051066904576874737549979887847806955662502738986630454336931791238784800404714101190106940899556139200869987149012959023189160975), 601201094732353224640534306619895007746271908982
p, q, g, y = klucz_publiczny_DSA
x = klucz_prywatny_DSA
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
conn.send(b"klucz publiczny")
conn.send(p.bit_length().to_bytes(1024, byteorder))
conn.send(q.bit_length().to_bytes(1024, byteorder))
conn.send(g.bit_length().to_bytes(1024, byteorder))
conn.send(y.bit_length().to_bytes(1024, byteorder))
conn.sendall(p.to_bytes(p.bit_length()+7//8, byteorder))
conn.sendall(q.to_bytes(q.bit_length()+7//8, byteorder))
conn.sendall(g.to_bytes(g.bit_length()+7//8, byteorder))
conn.sendall(y.to_bytes(y.bit_length()+7//8, byteorder))
with conn:
print(f"Connected by {addr}")
while True:
do_podpisania = conn.recv(1024)
if not do_podpisania:
break
m = do_podpisania
r, s = sig_DSA(p, q, g, x, m)
conn.send(r.bit_length().to_bytes(1024, byteorder))
conn.send(s.bit_length().to_bytes(1024, byteorder))
conn.sendall(r.to_bytes(r.bit_length() + 7 // 8, byteorder))
conn.sendall(s.to_bytes(s.bit_length() + 7 // 8, byteorder))