Wav Integrated and Checkbox Added

This commit is contained in:
cleontay 2023-05-30 19:49:13 +08:00
parent 3cefaa692c
commit c438350b1b
5 changed files with 183 additions and 55 deletions

View File

@ -1,8 +1,9 @@
from flask import Flask, render_template, request, redirect, session, send_from_directory from flask import Flask, render_template, request, redirect, session, send_from_directory
from lib.steganography import img_steg from lib.steganography import img_steg, wav_steg
import cv2 import cv2
import os import os
import sys import sys
import wave
WORKING_PATH = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "upload") + os.sep WORKING_PATH = os.path.join(os.path.dirname(os.path.abspath(sys.argv[0])), "upload") + os.sep
@ -33,16 +34,32 @@ def encoding():
if file.filename != "": if file.filename != "":
file.save(WORKING_PATH + file.filename) file.save(WORKING_PATH + file.filename)
file_extension = os.path.splitext(file.filename)[1]
if file_extension == ".png" or file_extension == ".bmp":
steg = img_steg.img_steg(WORKING_PATH + file.filename, b2c).encode(payload) steg = img_steg.img_steg(WORKING_PATH + file.filename, b2c).encode(payload)
cv2.imwrite(WORKING_PATH + "encoded_" + file.filename, steg) cv2.imwrite(WORKING_PATH + "encoded_" + file.filename, steg)
session['image'] = file.filename session['image'] = file.filename
session['image2'] = "encoded_" + file.filename session['image2'] = "encoded_" + file.filename
elif file_extension == ".wav":
steg = wav_steg.wav_steg(WORKING_PATH + file.filename, b2c).encode(payload)
# Write encoded data to file
new_wav_file = wave.open(WORKING_PATH + "encoded_" + file.filename, "wb")
new_wav_file.setnchannels(steg["num_channels"])
new_wav_file.setsampwidth(steg["sample_width"])
new_wav_file.setframerate(steg["frame_rate"])
new_wav_file.writeframes(steg["num_frames"])
new_wav_file.close()
session['wav'] = file.filename
session['wav2'] = "encoded_" + file.filename
return redirect("/encode_result") return redirect("/encode_result")
@app.route('/encode_result') @app.route('/encode_result')
def encode_result(): def encode_result():
if len(session) > 0: if len(session) > 0:
return render_template("encode_result.html", image=session.get("image"), image2=session.get('image2')) return render_template("encode_result.html")
else: else:
return redirect("/encode") return redirect("/encode")
@ -57,18 +74,32 @@ def decoding():
b2c = [int(x) for x in request.form.getlist("b2c")] b2c = [int(x) for x in request.form.getlist("b2c")]
if file.filename != "": if file.filename != "":
file.save(WORKING_PATH + file.filename) file.save(WORKING_PATH + file.filename)
file_extension = os.path.splitext(file.filename)[1]
if file_extension == ".png" or file_extension == ".bmp":
payload = img_steg.img_steg(WORKING_PATH + file.filename, b2c).decode() payload = img_steg.img_steg(WORKING_PATH + file.filename, b2c).decode()
session["payload"] = payload session["payload"] = payload
session["image"] = file.filename session["image"] = file.filename
elif file_extension == ".wav":
payload = wav_steg.wav_steg(WORKING_PATH + file.filename, b2c).decode()
session["payload"] = payload
session["wav"] = file.filename
return redirect("/decode_result") return redirect("/decode_result")
@app.route('/decode_result') @app.route('/decode_result')
def decode_result(): def decode_result():
if len(session) > 0: if len(session) > 0:
return render_template("decode_result.html", payload=session.get("payload"), image=session.get("image")) return render_template("decode_result.html")
else: else:
return redirect("/decode") return redirect("/decode")
@app.route('/get_session')
def get_session():
session_data = dict(session)
return session_data
@app.route('/upload/<path:filename>') @app.route('/upload/<path:filename>')
def upload(filename): def upload(filename):
return send_from_directory('upload', filename) return send_from_directory('upload', filename)

View File

@ -55,6 +55,10 @@
color: white; color: white;
cursor: pointer; cursor: pointer;
} }
table{
width: 400px;
margin: 10px auto;
}
</style> </style>
</head> </head>
@ -66,20 +70,33 @@
<div class="upload-text">Drag and drop an image here or click to browse</div> <div class="upload-text">Drag and drop an image here or click to browse</div>
<input type="file" name="encoded_file" id="fileInput"> <input type="file" name="encoded_file" id="fileInput">
</div> </div>
<label for="b2c">Bits to Change:</label>
<div>
<input type="checkbox" name="b2c" value="8" placeholder="8">
<input type="checkbox" name="b2c" value="7" placeholder="7">
<input type="checkbox" name="b2c" value="6" placeholder="6">
<input type="checkbox" name="b2c" value="5" placeholder="5">
<input type="checkbox" name="b2c" value="4" placeholder="4">
<input type="checkbox" name="b2c" value="3" placeholder="3">
<input type="checkbox" name="b2c" value="2" placeholder="2">
<input type="checkbox" name="b2c" value="1" placeholder="1">
</div>
<div style="width: 100%; text-align: center; padding: 10px 0px"> <div style="width: 100%; text-align: center; padding: 10px 0px">
<label for="b2c">Bits to Change:</label>
<table>
<tr>
<th>8</th>
<th>7</th>
<th>6</th>
<th>5</th>
<th>4</th>
<th>3</th>
<th>2</th>
<th>1</th>
</tr>
<tr>
<td><input type="checkbox" name="b2c" value="8" placeholder="8"></td>
<td><input type="checkbox" name="b2c" value="7" placeholder="7"></td>
<td><input type="checkbox" name="b2c" value="6" placeholder="6"></td>
<td><input type="checkbox" name="b2c" value="5" placeholder="5"></td>
<td><input type="checkbox" name="b2c" value="4" placeholder="4"></td>
<td><input type="checkbox" name="b2c" value="3" placeholder="3"></td>
<td><input type="checkbox" name="b2c" value="2" placeholder="2"></td>
<td><input type="checkbox" name="b2c" value="1" placeholder="1"></td>
</tr>
</table>
<input type="submit" value="Decode"> <input type="submit" value="Decode">
</div> </div>
</form> </form>
</div> </div>
</body> </body>

