-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
60 lines (45 loc) · 1.78 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
from flask import Flask,render_template,request,json,Response,jsonify,send_from_directory,send_file
import json
from datetime import datetime
import os
from werkzeug.utils import secure_filename
uploads_dir='uploads'
app = Flask(__name__,template_folder="templates",static_url_path='/static',static_folder='static')
@app.errorhandler(404)
def not_found(error=None):
message = {
'status':404,
'message':'Not Found: ' + request.url,
}
resp = jsonify(message)
resp.status_code = 404
return resp
@app.route('/',methods=['GET','POST'])
def encrypt_page():
if request.method=='GET':
return render_template('encrypt.html')
if request.method=='POST':
file=request.files['file']
key=request.form['key']
file_path=os.path.join(uploads_dir, secure_filename(file.filename))
temp_file_path=uploads_dir+'/'+secure_filename(file.filename)+'_temp'
# temp_file_path_new=uploads_dir+'/'+'decoded_a/'+ secure_filename(file.filename)
file.save(os.path.join(temp_file_path))
fin = open(temp_file_path, 'rb')
image = fin.read()
fin.close()
image = bytearray(image)
key= int.from_bytes(key.encode('utf-8'), byteorder='big')%256
for index, values in enumerate(image):
image[index] = values ^ key
fin = open(file_path, 'wb')
fin.write(image)
fin.close()
os.remove(temp_file_path)
return send_file(file_path,as_attachment=True)
@app.route('/decrypt',methods=['GET','POST'])
def decrypt_page():
if request.method=='GET':
return render_template('decrypt.html')
if __name__=='__main__':
app.run(debug=True)