-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes.py
162 lines (131 loc) · 5.46 KB
/
routes.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
from fileinput import filename
from flask import Flask,render_template,request,redirect,send_from_directory,send_file
# from werkzeug import secure_filename
import wave,os,time,zipfile
import tempfile
from steno import encode,decode,phase_encode,phase_decode
from img_en import lsbimg_decode,lsbimg_encode,mask
from text_en import txt_encode,txt_decode
app = Flask(__name__)
uploads_dir = tempfile.gettempdir()
os.makedirs(os.path.join(uploads_dir,"steno"), exist_ok=True)
uploads_dir = os.path.join(uploads_dir,"steno")
@app.route('/',methods=['GET','POST'])
def hello_world():
if request.method == 'GET':
return render_template('./index.html')
elif request.method == 'POST':
# file = request.args
# audio = wave.open(file,mode="rb")
# print(request)
# encode(audio)
return redirect('/')
@app.route('/upload',methods=['POST'])
def upload():
if request.method == 'POST':
profile = request.files['file']
# path = os.path.join(uploads_dir, secure_filename(profile.filename))
path = os.path.join(uploads_dir, profile.filename)
print(list(request.form.keys()))
type = request.form['type']
profile.save(path)
# print("Saved ",message)
# IMAGE STEGANOGRAPHY
if(type in ["lsbimg","mask","enimg"]):
if(type=="lsbimg"):
message = request.form['message']
lsbimg_encode(path,message)
elif(type=="mask"):
maskpath = os.path.join(uploads_dir, request.files['maskfile'].filename)
request.files['maskfile'].save(maskpath)
print("maskpath: ",maskpath)
mask(path,maskpath)
else:
pass
return send_file(
os.path.join(uploads_dir,profile.filename),
# mimetype = 'png',
attachment_filename= profile.filename[:-4]+"_encoded"+profile.filename[-4:],
as_attachment = True)
# AUDIO STEGANOGRAPHY
elif(type in ["phase","lsb","parity"]):
audio = wave.open(path,mode="rb")
# print("Opened")
# encode(audio,profile.filename,message)
if(type=="phase"):
message = request.form['message']
phase_encode(audio,path,profile.filename,message)
elif(type=="lsb"):
message = request.form['message']
encode(audio,profile.filename,message)
# print("Encoded")
return send_file(os.path.join(uploads_dir,profile.filename+"encoded"),
mimetype = 'wav',
attachment_filename= profile.filename+"_encoded.wav",
as_attachment = True)
# TEXT STEGANOGRAPHY
elif(type in ["snow","lookalike","zw"]):
f = open(path, "r")
data = f.read()
f.close()
message = request.form['message']
if(type=="zero"):
new_data = txt_encode(data,message.encode("ascii"),"zw",binary=True)
elif(type=="snow"):
new_data = txt_encode(data,message.encode("ascii"),"snow",binary=True)
else:
new_data = txt_encode(data,message.encode("ascii"),"lookalike",binary=True)
with open(path+"_encoded", "w", encoding="utf-8") as f:
f.write(new_data)
f.close()
return send_file(os.path.join(uploads_dir,profile.filename+"_encoded"),
mimetype = 'txt',
attachment_filename= profile.filename+"_encoded.txt",
as_attachment = True)
# return send_from_directory(os.path.join(uploads_dir, 'sampleStego.wav'),filename="encoded.wav",as_attachment=True)
else:
return redirect('/')
@app.route('/decode',methods=['POST'])
def decoder():
if request.method == 'POST':
profile = request.files['file']
type = request.form['type']
# path = os.path.join(uploads_dir, secure_filename(profile.filename))
path = os.path.join(uploads_dir,profile.filename)
# setupdir(path)
# os.mkdir(path)
profile.save(path)
# print("Saved")
# IMAGE STEGANOGRAPHY
if(type in ["lsbimg","rpeimg","enimg"]):
if(type=="lsbimg"):
text = lsbimg_decode(path)
print("got text",text)
elif(type=="mask"):
pass
else:
pass
elif(type in ["phase","lsb","parity"]):
audio = wave.open(path,mode="rb")
# print("Opened")
if(type=="phase"):
text = phase_decode(audio,profile.filename,path)
elif(type=="lsb"):
text = decode(audio,profile.filename)
# TEXT STEGANOGRAPHY
elif(type in ["snow","lookalike","zw"]):
with open(path, "r", encoding="utf-8") as f:
data = f.read()
f.close()
text = txt_decode(data,type,binary=True)
print("decoded==> ",str(text))
return render_template('./index.html', deciphered=text)
else:
return redirect('/')
# def setupdir(uploads_dir):
# try:
# os.rmdir(uploads_dir)
# print("Directory '% s' has been removed successfully" % uploads_dir)
# except OSError as error:
# print(error)
# print("Directory '% s' can not be removed" % uploads_dir)