forked from AhmadJamal01/cipheRSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
141 lines (127 loc) · 4.3 KB
/
app.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from flask import Flask , request, jsonify ,session
from helpers import *
from flask_socketio import SocketIO, emit , join_room
from collections import deque
import json
from ast import literal_eval
channelsCreated = []
# Keep track of users logged (Check for username)
usersLogged = []
# Instanciate a dict
channelsMessages = dict()
NUMOFBITS = 128
app = Flask(__name__)
socketio = SocketIO(app,cors_allowed_origins="*",async_mode='eventlet')
app.config["SECRET_KEY"] = "my secret key"
@app.route('/signup' , methods=['POST'])
def signup():
data = request.get_json()
print(data)
p = data['p']
q = data['q']
e = data['e']
errorMsg = ""
if p != "":
p = int(data['p'])
else:
p= generatePrime(NUMOFBITS )
if q != "":
q = int(data['q'])
else:
q = generatePrime(NUMOFBITS )
while p==q:
q = generatePrime(NUMOFBITS )
if e != "":
e = int(data['e'])
else:
e = random.randrange(1,(p-1)*(q-1))
while not testexponent(e,p,q):
e = random.randrange(1,(p-1)*(q-1))
username = data['username']
d= 0
ptest=fermatPrimalityTest(p)
qtest=fermatPrimalityTest(q)
etest=testexponent(e,p,q)
d = getPrivateKey(e,p,q)
if not etest :
errorMsg += "e is not valid"
if not ptest :
errorMsg = "p is not prime "
if not qtest :
errorMsg += "q is not prime "
if etest and ptest and qtest :
user_data = {
"username":username,
"n" : p*q,
"e" : e,
}
json_object = json.dumps(user_data, indent = 4)
if username == "jemy" :
with open("jemy.json", "w") as outfile:
outfile.write(json_object)
else:
with open("ihab.json", "w") as outfile:
outfile.write(json_object)
print("d" + str(d))
return jsonify({'Error':errorMsg , 'privateKey':str(d) , 'publicKey':str(e) , 'n' : str(p*q) , 'p' : str(p) , 'q' : str(q)})
@app.route("/obtainpublic", methods=['GET'])
def obtainpublic():
data = request.get_json()
if data['username'] == "jemy" :
with open('ihab.json', 'r') as openfile:
# Reading from json file
json_object = json.load(openfile)
else:
with open('jemy.json', 'r') as openfile:
# Reading from json file
json_object = json.load(openfile)
return jsonify(json_object)
@app.route("/sendmsg", methods=['POST'])
def sendmsg():
data = request.get_json()
if (data["username"]=="jemy"):
with open('ihab.json', 'r') as openfile:
# Reading from json file
json_object = json.load(openfile)
n = json_object["n"]
m_encrypted=use_encrypt(data["message"],json_object["e"],n)
f = open("ihabroomview.txt", "a")
f.write(data["username"]+" "+str(m_encrypted) + "\n")
f2 = open("jemyroomview.txt", "a")
f2.write(data["username"]+" "+str(data["message"]) + "\n")
else:
with open('jemy.json', 'r') as openfile:
# Reading from json file
json_object = json.load(openfile)
n = json_object["n"]
m_encrypted=use_encrypt(data["message"],json_object["e"],n)
f = open("jemyroomview.txt", "a")
f.write(data["username"]+" "+str(m_encrypted) + "\n")
f2 = open("ihabroomview.txt", "a")
f2.write(data["username"]+" "+str(data["message"]) + "\n")
f = open("chatroom.txt", "a")
f.write(data["username"]+" "+str(data["message"])+" "+str(m_encrypted) + "\n")
return jsonify({'Error':"",'message':data["message"]})
@app.route('/decrypt', methods=['POST'])
def decrypt():
data = request.get_json()
d = int(data["d"])
p = int(data["p"])
q = int(data["q"])
m = use_decrypt(literal_eval(data["message"]),d,p,q)
return jsonify({'Error':"",'message':m})
@socketio.on('jemysevent')
def send_msg():
with open('jemyroomview.txt', 'r') as openfile:
lines = openfile.read().splitlines()
emit('jemymessage', {
'yourmsgs': lines
}, broadcast=True)
@socketio.on('ihabsevent')
def send_msg():
with open('ihabroomview.txt', 'r') as openfile:
lines = openfile.read().splitlines()
emit('ihabmessage', {
'yourmsgs': lines}, broadcast=True)
if __name__ == "__main__":
socketio.run(app)