-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
116 lines (92 loc) · 3.07 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import socket
import threading
import time
PORT = 5050
FORMAT = "utf-8"
END_CONVERSATION = "DES"
HEADER = 64
SERVER = socket.gethostbyname(socket.gethostname())
# print(SERVER)
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
plain_text = ''
public_key = 0
n = 0
encrypted_timers = []
mapping = {str(i): i for i in range(10)}
mapping.update({chr(i + 97): i + 10 for i in range(26)})
mapping[' '] = 36
def encode(arrString):
encoded_text = []
for string in arrString:
sum = 0
for i in range(0, 5):
if string[i].lower() in mapping:
sum += int(mapping[string[i].lower()] * (37**(5 - i - 1)))
else:
sum += int(mapping[' '] * (37**(5 - i - 1)))
encoded_text.append(int(sum))
return encoded_text
#!#########################################################################################
def convert_string_to_blocks(plain_text):
size = len(plain_text)
if (size % 5 != 0):
plain_text += " " * (5 - (size % 5))
plain_text += "endom"
size = len(plain_text)
return [plain_text[i:i+5] for i in range(0, size, 5)]
#!#########################################################################################
def ciphering(encoded_msg, n, e):
cipher_text = []
for num in encoded_msg:
cipher_text.append(pow(num, e, n))
return cipher_text
#!#########################################################################################
def send_to_server(msg):
converted_msg = convert_string_to_blocks(msg)
encoded_msg = encode(converted_msg)
print(f"encoded message: %s" % encoded_msg)
cipher_text = ciphering(encoded_msg, n, public_key)
for char in cipher_text:
MSG = str(char).encode(FORMAT)
msg_len = len(MSG)
send_len = str(msg_len).encode(FORMAT)
send_len += b' '*(HEADER - len(send_len))
client.send(send_len)
client.send(MSG)
#!#########################################################################################
def read():
try:
msg_len = len(client.recv(HEADER).decode(FORMAT))
if msg_len:
msg_len = int(msg_len)
msg = client.recv(msg_len).decode(FORMAT)
except Exception as e:
print(f"Error: {e}")
msg_len = len(client.recv(HEADER).decode(FORMAT))
if msg_len:
msg_len = int(msg_len)
msg = client.recv(msg_len).decode(FORMAT)
public_key = int(msg)
print("Public key => ", public_key, flush=True)
msg_len = len(client.recv(HEADER).decode(FORMAT))
if msg_len:
msg_len = int(msg_len)
msg = client.recv(msg_len).decode(FORMAT)
n = int(msg)
print("N => ", n, flush=True)
while True:
plain_text = input("Enter a plain text: ")
if plain_text != END_CONVERSATION:
# * calc elapsed time
start = time.time()
send_to_server(plain_text)
end = time.time()
encrypted_timers.append(end-start)
else:
break
thread = threading.Thread(target=read)
thread.start()
#! to end conversation with server
send_to_server(END_CONVERSATION)