-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
292 lines (236 loc) · 10.2 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
# import the necessary modules
from flask import Flask, render_template, request , session, redirect, url_for
from cryptography.fernet import Fernet
from Crypto.Cipher import DES3, AES,PKCS1_OAEP
from Crypto.Random import get_random_bytes
from Crypto.PublicKey import RSA
import os
import hashlib
import base64
# Initialize flask
app = Flask(__name__)
app.secret_key = 'secret_key'
app.config['UPLOAD_FOLDER'] = 'uploads/'
# User data (username and password hash) dictionary
users = {}
# Generate a new RSA key pair
rsa_key = RSA.generate(2048)
# Use the public key to create a new PKCS1_OAEP cipher object
cipher_rsa = PKCS1_OAEP.new(rsa_key.publickey())
# Generate a new encryption key
key = Fernet.generate_key()
cipher_suite = Fernet(key)
des_key = DES3.new('1234567890123456', DES3.MODE_EAX)
# Define the encryption key
aes_key_128 = b'Sixteen byte key'
aes_key_256 = get_random_bytes(32) # 32 bytes = 256 bits
cipher_rsa = PKCS1_OAEP.new(rsa_key.publickey())
ciphertext = cipher_rsa.encrypt(aes_key_256)
# Create upload folder if it does not exist
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
# Login required decorator
def login_required(func):
def wrapper(*args, **kwargs):
if 'username' not in session:
return redirect(url_for('login'))
return func(*args, **kwargs)
return wrapper
@app.route('/')
def home():
return render_template('index.html')
@app.route('/signup', methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
password_hash = hashlib.sha256(password.encode('utf-8')).hexdigest()
if username in users:
return render_template('signup.html', error='Username already taken')
users[username] = password_hash
session['username'] = username
return redirect(url_for('home'))
else:
return render_template('signup.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
password_hash = hashlib.sha256(password.encode('utf-8')).hexdigest()
if username not in users:
return render_template('login.html', error='Username not found')
if users[username] != password_hash:
return render_template('login.html', error='Incorrect password')
session['username'] = username
return redirect(url_for('home'))
else:
return render_template('login.html')
@app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('home'))
@app.route('/forgot_password', methods=['GET', 'POST'])
def forgot_password():
if request.method == 'POST':
username = request.form['username']
security_question = request.form['security_question']
security_answer = request.form['security_answer']
if username not in users:
return render_template('forgot_password.html', error='Username not found')
if security_question != 'What was the name of your first pet?' or security_answer != 'Fluffy':
return render_template('forgot_password.html', error='Incorrect security question or answer')
session['username'] = username
session['security_question'] = security_question
session['security_answer'] = security_answer
return redirect(url_for('reset_password'))
else:
return render_template('forgot_password.html')
@app.route('/reset_password', methods=['GET', 'POST'])
def reset_password():
if 'username' not in session or 'security_question' not in session or 'security_answer' not in session:
return redirect(url_for('forgot_password'))
if request.method == 'POST':
username = session['username']
new_password = request.form['new_password']
confirm_password = request.form['confirm_password']
if new_password != confirm_password:
return render_template('reset_password.html', error='Passwords do not match')
password_hash = hashlib.sha256(new_password.encode('utf-8')).hexdigest()
users[username] = password_hash
session.pop('username', None)
session.pop('security_question', None)
session.pop('security_answer', None)
return redirect(url_for('login'))
else:
username = session['username']
security_question = session['security_question']
security_answer = session['security_answer']
return render_template('reset_password.html', username=username, security_question=security_question, security_answer=security_answer)
@app.route('/encrypt', methods=['POST'])
def encrypt():
if 'username' not in session:
return redirect(url_for('login'))
f = request.files['file']
filename = f.filename
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
f.save(filepath)
algorithm = request.form['algorithm']
if algorithm == 'des':
# Encrypt the file using 3-DES
with open(filepath, 'rb') as file:
data = file.read()
encrypted_data = cipher_suite.encrypt(data)
with open(filepath, 'wb') as file:
file.write(encrypted_data)
elif algorithm == 'aes128':
# Encrypt the file using AES-128 in CBC mode
with open(filepath, 'rb') as file:
data = file.read()
iv = b"\0" * AES.block_size # Set the initialization vector to all zeros
cipher = AES.new(aes_key_128, AES.MODE_CBC, iv)
padded_data = data + b"\0" * (AES.block_size - len(data) % AES.block_size) # Padded to the block size
encrypted_data = cipher.encrypt(padded_data)
with open(filepath, 'wb') as file:
file.write(encrypted_data)
elif algorithm == 'aes256':
# Encrypt the file using AES-256 in CFB mode
with open(filepath, 'rb') as file:
data = file.read()
iv = b"\0" * AES.block_size # Set the initialization vector to all zeros
cipher = AES.new(aes_key_256, AES.MODE_CFB, iv)
encrypted_data = cipher.encrypt(data)
with open(filepath, 'wb') as file:
file.write(encrypted_data)
elif algorithm == 'rsa':
# Encrypt the file using RSA public key encryption
with open(filepath, 'rb') as file:
data = file.read()
cipher = PKCS1_OAEP.new(rsa_key.publickey())
encrypted_data = cipher.encrypt(data)
with open(filepath, 'wb') as file:
file.write(encrypted_data)
return 'File encrypted successfully!'
@app.route('/decrypt', methods=['POST'])
def decrypt():
if 'username' not in session:
return redirect(url_for('login'))
# Get the selected algorithm from the form
algorithm = request.form.get('algorithm')
# Get the uploaded file
f = request.files['file']
filename = f.filename
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
f.save(filepath)
# Decrypt the file based on the selected algorithm
if algorithm == 'des':
with open(filepath, 'rb') as file:
encrypted_data = file.read()
decrypted_data = cipher_suite.decrypt(encrypted_data)
with open(filepath, 'wb') as file:
file.write(decrypted_data)
elif algorithm == 'aes128':
with open(filepath, 'rb') as file:
data = file.read()
iv = b"\0" * AES.block_size # Set the initialization vector to all zeros
cipher = AES.new(aes_key_128, AES.MODE_CBC, iv)
decrypted_data = cipher.decrypt(data)
# Remove the padding
decrypted_data = decrypted_data.rstrip(b"\0")
with open(filepath, 'wb') as file:
file.write(decrypted_data)
elif algorithm == 'aes256':
with open(filepath, 'rb') as file:
data = file.read()
iv = b"\0" * AES.block_size # Set the initialization vector to all zeros
cipher = AES.new(aes_key_256, AES.MODE_CFB, iv)
decrypted_data = cipher.decrypt(data)
with open(filepath, 'wb') as file:
file.write(decrypted_data)
elif algorithm == 'rsa':
with open(filepath, 'rb') as file:
data = file.read()
cipher = PKCS1_OAEP.new(rsa_key)
decrypted_data = cipher.decrypt(data)
with open(filepath, 'wb') as file:
file.write(decrypted_data)
return 'File decrypted successfully!'
@app.route('/hash', methods=['POST'])
def hash():
if 'username' not in session:
return redirect(url_for('login'))
f = request.files['file']
filename = f.filename
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
f.save(filepath)
with open(filepath, 'rb') as file:
data = file.read()
hash_object = hashlib.sha256(data)
hex_dig = hash_object.hexdigest()
return f'The hash of {filename} is: {hex_dig}'
@app.route('/compare', methods=['POST'])
def compare():
if 'username' not in session:
return redirect(url_for('login'))
f1 = request.files['file1']
f2 = request.files['file2']
filename1 = f1.filename
filename2 = f2.filename
filepath1 = os.path.join(app.config['UPLOAD_FOLDER'], filename1)
filepath2 = os.path.join(app.config['UPLOAD_FOLDER'], filename2)
f1.save(filepath1)
f2.save(filepath2)
with open(filepath1, 'rb') as file1:
data1 = file1.read()
hash_object1 = hashlib.sha256(data1)
hex_dig1 = hash_object1.hexdigest()
with open(filepath2, 'rb') as file2:
data2 = file2.read()
hash_object2 = hashlib.sha256(data2)
hex_dig2 = hash_object2.hexdigest()
if hex_dig1 == hex_dig2:
return f'The hashes of {filename1} and {filename2} match!'
else:
return f'The hashes of {filename1} and {filename2} do not match!'
if __name__ == '__main__':
app.run(debug=True)