Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,13 @@ import random
|
|
| 5 |
import string
|
| 6 |
import base64
|
| 7 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def random_key(length=16):
|
| 10 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
|
@@ -13,9 +20,27 @@ def xor_encrypt_decrypt(text, key):
|
|
| 13 |
key_repeated = (key * (len(text) // len(key) + 1))[:len(text)]
|
| 14 |
return ''.join(chr(ord(a) ^ ord(b)) for a, b in zip(text, key_repeated))
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def encode_image(image, input_text, key):
|
| 17 |
if image is None or not input_text or not key:
|
| 18 |
-
return None, gr.update(visible=True, value="Please provide image, input text, and key.")
|
| 19 |
try:
|
| 20 |
encrypted = xor_encrypt_decrypt(input_text, key)
|
| 21 |
encrypted_b64 = base64.b64encode(encrypted.encode()).decode()
|
|
@@ -30,9 +55,14 @@ def encode_image(image, input_text, key):
|
|
| 30 |
temp_file.write(buf.read())
|
| 31 |
temp_file.flush()
|
| 32 |
temp_file.close()
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
-
return None, gr.update(visible=True, value=f"Error: {str(e)}")
|
| 36 |
|
| 37 |
def decode_image(img, key):
|
| 38 |
if img is None or not key:
|
|
@@ -49,39 +79,39 @@ def decode_image(img, key):
|
|
| 49 |
return "", gr.update(visible=True, value=f"Error: {str(e)}")
|
| 50 |
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
-
gr.Markdown("
|
| 53 |
-
with gr.Tab("ENC"):
|
| 54 |
with gr.Row():
|
| 55 |
with gr.Column():
|
| 56 |
image_enc = gr.Image(label="Upload Image", type="pil")
|
| 57 |
-
input_text = gr.Textbox(label="Input text", lines=5, placeholder="Enter
|
| 58 |
with gr.Row():
|
| 59 |
key_enc = gr.Textbox(label="Key", max_lines=1, placeholder="Enter key or generate one")
|
| 60 |
gen_key_btn = gr.Button("๐ Generate random key")
|
| 61 |
start_btn = gr.Button("Start")
|
| 62 |
download_link = gr.File(label="Download image with encrypted message", visible=False)
|
| 63 |
enc_status = gr.Textbox(label="Status", interactive=False, visible=True)
|
|
|
|
| 64 |
|
| 65 |
-
with gr.Tab("DEC"):
|
| 66 |
with gr.Row():
|
| 67 |
with gr.Column():
|
| 68 |
-
image_dec = gr.Image(label="Upload Image", type="filepath")
|
| 69 |
-
key_dec = gr.Textbox(label="Key", max_lines=1, placeholder="Enter key")
|
| 70 |
dec_btn = gr.Button("Decrypt")
|
| 71 |
output_text = gr.Textbox(label="Decrypted Message", lines=5, interactive=False)
|
| 72 |
dec_status = gr.Textbox(label="Status", interactive=False, visible=True)
|
| 73 |
|
| 74 |
-
# Button actions
|
| 75 |
gen_key_btn.click(lambda: random_key(), [], key_enc)
|
| 76 |
|
| 77 |
def on_start(image, text, key):
|
| 78 |
-
result, status = encode_image(image, text, key)
|
| 79 |
-
return result, gr.update(visible=bool(result)), status
|
| 80 |
|
| 81 |
start_btn.click(
|
| 82 |
on_start,
|
| 83 |
inputs=[image_enc, input_text, key_enc],
|
| 84 |
-
outputs=[download_link, download_link, enc_status]
|
| 85 |
)
|
| 86 |
|
| 87 |
def on_decrypt(image_path, key):
|
|
|
|
| 5 |
import string
|
| 6 |
import base64
|
| 7 |
import tempfile
|
| 8 |
+
import dropbox
|
| 9 |
+
import os
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
+
# Get Dropbox token from environment (set as Space Secret)
|
| 13 |
+
DROPBOX_TOKEN = os.environ.get("DROPBOX_TOKEN")
|
| 14 |
+
DROPBOX_FOLDER = "/imagefun" # change as needed
|
| 15 |
|
| 16 |
def random_key(length=16):
|
| 17 |
return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
|
|
|
|
| 20 |
key_repeated = (key * (len(text) // len(key) + 1))[:len(text)]
|
| 21 |
return ''.join(chr(ord(a) ^ ord(b)) for a, b in zip(text, key_repeated))
|
| 22 |
|
| 23 |
+
def save_to_dropbox(image_bytes, key):
|
| 24 |
+
if not DROPBOX_TOKEN:
|
| 25 |
+
return "Dropbox token not configured in Space secrets."
|
| 26 |
+
dbx = dropbox.Dropbox(DROPBOX_TOKEN)
|
| 27 |
+
now = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 28 |
+
filename = f"{key}_{now}.png"
|
| 29 |
+
key_filename = f"{key}_{now}.txt"
|
| 30 |
+
img_path = f"{DROPBOX_FOLDER}/{filename}"
|
| 31 |
+
key_path = f"{DROPBOX_FOLDER}/{key_filename}"
|
| 32 |
+
try:
|
| 33 |
+
# Upload image
|
| 34 |
+
dbx.files_upload(image_bytes, img_path, mode=dropbox.files.WriteMode.overwrite)
|
| 35 |
+
# Upload key as a text file
|
| 36 |
+
dbx.files_upload(key.encode(), key_path, mode=dropbox.files.WriteMode.overwrite)
|
| 37 |
+
return f"Saved to Dropbox as {img_path} and {key_path}"
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return f"Dropbox upload failed: {e}"
|
| 40 |
+
|
| 41 |
def encode_image(image, input_text, key):
|
| 42 |
if image is None or not input_text or not key:
|
| 43 |
+
return None, gr.update(visible=True, value="Please provide image, input text, and key."), ""
|
| 44 |
try:
|
| 45 |
encrypted = xor_encrypt_decrypt(input_text, key)
|
| 46 |
encrypted_b64 = base64.b64encode(encrypted.encode()).decode()
|
|
|
|
| 55 |
temp_file.write(buf.read())
|
| 56 |
temp_file.flush()
|
| 57 |
temp_file.close()
|
| 58 |
+
|
| 59 |
+
# For Dropbox, we need the bytes again
|
| 60 |
+
buf.seek(0)
|
| 61 |
+
dbx_status = save_to_dropbox(buf.getvalue(), key)
|
| 62 |
+
|
| 63 |
+
return temp_file.name, gr.update(visible=True, value="Success! Download your image below."), dbx_status
|
| 64 |
except Exception as e:
|
| 65 |
+
return None, gr.update(visible=True, value=f"Error: {str(e)}"), ""
|
| 66 |
|
| 67 |
def decode_image(img, key):
|
| 68 |
if img is None or not key:
|
|
|
|
| 79 |
return "", gr.update(visible=True, value=f"Error: {str(e)}")
|
| 80 |
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown("# ๐ผ๏ธ Steganography: Embed & Extract Encrypted Messages in Images โ Phase 2 with Dropbox")
|
| 83 |
+
with gr.Tab("ENC (Encode)"):
|
| 84 |
with gr.Row():
|
| 85 |
with gr.Column():
|
| 86 |
image_enc = gr.Image(label="Upload Image", type="pil")
|
| 87 |
+
input_text = gr.Textbox(label="Input text", lines=5, placeholder="Enter your secret message here...")
|
| 88 |
with gr.Row():
|
| 89 |
key_enc = gr.Textbox(label="Key", max_lines=1, placeholder="Enter key or generate one")
|
| 90 |
gen_key_btn = gr.Button("๐ Generate random key")
|
| 91 |
start_btn = gr.Button("Start")
|
| 92 |
download_link = gr.File(label="Download image with encrypted message", visible=False)
|
| 93 |
enc_status = gr.Textbox(label="Status", interactive=False, visible=True)
|
| 94 |
+
dbx_status = gr.Textbox(label="Dropbox Status", interactive=False, visible=True)
|
| 95 |
|
| 96 |
+
with gr.Tab("DEC (Decode)"):
|
| 97 |
with gr.Row():
|
| 98 |
with gr.Column():
|
| 99 |
+
image_dec = gr.Image(label="Upload Image With Encrypted Text", type="filepath")
|
| 100 |
+
key_dec = gr.Textbox(label="Key", max_lines=1, placeholder="Enter key used for encryption")
|
| 101 |
dec_btn = gr.Button("Decrypt")
|
| 102 |
output_text = gr.Textbox(label="Decrypted Message", lines=5, interactive=False)
|
| 103 |
dec_status = gr.Textbox(label="Status", interactive=False, visible=True)
|
| 104 |
|
|
|
|
| 105 |
gen_key_btn.click(lambda: random_key(), [], key_enc)
|
| 106 |
|
| 107 |
def on_start(image, text, key):
|
| 108 |
+
result, status, dbx_status_msg = encode_image(image, text, key)
|
| 109 |
+
return result, gr.update(visible=bool(result)), status, dbx_status_msg
|
| 110 |
|
| 111 |
start_btn.click(
|
| 112 |
on_start,
|
| 113 |
inputs=[image_enc, input_text, key_enc],
|
| 114 |
+
outputs=[download_link, download_link, enc_status, dbx_status]
|
| 115 |
)
|
| 116 |
|
| 117 |
def on_decrypt(image_path, key):
|