-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
104 lines (81 loc) · 3.69 KB
/
server.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
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
import socket
import numpy as np
import os
import sys
import datetime
parent_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(parent_dir, '..'))
import rlwe_he_scheme_updated as rlwe_updated
def decrypt_ciphertexts(ciphertexts, sk, n, q, t, poly_mod):
decrypted_binary_vectors = []
for ct in ciphertexts:
ct_combined = np.concatenate((ct[0], ct[1]))
decrypted_value = rlwe_updated.decrypt(
sk, n, q, t, poly_mod, ct_combined)
decrypted_binary_vectors.append(decrypted_value)
return ''.join(
[
chr(int(''.join(map(str, binary_vector)), 2))
for binary_vector in decrypted_binary_vectors
]
)
if __name__ == "__main__":
host = 'localhost'
port = 12345
n = 2 ** 3
q = 2 ** 14
t = 2
poly_mod = [1] + [0] * (n - 1) + [1]
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((host, port))
server_socket.listen()
print("Server is listening for incoming connections...")
client_socket, _ = server_socket.accept()
# Receive the secret key from the client
sk_bytes = client_socket.recv(4096)
sk = np.frombuffer(sk_bytes, dtype=np.int64)
received_data = b''
while True:
if data := client_socket.recv(4096):
received_data += data
else:
break
print("Received data")
ciphertexts = []
chunk_size = n * 8 * 2 # 8 * 8 * 2 = 128 bytes
for i in range(0, len(received_data), chunk_size):
ct_part1 = np.frombuffer(received_data[i:i + n * 8], dtype=np.int64)
ct_part2 = np.frombuffer(
received_data[i + n * 8:i + 2 * n * 8], dtype=np.int64)
ciphertexts.append((ct_part1, ct_part2))
print(ciphertexts)
decrypted_binary_vectors = []
for ct in ciphertexts:
decrypted_value = rlwe_updated.decrypt(sk, n, q, t, poly_mod, ct)
decrypted_binary_vectors.append(decrypted_value)
print(decrypted_binary_vectors)
decrypted_text = ''.join([chr(int(''.join(map(str, binary_vector)), 2))
for binary_vector in decrypted_binary_vectors])
print(f"Decrypted text: {decrypted_text}")
print(f"Character count: {len(decrypted_text)}")
server_socket.close()
now = datetime.datetime.now()
os.chdir(os.path.dirname(__file__))
os.makedirs("log", exist_ok=True)
os.chdir("log")
with open("output.log", "a", encoding="utf-8") as log_file:
log_file.write(
f"{now.year}-{now.month}-{now.day} {now.hour}:{now.minute}:{now.second}.{now.microsecond} [SERVER] Received data: {received_data}\n")
ciphertexts_str = "["
for ct in ciphertexts:
ct_str = "(array(" + ', '.join(map(str, ct[0])) + "), array(" + ', '.join(map(str, ct[1])) + "))"
ciphertexts_str += f"{ct_str}, "
ciphertexts_str = ciphertexts_str.rstrip(", ") + "]"
log_file.write(
f"{now.year}-{now.month}-{now.day} {now.hour}:{now.minute}:{now.second}.{now.microsecond} [SERVER] Ciphertexts: {ciphertexts_str}\n")
log_file.write(
f"{now.year}-{now.month}-{now.day} {now.hour}:{now.minute}:{now.second}.{now.microsecond} [SERVER] Decrypted binary vectors: {decrypted_binary_vectors}\n")
log_file.write(
f"{now.year}-{now.month}-{now.day} {now.hour}:{now.minute}:{now.second}.{now.microsecond} [SERVER] Decrypted text: {decrypted_text}\n")
log_file.write(
f"{now.year}-{now.month}-{now.day} {now.hour}:{now.minute}:{now.second}.{now.microsecond} [SERVER] Character count: {len(decrypted_text)}\n")