-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
103 lines (82 loc) · 3.27 KB
/
Copy pathserver.py
File metadata and controls
103 lines (82 loc) · 3.27 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# server.py (Alice)
import socket
import pickle
from crypto_participant import CryptoParticipant
def start_server():
print("\n" + "="*50)
print("Starting Alice's Server")
print("="*50)
# Initialize Alice
alice = CryptoParticipant("Alice")
# Set up server socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('localhost', 5000))
server.listen(1)
print("\nWaiting for Bob to connect...")
client_socket, address = server.accept()
print(f"\nConnection established with Bob at {address}")
try:
print("\n" + "="*50)
print("Starting Key Exchange Protocol")
print("="*50)
# Exchange RSA keys
public_key = {'e': alice.e, 'n': alice.n}
client_socket.send(pickle.dumps(public_key))
print("\nSent Alice's RSA public key to Bob")
bob_public = pickle.loads(client_socket.recv(4096))
print(f"\nReceived Bob's RSA public key:")
print(f"e: {bob_public['e']}")
print(f"n: {bob_public['n']}")
# Send DH parameters
dh_params = {'base': 5, 'modulus': 2357}
client_socket.send(pickle.dumps(dh_params))
print("\nSent DH parameters to Bob")
# Setup DH
alice.set_dh_parameters(dh_params['base'], dh_params['modulus'])
alice.generate_dh_private()
alice_public_dh = alice.calculate_dh_public()
# Exchange encrypted DH values
print("\n" + "="*50)
print("Exchanging Encrypted DH Values")
print("="*50)
alice_encrypted = alice.rsa_encrypt(alice_public_dh, bob_public['e'], bob_public['n'])
client_socket.send(pickle.dumps(alice_encrypted))
print("\nSent encrypted DH public value to Bob")
bob_encrypted = pickle.loads(client_socket.recv(4096))
print("\nReceived Bob's encrypted DH public value")
bob_public_dh = alice.rsa_decrypt(bob_encrypted)
# Calculate final keys
print("\n" + "="*50)
print("Calculating Final Keys")
print("="*50)
shared_secret = alice.calculate_shared_secret(bob_public_dh)
alice_aes = alice.derive_aes_key(shared_secret)
print("\n" + "="*50)
print("Secure Channel Established")
print("="*50)
print("\nYou can now start chatting with Bob")
print("Type 'quit' to exit\n")
while True:
# Receive from Bob
print("\nWaiting for Bob's message...")
encrypted_message = pickle.loads(client_socket.recv(4096))
if not encrypted_message:
break
# Decrypt and show all steps
message = alice.aes_decrypt(encrypted_message)
print(f"\nDecrypted message from Bob: {message}")
# Get Alice's response
response = input("\nAlice: ")
if response.lower() == 'quit':
break
# Encrypt and send
encrypted_response = alice.aes_encrypt(response)
client_socket.send(pickle.dumps(encrypted_response))
except Exception as e:
print(f"\nError occurred: {e}")
finally:
client_socket.close()
server.close()
print("\nServer shut down")
if __name__ == "__main__":
start_server()