-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecure_transmit.py
More file actions
145 lines (123 loc) · 5.97 KB
/
Copy pathSecure_transmit.py
File metadata and controls
145 lines (123 loc) · 5.97 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
137
138
139
140
141
142
143
144
145
import cv2
import numpy as np
import base64
import os
import PyPDF2
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.fernet import Fernet
# --- CORE TOOLS (UNCHANGED) ---
def get_crypto_tools(password: str):
salt = b'\x00' * 16
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000)
key = base64.urlsafe_b64encode(kdf.derive(password.encode()))
return Fernet(key)
# --- 1. IMAGE STEGANOGRAPHY ---
def transmit_file(source_file, carrier_image, output_name, password):
with open(source_file, 'rb') as f:
raw_data = f.read()
_embed_in_image(raw_data, carrier_image, output_name, password)
def transmit_text(text_message, carrier_image, output_name, password):
raw_data = text_message.encode()
_embed_in_image(raw_data, carrier_image, output_name, password)
def _embed_in_image(raw_data, carrier_image, output_name, password):
cipher = get_crypto_tools(password)
encrypted_blob = cipher.encrypt(raw_data)
final_payload = encrypted_blob + b"STOP_HERE"
bits = ''.join([format(b, "08b") for b in final_payload])
img = cv2.imread(carrier_image)
flat_img = img.flatten()
# SAFETY CHECK: Prevents the IndexError crash
if len(bits) > len(flat_img):
print(f"❌ Error: {len(bits)} bits is too large for this image capacity!")
return
for i in range(len(bits)):
flat_img[i] = (flat_img[i] & 254) | int(bits[i])
cv2.imwrite(output_name, flat_img.reshape(img.shape))
print(f"✅ SUCCESS: Data hidden in IMAGE {output_name}")
# --- ADDED RECOVERY FUNCTIONS ---
def recover_from_image(stego_image, output_file, password, is_text=False):
img = cv2.imread(stego_image)
flat_bits = [str(p & 1) for p in img.flatten()]
full_bin = "".join(flat_bits)
all_bytes = bytearray()
for i in range(0, len(full_bin), 8):
byte = int(full_bin[i:i+8], 2)
all_bytes.append(byte)
if all_bytes.endswith(b"STOP_HERE"): break
cipher = get_crypto_tools(password)
decrypted = cipher.decrypt(bytes(all_bytes[:-9]))
if is_text: print(f"🔓 TEXT: {decrypted.decode()}")
else:
with open(output_file, 'wb') as f: f.write(decrypted)
print(f"🔓 FILE RECOVERED: {output_file}")
def recover_from_pdf(stego_pdf, output_file, password, is_text=False):
reader = PyPDF2.PdfReader(stego_pdf)
encrypted_blob = reader.metadata.get('/Subject')
cipher = get_crypto_tools(password)
decrypted = cipher.decrypt(encrypted_blob.encode())
if is_text: print(f"🔓 TEXT: {decrypted.decode()}")
else:
with open(output_file, 'wb') as f: f.write(decrypted)
print(f"🔓 FILE RECOVERED: {output_file}")
def recover_from_file(stego_file, output_file, password, is_text=False):
with open(stego_file, 'rb') as f: data = f.read()
secret_part = data.split(b"FILE_SECRET_START")[1]
cipher = get_crypto_tools(password)
decrypted = cipher.decrypt(secret_part)
if is_text: print(f"🔓 TEXT: {decrypted.decode()}")
else:
with open(output_file, 'wb') as f: f.write(decrypted)
print(f"🔓 FILE RECOVERED: {output_file}")
# --- 2. PDF STEGANOGRAPHY ---
def hide_in_pdf(secret_source, carrier_pdf, output_pdf, password, is_text=False):
if is_text: raw_data = secret_source.encode()
else:
with open(secret_source, 'rb') as f: raw_data = f.read()
cipher = get_crypto_tools(password)
encrypted_blob = cipher.encrypt(raw_data).decode()
reader = PyPDF2.PdfReader(carrier_pdf)
writer = PyPDF2.PdfWriter()
for page in reader.pages: writer.add_page(page)
writer.add_metadata({'/Subject': encrypted_blob})
with open(output_pdf, 'wb') as f: writer.write(f)
print(f"✅ SUCCESS: Data hidden in PDF Metadata {output_pdf}")
# --- 3. FILE STEGANOGRAPHY ---
def hide_in_file(secret_source, carrier_file, output_file, password, is_text=False):
if is_text: secret_data = secret_source.encode()
else:
with open(secret_source, 'rb') as f: secret_data = f.read()
cipher = get_crypto_tools(password)
encrypted_secret = cipher.encrypt(secret_data)
with open(carrier_file, 'rb') as f: carrier_data = f.read()
with open(output_file, 'wb') as f:
f.write(carrier_data)
f.write(b"FILE_SECRET_START")
f.write(encrypted_secret)
print(f"✅ SUCCESS: Data hidden inside FILE {output_file}")
# --- EXECUTION EXAMPLES (YOUR ORIGINAL LINES) ---
PASS = "ismagi_secure_2026"
# A. HIDE PHOTO INSIDE A PHOTO
# # Use a smaller file like app2..pdf here if carrier.png is small to avoid IndexError
# transmit_file("licensed-image.jpg", "carrier.png", "image_in_image.png", PASS)
# B. HIDE TYPED TEXT INSIDE A PHOTO
transmit_text("hello this is a secret message", "carrier.png", "text_in_image.png", PASS)
# # C. HIDE A FILE INSIDE A PDF (Changed to message.txt so it's fast!)
# hide_in_pdf("message.txt", "app2..pdf", "message_in_pdf.pdf", PASS)
# # D. HIDE TYPED TEXT INSIDE A PDF (Corrected filename to app2..pdf)
# hide_in_pdf("im hiding shhh", "app2..pdf", "text_in_pdf.pdf", PASS, is_text=True)
# # E. HIDE A PDF INSIDE A TEXT FILE (message.txt)
# hide_in_file("app2..pdf", "message.txt", "pdf_hidden_in_text.txt", PASS)
# # F. HIDE TYPED TEXT INSIDE A TEXT FILE (message.txt)
# hide_in_file("hey im a secret", "message.txt", "hidden_in_text.txt", PASS, is_text=True)
# # G.HIDE A PHOTO INSIDE A TEXT FILE (message.txt)
# hide_in_file("carrier.png", "message.txt", "photo_hidden_in_text.txt", PASS)
# # H.PDF inside a Photo
# transmit_file("app2..pdf", "carrier.png", "pdf_in_photo.png", PASS)
# recover_from_image("pdf_in_photo.png", "recovered_pdf_from_photo.pdf", PASS)
# # I. PDF inside a PDF
# hide_in_pdf("app2..pdf", "app2..pdf", "pdf_in_pdf.pdf", PASS)
# recover_from_pdf("pdf_in_pdf.pdf", "recovered_pdf_from_pdf.pdf", PASS)
# # --- RECOVERY (How to get them out) ---
# recover_from_file("photo_hidden_in_text.txt", "recovered_photo.png", PASS)
# recover_from_pdf("text_in_pdf.pdf", None, PASS, is_text=True)