-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
357 lines (238 loc) · 9.51 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from cryptography.fernet import Fernet
from flask import Flask, request, jsonify, render_template, redirect, url_for, send_file
import jwt
import math
import os
import pymongo
from string import ascii_letters, digits
import time
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.utils import secure_filename
mongo = pymongo.MongoClient('localhost', 27017)
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.path.join(os.getcwd(), 'uploads')
app.secret_key = 'secret_key'
if not 'fernet.key' in os.listdir(os.getcwd()):
fernet_key = Fernet.generate_key()
with open('fernet.key', 'wb') as mykey:
mykey.write(fernet_key)
else:
with open('fernet.key', 'rb') as mykey:
fernet_key = mykey.read()
fernet = Fernet(fernet_key)
ascii_table_string = list(ascii_letters + digits + ':$! ')
ascii_table_file = list(ascii_letters + digits)
def login_user(user):
return jwt.encode({**user, 'expiration': time.time()+86400}, app.secret_key, algorithm='HS256')
def verify_login(request):
if len(request.cookies) > 0:
token = list(request.cookies.keys())[0]
if token:
details = jwt.decode(token.split(':', 1)[-1], app.secret_key, algorithms=['HS256'])
if 'username' in details and mongo['local']['user'].find({'username': details['username']}) and details['expiration'] > time.time():
return True
return False
def get_user(request):
return jwt.decode(list(request.cookies.keys())[0].split(':', 1)[-1], app.secret_key, algorithms=['HS256'])['username']
def get_hash(request):
return jwt.decode(list(request.cookies.keys())[0].split(':', 1)[-1], app.secret_key, algorithms=['HS256'])['password']
def matrixize(data, matrix=[]):
if len(data) < 1:
return matrix
if len(data) == 1:
matrix.append([data])
return matrix
size = int(math.sqrt(len(data)))
matrix.append(list(data[:size**2]))
return matrixize(data[size**2:], matrix)
def obfuscate_string(data):
obfuscated_string = ''
counter = 0
while data and len(data) >= counter:
if len(data[counter]) > 0:
obfuscated_string = obfuscated_string + data[counter].pop(0)
if len(data[counter]) == 0:
data.pop(counter)
else:
counter += 1
if counter == len(data):
counter = 0
return obfuscated_string
def matrix_to_list(matrices):
return [elem for matrix in matrices for elem in matrix ]
def matrix_to_string(matrices):
return ''.join([elem for matrix in matrices for elem in matrix ])
def encrypt(source, token, key, source_type='text'):
data = source
if isinstance(data, str) and source_type == 'text':
data = matrixize(key+data, [])
data = obfuscate_string(data)
data = matrixize(data, [])
data = matrix_to_list(data)
for x in range(0, len(data)):
data[x] = ascii_table_string[(ascii_table_string.index(data[x]) + ascii_table_string.index(token[x%len(token)])) % len(ascii_table_string)]
with open('uploads/encrypted file.txt', 'w') as f:
f.write(matrix_to_string(data))
elif source_type == 'file':
with open(source, 'rb') as f:
data = f.read()
data = fernet.encrypt(data)
source_dir, file_name = os.path.split(source)
file_name = list(file_name)
for x in range(0, len(file_name)):
if file_name[x] in ascii_table_file and token[x%len(token)] in ascii_table_file:
file_name[x] = ascii_table_file[(ascii_table_file.index(file_name[x]) + ascii_table_file.index(token[x%len(token)])) % len(ascii_table_file)]
file_name = ''.join(file_name)
file_path = os.path.join(source_dir, file_name)
with open(file_path, 'wb') as f:
f.write(data)
return file_path
def decrypt(source, token, key, source_type='text'):
data = source
length = len(data)
arr = []
while math.sqrt(length) > 0:
arr.append(int(math.sqrt(length))**2)
length -= (int(math.sqrt(length))**2)
if isinstance(data, str) and source_type == 'text':
data = list(data)
for x in range(0, len(data)):
if ascii_table_string.index(token[x%len(token)]) - ascii_table_string.index(data[x]) > 0:
data[x] = ascii_table_string[len(ascii_table_string) - ascii_table_string.index(token[x%len(token)]) + ascii_table_string.index(data[x])]
else:
data[x] = ascii_table_string[ascii_table_string.index(data[x]) - ascii_table_string.index(token[x%len(token)])]
matrix = []
while sum(arr) > 0:
for x in range(len(arr)):
if len(matrix) <= x:
matrix.append([])
if arr[x] > 0:
matrix[x].append(data.pop(0))
arr[x] -= 1
data = matrix_to_list(matrix)
with open('uploads/decrypted file.txt', 'w') as f:
f.write(''.join(data)[len(key):])
elif source_type == 'file':
with open(source, 'rb') as f:
data = f.read()
data = fernet.decrypt(data)
source_dir, file_name = os.path.split(source)
file_name = list(file_name)
for x in range(0, len(file_name)):
if file_name[x] in ascii_table_file and token[x%len(token)] in ascii_table_file:
if ascii_table_file.index(token[x%len(token)]) - ascii_table_file.index(file_name[x]) > 0:
file_name[x] = ascii_table_file[len(ascii_table_file) - ascii_table_file.index(token[x%len(token)]) + ascii_table_file.index(file_name[x])]
else:
file_name[x] = ascii_table_file[ascii_table_file.index(file_name[x]) - ascii_table_file.index(token[x%len(token)])]
file_name = ''.join(file_name)
file_path = os.path.join(source_dir, file_name)
with open(file_path, 'wb') as f:
f.write(data)
return file_path
@app.route('/logout', methods=['GET', 'POST'])
def logout():
# logout_user()
return redirect(url_for('/'))
@app.route('/', methods=['GET'])
def index():
if verify_login(request):
return redirect(url_for('crypter'))
return render_template('index.html')
@app.route('/login', methods=['POST'])
def login():
if request.method == 'POST':
payload = request.form
if 'username' in payload and 'password' in payload:
if mongo['local']['user'].find_one({'username': payload['username']}) and check_password_hash(mongo['local']['user'].find_one({'username': payload['username']})['password'], payload['password']):
response = jsonify({
'status': 200,
'message': 'User logged in successfully',
'token': login_user({'username': payload['username'], 'password': generate_password_hash(payload['password'])})
})
response.status_code = 200
return response
response = jsonify({
'status': 401,
'message': 'Invalid username or password'
})
response.status_code = 200
return response
@app.route('/register', methods=['POST'])
def register():
if request.method == 'POST':
payload = request.form
if 'username' in payload and 'password' in payload:
if mongo['local']['user'].find_one({'username': payload['username']}):
response = jsonify({
'status': 406,
'message': 'A user with this username/e-mail already exists, please login'
})
response.status_code = 200
return response
mongo['local']['user'].insert_one({'username': payload['username'], 'password': generate_password_hash(payload['password'])})
response = jsonify({
'status': 200,
'message': 'User created successfully',
'token': login_user({'username': payload['username'], 'password': generate_password_hash(payload['password'])})
})
response.status_code = 200
return response
else:
response = jsonify({
'status': 403,
'message': 'Invalid details for user registation, please try again'
})
response.status_code = 200
return response
@app.route('/crypter', methods=['GET'])
def crypter():
if verify_login(request):
return render_template('crypter.html')
return redirect(url_for('/'))
@app.route('/crypt/text', methods=['POST'])
def crypt_text():
if verify_login(request):
if request.form['crypt_type'] == 'encrypt':
encrypt(request.form['text'], get_hash(request), request.form['key'])
return send_file('uploads/encrypted file.txt', as_attachment=True)
elif request.form['crypt_type'] == 'decrypt':
decrypt(request.form['text'], get_hash(request), request.form['key'])
return send_file('uploads/decrypted file.txt', as_attachment=True)
else:
response = jsonify({
'status': 401,
'message': f"Invalid option",
})
response.status_code = 401
return response
return redirect(url_for('/'))
@app.route('/crypt/file', methods=['POST'])
def crypt_file():
if verify_login(request):
mongo['local']['files'].insert_one({'user': get_user(request), 'filename': request.files['file'].filename, 'timestamp': time.time()})
f = request.files['file']
# Extracting uploaded file name
data_filename = secure_filename(f.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], data_filename)
f.save(file_path)
if request.form['crypt_type'] == 'encrypt':
return send_file(encrypt(file_path, get_hash(request), '', 'file'), as_attachment=True)
elif request.form['crypt_type'] == 'decrypt':
return send_file(decrypt(file_path, get_hash(request), '', 'file'), as_attachment=True)
else:
response = jsonify({
'status': 401,
'message': f"Invalid option",
})
response.status_code = 401
return response
return redirect(url_for('/'))
@app.errorhandler(404)
def not_found(error=None):
message = jsonify({
'status': 404,
'message': request.url + ' Not Found'
})
message.status_code = 404
return message
app.run(debug=True)