-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
80 lines (61 loc) · 2.51 KB
/
Client.py
File metadata and controls
80 lines (61 loc) · 2.51 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
from socket import *
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import rsa
serverName = "localhost"
serverPort = 13000
# Create socket and connect to server
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName, serverPort))
# Send new Connection to server looking for the server public RSA KEY
print('Sending newConnection to server looking for server public RSA KEY')
clientSocket.send('newConnection'.encode('utf-8'))
# Read line from server
publicRSAKeyServer = clientSocket.recv(1024)
# Transform response into UTF-8
publicRSAKeyServer = publicRSAKeyServer.decode('utf-8')
print('Received Public RSA server KEY message TAG\n')
# Recreating RSA server key to use it as a RSA key
publicRSAKeyServer = rsa.PublicKey.load_pkcs1(publicRSAKeyServer, 'PEM')
# Creating Client AES key
AESkey = get_random_bytes(16*2)
AESnonce = get_random_bytes(16)
AEScipher = AES.new(AESkey, AES.MODE_SIV, nonce=AESnonce)
# Send client AES key crypted with server RSA public key to server
print('Sending AES key encrypted with public RSA')
print(rsa.encrypt(AESkey, publicRSAKeyServer),"\n")
clientSocket.send(rsa.encrypt(AESkey, publicRSAKeyServer))
# Send client AES nonce crypted with server RSA public key to server
print('Sending AES nonce encrypted with public RSA')
print(rsa.encrypt(AESnonce, publicRSAKeyServer),"\n")
clientSocket.send(rsa.encrypt(AESnonce, publicRSAKeyServer))
# Get message to send
message = input('Client ready for input\n')
# Encrypt message with client AES key after encode with utf-8
cipherMessage, messageTag = AEScipher.encrypt_and_digest(message.encode('utf-8'))
# Send client AES message tag to server
print('\nSending AES message TAG')
print(messageTag,"\n")
clientSocket.send(messageTag)
# Send client AES message to server
print('Sending message encrypted with AES')
print(cipherMessage,"\n")
clientSocket.send(cipherMessage)
# Read line from server
responseTag = clientSocket.recv(1024)
print('Received AES server response TAG')
# Read line from server
responseMessage = clientSocket.recv(1024)
print('Received AES server response')
# Decrypt server Response with Client RSA key
AEScipher = AES.new(AESkey, AES.MODE_SIV, nonce=AESnonce)
response = AEScipher.decrypt_and_verify(responseMessage, responseTag)
response = response.decode('utf-8')
# Verify message authenticity
try:
AEScipher.verify(responseTag)
print("The response is authentic:", response)
except ValueError:
print("Key incorrect or message corrupted")
# Close the connection socket
clientSocket.close()