-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.py
59 lines (39 loc) · 1.71 KB
/
helper.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
import encryption
import socket
HEADER_LENGTH = 10
def send_message(message, key):
en_message = encryption.sym_encrypt(message.encode(), key)
return f"{len(en_message):<{HEADER_LENGTH}}".encode() + en_message # in bytes form
def receive_message(target_socket, key):
try:
message_header = target_socket.recv(HEADER_LENGTH)
if not len(message_header):
return False
message_length = int(message_header.decode())
p = target_socket.recv(message_length)
de_message = encryption.sym_decrypt(p, key)
return de_message.decode() # return a message in string form
except:
print("Enable to receive")
return False
def encryption_gen_sym(client_socket): # client
asy_key = encryption.asy_key_gen() # generate asymmetric key
client_public_key = asy_key.public_key()
# receive server's public key
server_public_key = encryption.bytes_to_key(client_socket.recv(2048))
# generate common symmetric key
sym_key = encryption.sym_key_gen()
# encrypt symmetric key with server's public key
encrypted_sym_key = encryption.asy_encrypt(sym_key, server_public_key)
client_socket.send(encrypted_sym_key)
return sym_key
def encryption_recv_sym(server_socket): # server
asy_key = encryption.asy_key_gen() # generate asymmetric key
server_public_key = asy_key.public_key()
# send server's public key
server_socket.send(encryption.key_to_bytes(server_public_key))
# receive common symmetric key
encrypted_sym_key = server_socket.recv(2048)
# decrypt to get symmetric key using server's public key
sym_key = encryption.asy_decrypt(encrypted_sym_key, asy_key)
return sym_key