-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpsi_de_christofaro_srv.py
More file actions
executable file
·94 lines (77 loc) · 2.22 KB
/
Copy pathpsi_de_christofaro_srv.py
File metadata and controls
executable file
·94 lines (77 loc) · 2.22 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
#!/usr/bin/python
###SERVER###
import random
from time import time
import gmpy
import hashlib
from flask import Flask
from flask import request
e=2**16+1
app = Flask(__name__)
def hash_int(x):
return int(hashlib.sha256(str(x)).hexdigest(),16)
#generates a random prime of the requested bit length
def gen_prime(BIT_LEN):
found=False
while found==False:
p=random.randrange(2**(BIT_LEN-1),2**BIT_LEN)
found=gmpy.is_prime(p)
return p
#generates a random safe prime of the requested bit length
def gen_safe_prime(BIT_LEN):
found=False
while found==False:
q=random.randrange(2**(BIT_LEN-2),2**(BIT_LEN-1))
p=2*q+1
found=gmpy.is_prime(p) and gmpy.is_prime(q)
return p
def RSA_GEN(BIT_LEN):
# this is faster but not so secure
p=gen_prime(BIT_LEN)
q=gen_prime(BIT_LEN)
# p=gen_safe_prime(BIT_LEN)
# q=gen_safe_prime(BIT_LEN)
n=p*q
f=(p-1)*(q-1)
e=2**16+1
d=gmpy.invert(e,f)
return d,n
def RSA_ENC(m,n):
return pow(m,e,n)
def RSA_DEC(c,d,n):
return pow(c,d,n)
@app.route('/', methods=['GET', 'POST'])
def hello_world():
if request.method == 'POST':
vals = request.form['params']
print "Client values received"
client_elements=vals.split(",")
for i in range(len(client_elements)):
client_elements[i]=int(client_elements[i])
m_B1=[RSA_DEC(x,d,n) for x in client_elements]
hs=[hash_int(i) for i in server_elements]
m_B2=[hash_int(RSA_DEC(y,d,n)) for y in hs]
print "sending response..."
client_enc=""
for cc in m_B1:
client_enc+=str(cc)+","
client_enc=client_enc[:-1]
srv_enc=""
for cc in m_B2:
srv_enc+=str(cc)+","
srv_enc=srv_enc[:-1]
resp= client_enc+"|"+srv_enc
return str(resp)
if __name__ == '__main__':
#generate RSA key pair (public,private) for the server
d,n=RSA_GEN(512)
fout=open("srv.key","w")
fout.write(str(n))
fout.close()
fin=open("srv_elements.txt","r")
srv_vals=fin.read()
fin.close()
server_elements=srv_vals.split(",")
for i in range(len(server_elements)):
server_elements[i]=int(server_elements[i])
app.run()