-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_stego1.py
More file actions
136 lines (115 loc) · 5.14 KB
/
gui_stego1.py
File metadata and controls
136 lines (115 loc) · 5.14 KB
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
import os
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter.scrolledtext import ScrolledText
from PIL import Image
from codons import text_to_codons, codons_to_text, set_key
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
# ===== Stego helpers =====
def encode_into_image(cover_path, out_path, message, password=""):
set_key(password)
codons = text_to_codons(message)
data = " ".join(codons).encode("utf-8")
length = len(data)
header = length.to_bytes(4, "big")
payload = header + data
bits = ''.join(f"{b:08b}" for b in payload)
img = Image.open(cover_path).convert("RGB")
pixels = list(img.getdata())
cap = len(pixels) * 3
if len(bits) > cap:
raise ValueError("Message too long for this image!")
new_pixels, idx = [], 0
for r,g,b in pixels:
rgb = [r,g,b]
for c in range(3):
if idx < len(bits):
rgb[c] = (rgb[c] & ~1) | int(bits[idx])
idx += 1
new_pixels.append(tuple(rgb))
img.putdata(new_pixels)
img.save(out_path)
return codons
def decode_from_image(stego_path, password=""):
set_key(password)
img = Image.open(stego_path).convert("RGB")
pixels = list(img.getdata())
bits = "".join(str(ch & 1) for px in pixels for ch in px)
msg_len = int(bits[:32], 2)
data_bits = bits[32:32 + msg_len*8]
data = bytes(int(data_bits[i:i+8], 2) for i in range(0, len(data_bits), 8))
s = data.decode("utf-8", errors="ignore")
codons = s.strip().split()
return codons, codons_to_text(codons)
# ===== GUI =====
app = ttk.Window(themename="cyborg") # High-contrast dark theme
app.title("🧬 DNA Codon Steganography")
app.geometry("800x710")
# Title label
ttk.Label(app, text="DNA Codon Steganography Tool", font=("Segoe UI", 18, "bold"), bootstyle=INFO).pack(pady=10)
# Message input
msg_label = ttk.Label(app, text="Enter Message:", font=("Segoe UI", 12, "bold"))
msg_label.pack(anchor="w", padx=20)
msg_entry = ScrolledText(app, width=80, height=4, font=("Consolas", 11))
msg_entry.pack(padx=20, pady=6)
# Password
pwd_frame = ttk.Frame(app)
pwd_frame.pack(fill="x", padx=20, pady=10)
ttk.Label(pwd_frame, text="🔐 Password (optional):", font=("Segoe UI", 11)).pack(side="left")
pwd_entry = ttk.Entry(pwd_frame, show="*", width=30)
pwd_entry.pack(side="left", padx=10)
# Codon display
ttk.Label(app, text="Generated Codons:", font=("Segoe UI", 12, "bold")).pack(anchor="w", padx=20)
codon_box = ScrolledText(app, width=80, height=4, font=("Consolas", 11), fg="cyan", bg="black")
codon_box.pack(padx=20, pady=6)
# Decoded output
ttk.Label(app, text="Decoded Message:", font=("Segoe UI", 12, "bold")).pack(anchor="w", padx=20)
decoded_box = ScrolledText(app, width=80, height=4, font=("Consolas", 11), fg="lime", bg="black")
decoded_box.pack(padx=20, pady=6)
cover_path, stego_path = tk.StringVar(), tk.StringVar()
def choose_cover():
path = filedialog.askopenfilename(filetypes=[("PNG Images","*.png")])
if path: cover_path.set(path)
def save_stego():
path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG Images","*.png")])
if path: stego_path.set(path)
def do_encode():
try:
if not cover_path.get():
messagebox.showerror("Error","Choose a cover image first")
return
if not stego_path.get():
messagebox.showerror("Error","Choose where to save stego image")
return
msg = msg_entry.get("1.0","end-1c")
pwd = pwd_entry.get()
codons = encode_into_image(cover_path.get(), stego_path.get(), msg, pwd)
codon_box.delete("1.0","end")
codon_box.insert("1.0", " ".join(codons))
messagebox.showinfo("Success", f"Message hidden in {os.path.basename(stego_path.get())}")
except Exception as e:
messagebox.showerror("Error", str(e))
from tkinter import simpledialog
def do_decode():
try:
path = filedialog.askopenfilename(filetypes=[("PNG Images","*.png")])
if not path: return
# Ask password each time
pwd = simpledialog.askstring("Password", "Enter password for decoding:", show="*")
if pwd is None: pwd = "" # Cancel → blank password
codons, msg = decode_from_image(path, pwd)
codon_box.delete("1.0","end")
codon_box.insert("1.0", " ".join(codons))
decoded_box.delete("1.0","end")
decoded_box.insert("1.0", msg)
except Exception as e:
messagebox.showerror("Error", str(e))
# Buttons row
btn_frame = ttk.Frame(app)
btn_frame.pack(pady=20)
ttk.Button(btn_frame, text="📂 Choose Cover Image", command=choose_cover, bootstyle=PRIMARY).grid(row=0, column=0, padx=10)
ttk.Button(btn_frame, text="💾 Save Stego As...", command=save_stego, bootstyle=SECONDARY).grid(row=0, column=1, padx=10)
ttk.Button(btn_frame, text="🧬 Encode", command=do_encode, bootstyle=SUCCESS).grid(row=0, column=2, padx=10)
ttk.Button(btn_frame, text="🔎 Decode from Image", command=do_decode, bootstyle=WARNING).grid(row=0, column=3, padx=10)
app.mainloop()