-
Notifications
You must be signed in to change notification settings - Fork 1
/
servercalc.py
47 lines (40 loc) · 1.2 KB
/
servercalc.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
from linmodel import LinModel
import phe as paillier
import json
import os
def getData():
with open('data.json', 'r') as file:
d=json.load(file)
data=json.loads(d)
return data
#getData()
def computeData():
data=getData()
mycoef=LinModel().getCoef()
pk=data['public_key']
pubkey= paillier.PaillierPublicKey(n=int(pk['n']))
enc_nums_rec = [paillier.EncryptedNumber(pubkey, int(x[0], int(x[1]))) for x in data['values']]
results=sum([mycoef[i]*enc_nums_rec[i] for i in range(len(mycoef))])
return results, pubkey
#print(computeData())
def serializeData():
results, pubkey = computeData()
encrypted_data={}
encrypted_data['pubkey'] = {'n': pubkey.n}
encrypted_data['values'] = (str(results.ciphertext()), results.exponent)
global serialized
serialized = json.dumps(encrypted_data)
return serialized
#print(serializeData())
#data = age, he, al, gen =[24, 4, 6, 1]
#mycoef = LinModel().getCoef()
#print(sum([data[i]*mycoef[i] for i in range(len(data))]))
def main():
if os.path.exists('data.json'):
datafile=serializeData()
with open('answer.json', 'w') as file:
json.dump(datafile, file)
else:
print("Es sind noch keine Daten zur Auswertung verfügbar.")
if __name__=='__main__':
main()