-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_a.py.py
More file actions
125 lines (104 loc) · 4.95 KB
/
Copy pathserver_a.py.py
File metadata and controls
125 lines (104 loc) · 4.95 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import socket
from des_cipher import DESCipher
from rsa_keys import RSAKeys
import time
class ServerA:
def __init__(self, host='0.0.0.0', port=5000):
self.host = host
self.port = port
self.rsa = RSAKeys()
self.des_cipher = None
self.des_key = None
self.server_socket = None
self.client_socket = None
def start(self):
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_socket.bind((self.host, self.port))
self.server_socket.listen(1)
print("=" * 60)
print(" SERVER A - SECURE COMMUNICATION SYSTEM")
print(" Hybrid Cryptography: RSA + DES")
print("=" * 60)
print(f"\n[*] Listening on {self.host}:{self.port}")
print("[*] Waiting for Server B to connect...\n")
self.client_socket, addr = self.server_socket.accept()
print(f"[+] Connection established with Server B: {addr}")
self.hybrid_key_exchange()
def hybrid_key_exchange(self):
"""HYBRID CRYPTOGRAPHY: RSA for key exchange, DES for data"""
print("\n" + "=" * 60)
print(" PHASE 1: HYBRID KEY EXCHANGE (RSA + DES)")
print("=" * 60)
# Step 1: Generate RSA key pair for Server A
print("\n[Step 1] Generating RSA Key Pair (1024-bit)...")
self.rsa.generate_keys(1024)
print(" ✓ RSA Public/Private keys generated for Server A")
time.sleep(0.5)
# Step 2: Receive Server B's RSA public key
print("\n[Step 2] Receiving Server B's RSA Public Key...")
b_pub_data = self.client_socket.recv(4096)
b_public_key = self.rsa.import_public_key(b_pub_data)
print(f" ✓ Received ({len(b_pub_data)} bytes)")
time.sleep(0.5)
# Step 3: Send Server A's RSA public key to B
print("\n[Step 3] Sending Server A's RSA Public Key to B...")
a_pub_data = self.rsa.export_public_key()
self.client_socket.send(a_pub_data)
print(f" ✓ Sent ({len(a_pub_data)} bytes)")
time.sleep(0.5)
# Step 4: Generate DES session key (symmetric)
print("\n[Step 4] Generating DES Session Key (64-bit)...")
self.des_cipher = DESCipher()
self.des_key = self.des_cipher.get_key()
print(f" ✓ DES Key generated: {self.des_key.hex()}")
time.sleep(0.5)
# Step 5: Encrypt DES key with B's RSA public key (HYBRID!)
print("\n[Step 5] ENCRYPTING DES Key with Server B's RSA Public Key...")
print(f" → This is the HYBRID step: RSA encrypts the DES key")
encrypted_des_key = self.rsa.encrypt_with_public_key(b_public_key, self.des_key)
print(f" ✓ Encrypted DES Key ({len(encrypted_des_key)} bytes): {encrypted_des_key.hex()[:50]}...")
time.sleep(0.5)
# Step 6: Send encrypted DES key to B
print("\n[Step 6] Sending Encrypted DES Key to Server B...")
self.client_socket.send(encrypted_des_key)
print(f" ✓ Sent encrypted DES key ({len(encrypted_des_key)} bytes)")
time.sleep(0.5)
print("\n" + "=" * 60)
print(" PHASE 2: SECURE DATA EXCHANGE (DES Encryption)")
print("=" * 60)
print(" ✓ All messages now encrypted with DES")
print(" ✓ DES key was securely exchanged using RSA")
print("\n Type 'quit' to end the session\n")
self.secure_chat()
def secure_chat(self):
"""Data encrypted with DES (symmetric), key exchanged via RSA (asymmetric)"""
while True:
msg = input("\n[Server A] Enter message: ")
if msg.lower() == 'quit':
break
# DES Encryption
print(f" → Plaintext: '{msg}'")
encrypted_msg = self.des_cipher.encrypt(msg)
print(f" → DES Encrypted ({len(encrypted_msg)} bytes): {encrypted_msg.hex()[:40]}...")
self.client_socket.send(encrypted_msg)
print(f" → Sent encrypted message to Server B")
# Receive encrypted response
encrypted_response = self.client_socket.recv(4096)
if not encrypted_response:
break
print(f" ← Received encrypted response ({len(encrypted_response)} bytes)")
# DES Decryption
response = self.des_cipher.decrypt(encrypted_response)
print(f" ← DES Decrypted: '{response}'")
self.cleanup()
def cleanup(self):
print("\n" + "=" * 60)
print(" Session Ended - Connection Closed")
print("=" * 60)
if self.client_socket:
self.client_socket.close()
if self.server_socket:
self.server_socket.close()
if __name__ == "__main__":
server = ServerA()
server.start()