-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
156 lines (130 loc) · 5.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
import os, sys
import json
import zipfile
from os.path import dirname, join, abspath
from flask import Flask
from flask import Flask, jsonify, request, render_template,redirect,url_for,flash,send_file
from . import module
import common
from werkzeug.utils import secure_filename
import pathlib
import shutil
import base64
UPLOAD_FOLDER = pathlib.Path(__file__).parent.absolute()
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
@app.route('/', methods=['GET', 'POST'])
def home():
return render_template("index.html")
@app.route('/generate_key')
def generate_key():
key = os.urandom(16)
enc_key = base64.b64encode(key)
completeName = os.path.join(UPLOAD_FOLDER, "SecretKey"+".txt")
file1 = open(completeName, "w")
toFile = str(enc_key.decode('UTF-8'))
file1.write(toFile)
file1.close()
return send_file(completeName, as_attachment=True)
@app.route('/encrypt-folder', methods=['GET', 'POST'])
def upload_folder_encrypt():
if request.method == 'POST':
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
else:
key = request.form['key']
common.file_remover()
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
file_path = str(pathlib.Path(__file__).parent.absolute())+'/' +filename
unziper(path=file_path)
nsf_folder = filename.replace('.zip', '')
os.remove(file_path)
name_of_file = key.encode('UTF_8')
completeName = os.path.join(nsf_folder, "SecretKey"+".txt")
file1 = open(completeName, "w")
toFile = str(key)
file1.write(toFile)
file1.close()
module.aes_encrypt_all_files(nsf_folder,secret_key=name_of_file)
nsf_path = str(UPLOAD_FOLDER)+'/' + nsf_folder
common.ziper_(folder= nsf_path, filename=nsf_folder)
download_path = str(UPLOAD_FOLDER)+ '/'+ nsf_folder+'_nsf.zip'
path = download_path
shutil.rmtree(nsf_path)
return send_file(path, as_attachment=True)
return render_template('encrypt_folder.html')
def unziper(path):
with zipfile.ZipFile(path, 'r') as zip_ref:
zip_ref.extractall(os.path.join(app.config['UPLOAD_FOLDER']))
@app.route('/encrypt-file', methods=['GET', 'POST'])
def upload_file_encrypt():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
common.file_remove_for_videos()
file = request.files['file']
key = request.form['key']
sec_key = key.encode('UTF_8')
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and common.allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
module.aes_encrypt_file(file_name=filename,secret_key=sec_key)
nsf_path = filename.replace('.mp4','')
download_path = str(UPLOAD_FOLDER)+ '/'+ nsf_path+'.nsf'
path = download_path
return send_file(path, as_attachment=True)
return render_template('encrypt_file.html')
@app.route('/decrypt-folder', methods=['GET', 'POST'])
def upload_folder_decrypt():
if request.method == 'POST':
file = request.files['file']
if file.filename == '':
flash('No selected file')
return redirect(request.url)
else:
key = request.form['key']
common.file_remover()
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
file_path = str(pathlib.Path(__file__).parent.absolute())+'/' +filename
unziper(path=file_path)
nsf_folder = filename.replace('.zip', '')
os.remove(file_path)
module.aes_decrypt_all_files(nsf_folder,secret_key=key)
nsf_path = str(UPLOAD_FOLDER)+'/' + nsf_folder
common.ziper_dec(folder= nsf_path, filename=nsf_folder)
download_path = str(UPLOAD_FOLDER)+ '/'+ nsf_folder +'.zip'
path = download_path
return send_file(path, as_attachment=True)
return render_template('decrypt_folder.html')
@app.route('/decrypt-file', methods=['GET', 'POST'])
def upload_file_decrypt():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
common.file_remove_for_videos()
file = request.files['file']
key = request.form['key']
sec_key = key.encode('UTF_8')
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and common.allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
module.aes_decrypt_file(file_name=filename,secret_key=key)
nsf_path = filename.replace('.nsf','')
download_path = str(UPLOAD_FOLDER)+ '/'+ nsf_path+'.mp4'
path = download_path
return send_file(path, as_attachment=True)
return render_template('decrypt_file.html')
if __name__ == "__main__":
app.run(debug=True,host= '0.0.0.0', port='5000')