View File

@ -4,18 +4,49 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<title>RESULT</title> <title>RESULT</title>
</head> </head>
<body> <body>
<section> </body>
<script>
$(document).ready(function(){
$.get('/get_session', function(response) {
if(response.hasOwnProperty("image")){
console.log(response)
var html_content = `<section>
<div> <div>
<h1>Decoded</h1> <h1>Decoded</h1>
<img src="{{ url_for('upload', filename=image) }}" alt="Image Not Found"> <img src="upload/` + response["image"] + `" alt="Image Not Found">
</div> </div>
<div> <div>
<h1>Secret Text:</h1> <h1>Secret Text:</h1>
<p>{{ payload }}</p> <p>` + response["payload"] + `</p>
</div> </div>
</section> </section>`
</body> $("body").append(html_content)
}
else if(response.hasOwnProperty("wav")){
var html_content = `<section>
<h1>Decoded</h1>
<audio controls>
<source src="upload/` + response["wav"] + `" type="audio/wav">
Your browser does not support the audio element.
</audio>
<div>
<h1>Secret Text:</h1>
<p>` + response["payload"] + `</p>
</div>
</section>`
$("body").append(html_content)
}
else if(response.hasOwnProperty("txt")){
console.log(response)
}
});
})
</script>
</html> </html>

View File

@ -57,6 +57,10 @@
color: white; color: white;
cursor: pointer; cursor: pointer;
} }
table{
width: 400px;
margin: 10px auto;
}
</style> </style>
</head> </head>
@ -70,16 +74,28 @@
</div> </div>
<div style="width: 100%; text-align: center; padding: 10px 0px"> <div style="width: 100%; text-align: center; padding: 10px 0px">
<label for="b2c">Bits to Change:</label> <label for="b2c">Bits to Change:</label>
<div> <table>
<input type="checkbox" name="b2c" value="8" placeholder="8"> <tr>
<input type="checkbox" name="b2c" value="7" placeholder="7"> <th>8</th>
<input type="checkbox" name="b2c" value="6" placeholder="6"> <th>7</th>
<input type="checkbox" name="b2c" value="5" placeholder="5"> <th>6</th>
<input type="checkbox" name="b2c" value="4" placeholder="4"> <th>5</th>
<input type="checkbox" name="b2c" value="3" placeholder="3"> <th>4</th>
<input type="checkbox" name="b2c" value="2" placeholder="2"> <th>3</th>
<input type="checkbox" name="b2c" value="1" placeholder="1"> <th>2</th>
</div> <th>1</th>
</tr>
<tr>
<td><input type="checkbox" name="b2c" value="8" placeholder="8"></td>
<td><input type="checkbox" name="b2c" value="7" placeholder="7"></td>
<td><input type="checkbox" name="b2c" value="6" placeholder="6"></td>
<td><input type="checkbox" name="b2c" value="5" placeholder="5"></td>
<td><input type="checkbox" name="b2c" value="4" placeholder="4"></td>
<td><input type="checkbox" name="b2c" value="3" placeholder="3"></td>
<td><input type="checkbox" name="b2c" value="2" placeholder="2"></td>
<td><input type="checkbox" name="b2c" value="1" placeholder="1"></td>
</tr>
</table>
<input type="text" name="payload" placeholder="Secret Code"> <input type="text" name="payload" placeholder="Secret Code">
<input type="submit" value="Encode"> <input type="submit" value="Encode">
</div> </div>

View File

@ -4,22 +4,55 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<title>RESULT</title> <title>RESULT</title>
</head> </head>
<body> <body>
</body>
<script>
<section style="display: flex; justify-content: flex-start;"> $(document).ready(function(){
$.get('/get_session', function(response) {
if(response.hasOwnProperty("image")){
console.log(response)
var html_content = `<section style="display: flex; justify-content: flex-start;">
<div> <div>
<h1>Original</h1> <h1>Original</h1>
<img src="{{ url_for('upload', filename=image2) }}" alt="Image Not Found"> <img src="upload/` + response["image"] + `" alt="Image Not Found">
</div> </div>
<div> <div>
<h1>Encoded</h1> <h1>Encoded</h1>
<img src="{{ url_for('upload', filename=image2) }}" alt="Image Not Found"> <img src="upload/` + response["image2"] + `" alt="Image Not Found">
</div> </div>
</section> </section>`
$("body").append(html_content)
}
else if(response.hasOwnProperty("wav")){
var html_content = `<section style="display: flex; justify-content: flex-start;">
<div>
<h1>Original</h1>
<audio controls>
<source src="upload/` + response["wav"] + `" type="audio/wav">
Your browser does not support the audio element.
</audio>
</div>
<div>
<h1>Encoded</h1>
<audio controls>
<source src="upload/` + response["wav2"] + `" type="audio/wav">
Your browser does not support the audio element.
</audio>
</div>
</section>`
$("body").append(html_content)
}
else if(response.hasOwnProperty("txt")){
console.log(response)
}
});
})
</body> </script>
</html> </html>