-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
48 lines (37 loc) · 1.3 KB
/
server.py
File metadata and controls
48 lines (37 loc) · 1.3 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
from model import Linmodel
from phe import paillier
import json
def clientdata():
with open('encrypted_data.txt', 'r') as file:
encrypted_data_str = json.load(file)
data = json.loads(encrypted_data_str)
return data
def computedata():
data = clientdata()
coef_we = Linmodel().getCoef()
pub_key = data['public_key']
public_key = paillier.PaillierPublicKey(n=int(pub_key['n']))
enc_nums_rec = []
for x in data['values']:
value1 = int(x[0])
value2 = int(x[1])
encrypted_num = paillier.EncryptedNumber(public_key, value1, value2)
enc_nums_rec.append(encrypted_num)
results = 0 # Initialize results to zero
for i in range(len(coef_we)):
results += coef_we[i] * enc_nums_rec[i]
return results, public_key
def serializedata():
results, public_key = computedata()
serialized_results = {}
serialized_results['public_key'] = {'n': public_key.n}
serialized_results['values'] = [(str(results.ciphertext()), results.exponent)]
serialdata = json.dumps(serialized_results)
print('This is the result: ', serialdata)
return serialdata
def main():
result = serializedata()
with open('results.txt', 'w') as file:
json.dump(result, file)
if __name__ == '__main__':
main()