-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.py
More file actions
89 lines (68 loc) · 3.13 KB
/
Server.py
File metadata and controls
89 lines (68 loc) · 3.13 KB
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
from socket import *
from Crypto.Cipher import AES
import rsa
# Creating server RSA keys (private and public)
(publicRSAKey, privateRSAKey) = rsa.newkeys(1024)
# Create socket using the given port
serverPort = 13000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('', serverPort))
serverSocket.listen(1)
print ('Listening on port', serverPort, '...')
# While loop to handle arbitrary sequence of clients making requests
while 1:
# Waits for some client to connect and creates new socket for connection
connectionSocket, addr = serverSocket.accept()
print("\n*******************************")
print ('Client Made Connection', addr )
# Read input line from socket
clientSentence = connectionSocket.recv(1024)
# If is a newConnection send the server public RSA key
if clientSentence.decode('utf-8') == 'newConnection':
print('Sending RSA public KEY')
print(publicRSAKey.save_pkcs1('PEM').decode('utf-8'),"\n")
connectionSocket.send(publicRSAKey.save_pkcs1('PEM'))
else:
connectionSocket.close()
# Wait client AES Key and decrypting it with server RSA private key
clientAESKey = connectionSocket.recv(1024)
clientAESKey = rsa.decrypt(clientAESKey, privateRSAKey)
print('Received AES client KEY encrypted with RSA')
# Wait client AES Nonce and decrypting it with server RSA private key
clientAESNonce = connectionSocket.recv(1024)
clientAESNonce = rsa.decrypt(clientAESNonce, privateRSAKey)
print('Received AES client NONCE encrypted with RSA')
# Mount AES client key
clientAESCipher = AES.new(clientAESKey, AES.MODE_SIV, nonce=clientAESNonce)
# Wait client Message Tag
clientMessageTag = connectionSocket.recv(1024)
print('Received AES client message TAG')
# Wait client Message
clientMessage = connectionSocket.recv(1024)
print('Received AES client message',"\n")
# Decrypt client Message with Client AES key
message = clientAESCipher.decrypt_and_verify(clientMessage, clientMessageTag)
message = message.decode('utf-8')
# Verify message authenticity
try:
clientAESCipher.verify(clientMessageTag)
print("The message is authentic:", message)
# Capitalize the sentence
response = message.upper()
print("\nAlterating client message to UpperCase:", response, "\n")
# Encrypt response with client AES key
clientAESCipher = AES.new(clientAESKey, AES.MODE_SIV, nonce=clientAESNonce)
cipherResponse, responseTag = clientAESCipher.encrypt_and_digest(response.encode('utf-8'))
# Send the response tag crypted with client AES key to client
print('Sending AES response TAG from new message to client')
print(responseTag,"\n")
connectionSocket.send(responseTag)
# Send the response crypted with client AES key to client
print('Sending AES response to client')
print(cipherResponse,"\n")
connectionSocket.send(cipherResponse)
except ValueError:
print("Key incorrect or message corrupted")
print("*******************************")
# Close the connection socket
connectionSocket.close()