-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
56 lines (41 loc) · 1.54 KB
/
server.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
import socket
from cryptography.fernet import Fernet
from base64 import urlsafe_b64encode
from util import generate_key, format_key
SERVER_HOST = '127.0.0.1'
SERVER_PORT = 5500
def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((SERVER_HOST, SERVER_PORT))
sock.listen(5)
print(f'Running in {SERVER_HOST}:{SERVER_PORT}\n')
print('- Wait connection...')
client, __ = sock.accept()
print('- Connected, performing key exchange...')
# keys received and generated
common = int(client.recv(1024).decode())
secret = generate_key()
public = common + secret
# sending "public" key and waiting for peer's public key
client.send(str(public).encode())
public_peer = int(client.recv(1024).decode())
message_secret = public_peer + secret
print(f'- Encrypt key is: {format_key(message_secret)}\n')
key_bytes = message_secret.to_bytes(32, byteorder='little')
fernet = Fernet(urlsafe_b64encode(key_bytes))
print('=' * 20)
try:
while True:
# wait response
client_msg = client.recv(1024)
client_msg_decrypted = fernet.decrypt(client_msg)
print(f'client> {client_msg_decrypted.decode()}')
message = input('message> ').strip()
encrypted_msg = fernet.encrypt(message.encode())
client.send(encrypted_msg)
print('...')
except KeyboardInterrupt:
print('Bye.')
sock.close()
main()