dev427tools commited on
Commit
9957645
·
verified ·
1 Parent(s): 647d8dc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, PngImagePlugin
3
+ import io
4
+ import random
5
+ import string
6
+ import base64
7
+
8
+ def random_key(length=16):
9
+ return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
10
+
11
+ def xor_encrypt_decrypt(text, key):
12
+ # Simple XOR encryption/decryption
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
+ # Encrypt and base64-encode the message
21
+ encrypted = xor_encrypt_decrypt(input_text, key)
22
+ encrypted_b64 = base64.b64encode(encrypted.encode()).decode()
23
+ img = image.copy()
24
+ meta = PngImagePlugin.PngInfo()
25
+ meta.add_text("encrypted_message", encrypted_b64)
26
+ buf = io.BytesIO()
27
+ img.save(buf, format="PNG", pnginfo=meta)
28
+ buf.seek(0)
29
+ return buf, gr.update(visible=True, value="Success! Download your image below.")
30
+ except Exception as e:
31
+ return None, gr.update(visible=True, value=f"Error: {str(e)}")
32
+
33
+ def decode_image(img, key):
34
+ if img is None or not key:
35
+ return "", gr.update(visible=True, value="Please provide image and key.")
36
+ try:
37
+ pil_img = Image.open(img)
38
+ encrypted_b64 = pil_img.info.get("encrypted_message")
39
+ if not encrypted_b64:
40
+ return "", gr.update(visible=True, value="No encrypted message found in this image.")
41
+ encrypted = base64.b64decode(encrypted_b64).decode()
42
+ decrypted = xor_encrypt_decrypt(encrypted, key)
43
+ return decrypted, gr.update(visible=False)
44
+ except Exception as e:
45
+ return "", gr.update(visible=True, value=f"Error: {str(e)}")
46
+
47
+ with gr.Blocks() as demo:
48
+ gr.Markdown("# 🖼️ Steganography: Embed & Extract Encrypted Messages in Images")
49
+ with gr.Tab("ENC (Encode)"):
50
+ with gr.Row():
51
+ with gr.Column():
52
+ image_enc = gr.Image(label="Upload Image", type="pil")
53
+ input_text = gr.Textbox(label="Input text", lines=5, placeholder="Enter your secret message here...")
54
+ with gr.Row():
55
+ key_enc = gr.Textbox(label="Key", max_lines=1, placeholder="Enter key or generate one")
56
+ gen_key_btn = gr.Button("🔑 Generate random key")
57
+ start_btn = gr.Button("Start")
58
+ download_link = gr.File(label="Download image with encrypted message", visible=False)
59
+ enc_status = gr.Textbox(label="Status", interactive=False, visible=True)
60
+
61
+ with gr.Tab("DEC (Decode)"):
62
+ with gr.Row():
63
+ with gr.Column():
64
+ image_dec = gr.Image(label="Upload Image With Encrypted Text", type="filepath")
65
+ key_dec = gr.Textbox(label="Key", max_lines=1, placeholder="Enter key used for encryption")
66
+ dec_btn = gr.Button("Decrypt")
67
+ output_text = gr.Textbox(label="Decrypted Message", lines=5, interactive=False)
68
+ dec_status = gr.Textbox(label="Status", interactive=False, visible=True)
69
+
70
+ # Button actions
71
+ gen_key_btn.click(lambda: random_key(), [], key_enc)
72
+
73
+ def on_start(image, text, key):
74
+ result, status = encode_image(image, text, key)
75
+ # Only show download if result exists
76
+ return result, gr.update(visible=bool(result)), status
77
+
78
+ start_btn.click(
79
+ on_start,
80
+ inputs=[image_enc, input_text, key_enc],
81
+ outputs=[download_link, download_link, enc_status]
82
+ )
83
+
84
+ def on_decrypt(image_path, key):
85
+ result, status = decode_image(image_path, key)
86
+ return result, status
87
+
88
+ dec_btn.click(
89
+ on_decrypt,
90
+ inputs=[image_dec, key_dec],
91
+ outputs=[output_text, dec_status]
92
+ )
93
+
94
+ if __name__ == "__main__":
95
+ demo.launch()