-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
69 lines (44 loc) · 1.78 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
57
58
59
60
61
62
63
64
65
66
67
68
69
import socket
import RSA as RSA
# create a socket object
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to a public host, and a well-known port
server_socket.bind(('localhost', 8000))
# set the socket to listen for incoming connections
server_socket.listen()
print("Server is listening for incoming connections...")
# accept the client connection
client_socket, address = server_socket.accept()
print(f"Connection established from {address}")
# receive data from the client
public_B = (int(client_socket.recv(1024).decode()),int(client_socket.recv(1024).decode()))
print(f"Received pub-key from client: {public_B}")
# send server public key
public_A,private_A = RSA.generate_keys(64)
client_socket.send(str(public_A[0]).encode()) # send n
client_socket.send(str(public_A[1]).encode()) # send e
# handshake is done now get the message and encrypt it
while True:
# get the message
message=input("please write a message to send : ")
encoded_messages=RSA.en.encode(message)
# send number of groups
client_socket.send(str(len(encoded_messages)).encode())
# send message
for encoded in encoded_messages:
enc = RSA.encrypt(int(encoded),public_B)
# send a response to the client
client_socket.send(str(enc).encode())
# recieve number of groups
n_blocks=int(client_socket.recv(1024).decode())
# recieve data
de=[]
for i in range(n_blocks):
encoded = int(client_socket.recv(1024).decode())
de.append(RSA.decrypt(encoded, private_A))
# messageReceived += RSA.en.decode(RSA.decrypt(encoded, private_B))
messageReceived = RSA.en.decode(de)
print(f"Received message from client : {messageReceived}")
# close the connection
client_socket.close()
server_socket.close()