-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
67 lines (52 loc) · 1.52 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import socket
from dh import diffie_hellman
IP = "127.0.0.1"
SERVER_PORT = 8083
CLIENT_PORT = 8084
CACHE_PORT = 8086
PACKET_SIZE = 64
# Setup client connection to server.
sock_out = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock_in = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock_in.bind((IP, CLIENT_PORT))
def send(data, port):
packets = ["%s"%data[i:i+PACKET_SIZE] for i in range(0,len(data),PACKET_SIZE)]
for p in packets:
sock_out.sendto(p, (IP, port))
sock_out.sendto("EOF", (IP, port))
def receive():
data = ""
while True:
msg, addr = sock_in.recvfrom(PACKET_SIZE) # buffer size is 64 bytes
if (msg=="EOF"):
break
else:
data += msg
return data
#Perform DH and send public values to server.
dh = diffie_hellman()
key = dh.generate_public_key()
signed = dh.sign_message(key, "certs/client.key")
data = signed + key
send(data, SERVER_PORT)
# Receive public values from server, create shared secret + Fernet.
data=receive()
signature = data[0:256]
data = data[256:];
verify = dh.verify_message(data, signature, "certs/server.key") # client
if verify == 1:
f = dh.generate_symetric_encryption_key(data)
# Encrypt a message using Fernet, send to server.
token = f.encrypt(b"Super secret hax")
send(token, CACHE_PORT)
send("Server", CACHE_PORT)
print("Secret msg sent!")
send("Close",CACHE_PORT)
else:
print "Invalid signature from server"
send("Close",CACHE_PORT)
# Close connections.
sock_out.close()
sock_in.close()