Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
240 lines (202 loc) · 6.33 KB
/
Copy pathapp.py
File metadata and controls
240 lines (202 loc) · 6.33 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
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
from crypto import *
from flask import (
Flask,
render_template,
request,
send_file,
flash,
redirect,
url_for,
after_this_request
)
import os
from crypto.text import (
encrypt_message,
decrypt_message
)
from crypto.files import (
encrypt_file,
decrypt_file
)
app = Flask(__name__)
app.secret_key = os.environ.get( #random key generated
"SECRET_KEY",
os.urandom(24)
)
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
# home route
@app.route("/")
def home():
return render_template("index.html")
# text encryption route
@app.route("/encrypt", methods=["POST"])
def encrypt():
if request.is_json:
data = request.get_json() or {}
message = data.get("message")
password = data.get("password")
else:
message = request.form.get("message")
password = request.form.get("password")
if not message or not password:
if request.is_json or request.headers.get("Accept") == "application/json":
return {"error": "Message and Password are required."}, 400
flash("Message and Password are required.")
return redirect(url_for("home"))
try:
encrypted = encrypt_message(
message,
password
)
if request.is_json or request.headers.get("Accept") == "application/json":
return {"encrypted_text": encrypted}
return render_template(
"index.html",
encrypted_text=encrypted
)
except Exception as e:
if request.is_json or request.headers.get("Accept") == "application/json":
return {"error": str(e)}, 500
flash(str(e))
return redirect(url_for("home"))
# text decryption
@app.route("/decrypt", methods=["POST"])
def decrypt():
if request.is_json:
data = request.get_json() or {}
token = data.get("encrypted_text")
password = data.get("password")
else:
token = request.form.get("encrypted_text")
password = request.form.get("password")
if not token or not password:
if request.is_json or request.headers.get("Accept") == "application/json":
return {"error": "Encrypted text and password required."}, 400
flash("Encrypted text and password required.")
return redirect(url_for("home"))
try:
plaintext = decrypt_message(
token,
password
)
if request.is_json or request.headers.get("Accept") == "application/json":
return {"decrypted_text": plaintext}
return render_template(
"index.html",
decrypted_text=plaintext
)
except Exception:
if request.is_json or request.headers.get("Accept") == "application/json":
return {"error": "Wrong password or corrupted token."}, 400
flash(
"Wrong password or corrupted token."
)
return redirect(url_for("home"))
# file encryption route
@app.route("/encrypt-file", methods=["POST"])
def encrypt_uploaded_file():
file = request.files.get("file")
password = request.form.get("password")
if not file or file.filename == "":
flash("Select a file.")
return redirect(url_for("home"))
if not password:
flash("Password is required.")
return redirect(url_for("home"))
input_path = os.path.join(
app.config["UPLOAD_FOLDER"],
file.filename
)
output_path = input_path + ".enc"
try:
file.save(input_path)
encrypt_file(
input_path,
output_path,
password
)
# Read the file into memory to release disk handles
import io
with open(output_path, "rb") as f:
file_data = io.BytesIO(f.read())
# Clean up disk files immediately
if os.path.exists(input_path):
try: os.remove(input_path)
except: pass
if os.path.exists(output_path):
try: os.remove(output_path)
except: pass
return send_file(
file_data,
download_name=file.filename + ".enc",
as_attachment=True
)
except Exception as e:
flash(f"File encryption failed: {str(e)}")
if os.path.exists(input_path):
try: os.remove(input_path)
except: pass
if os.path.exists(output_path):
try: os.remove(output_path)
except: pass
return redirect(url_for("home"))
#file decryption route
@app.route("/decrypt-file", methods=["POST"])
def decrypt_uploaded_file():
file = request.files.get("file")
password = request.form.get("password")
if not file or file.filename == "":
flash("Select a file.")
return redirect(url_for("home"))
if not password:
flash("Password is required.")
return redirect(url_for("home"))
input_path = os.path.join(
app.config["UPLOAD_FOLDER"],
file.filename
)
if file.filename.endswith(".enc"):
output_filename = file.filename[:-4]
else:
output_filename = file.filename + ".decrypted"
output_path = os.path.join(
app.config["UPLOAD_FOLDER"],
output_filename
)
try:
file.save(input_path)
decrypt_file(
input_path,
output_path,
password
)
# Read the file into memory to release disk handles
import io
with open(output_path, "rb") as f:
file_data = io.BytesIO(f.read())
# Clean up disk files immediately
if os.path.exists(input_path):
try: os.remove(input_path)
except: pass
if os.path.exists(output_path):
try: os.remove(output_path)
except: pass
return send_file(
file_data,
download_name=output_filename,
as_attachment=True
)
except Exception:
flash("File decryption failed: Invalid password or corrupted file.")
if os.path.exists(input_path):
try: os.remove(input_path)
except: pass
if os.path.exists(output_path):
try: os.remove(output_path)
except: pass
return redirect(url_for("home"))
# main function
if __name__ == "__main__":
app.run(debug=True)