-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBob.py
64 lines (50 loc) · 2.18 KB
/
Bob.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
# Hello, This is BOB.
# For Demonstraion Porpose Im The Server Connecting Alice & Me(Bob)
# This Connection Taking Place Btwn Alice & Me(Bob) Simulates
# Interconnection Through Optical FIber And Like Wise
import socket
import numpy as np
def eavesdrop_and_measure(alice_bits, alice_bases, num_bits, eavesdropping_probability):
eaves_bits = np.zeros(num_bits, dtype=int)
eaves_bases = np.random.randint(0, 2, num_bits)
intercepted = (np.random.rand(num_bits) < eavesdropping_probability)
for i in range(num_bits):
if intercepted[i]:
if eaves_bases[i] == alice_bases[i]:
eaves_bits[i] = alice_bits[i]
else:
eaves_bits[i] = np.random.randint(0, 2)
return eaves_bits, eaves_bases, intercepted
def start_server():
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 65432))
server_socket.listen(1)
print('Server is waiting for connection...')
conn, addr = server_socket.accept()
print('Connected by', addr)
num_bits = 64
eavesdropping_probability = 0.7
alice_bits = np.random.randint(0, 2, num_bits, dtype=np.uint8)
alice_bases = np.random.randint(0, 2, num_bits, dtype=np.uint8)
eaves_bits, eaves_bases, intercepted = eavesdrop_and_measure(alice_bits, alice_bases, num_bits, eavesdropping_probability)
# Send the number of bits first
conn.sendall(num_bits.to_bytes(4, byteorder='big'))
# Send Alice's bits and bases to the client (Bob)
conn.sendall(alice_bits.tobytes())
conn.sendall(alice_bases.tobytes())
while True:
message = conn.recv(1024).decode('utf-8')
if message.lower() == 'quit':
print("Alice has ended the connection.")
break
print("Alice: ", message)
response = input("Enter a message to send to Alice (or type 'quit' to end): ")
if response.lower() == 'quit':
conn.sendall(response.encode('utf-8'))
break
conn.sendall(response.encode('utf-8'))
# Close the connection
conn.close()
server_socket.close()
if __name__ == "__main__":
start_server()