-
Notifications
You must be signed in to change notification settings - Fork 30
/
secret_pixel.py
371 lines (290 loc) · 14.8 KB
/
secret_pixel.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import argparse
import sys
import os
import random
from PIL import Image
import numpy as np
from cryptography.hazmat.primitives.padding import PKCS7
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import zlib
from getpass import getpass
"""
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
def encrypt_data(data, public_key):
# Generate a random session key
session_key = os.urandom(32) # 32 bytes for 256-bit key
# Derive a symmetric key from the session key
salt = os.urandom(16) # 16 bytes for 128-bit salt
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=200000, # Increased iterations for added security
backend=default_backend()
)
key = kdf.derive(session_key)
# Encrypt the data with AES
iv = os.urandom(16) # 16 bytes for 128-bit IV
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
padder = PKCS7(algorithms.AES.block_size).padder()
padded_data = padder.update(data) + padder.finalize()
encrypted_data = encryptor.update(padded_data) + encryptor.finalize()
# Encrypt the session key with RSA
encrypted_session_key = public_key.encrypt(
session_key,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return encrypted_session_key, salt, iv, encrypted_data
def decrypt_data(encrypted_session_key, salt, iv, encrypted_data, private_key):
# Decrypt the session key with RSA
session_key = private_key.decrypt(
encrypted_session_key,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
# Derive the symmetric key from the session key
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=200000, # Increased iterations for added security
backend=default_backend()
)
key = kdf.derive(session_key)
# Decrypt the data with AES
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()
decrypted_padded_data = decryptor.update(encrypted_data) + decryptor.finalize()
unpadder = PKCS7(algorithms.AES.block_size).unpadder()
decrypted_data = unpadder.update(decrypted_padded_data) + unpadder.finalize()
return decrypted_data
def compute_seed_from_image_dimensions(image_path):
with Image.open(image_path) as img:
width, height = img.size
return width + height
def hide_file_in_png(image_path, file_to_hide, output_image_path, public_key_path):
# Load the public key
with open(public_key_path, 'rb') as key_file:
public_key = serialization.load_pem_public_key(
key_file.read(),
backend=default_backend()
)
# Use the sum of the image dimensions as the seed
seed = compute_seed_from_image_dimensions(image_path)
prng = random.Random(seed) # Create a new instance of a random number generator
# Read the original image
img = Image.open(image_path)
# Check if the image is in a mode that can be converted to RGB or RGBA
if img.mode not in ['RGB', 'RGBA', 'P', 'L']:
raise ValueError("Image mode must be RGB, RGBA, P (palette-based), or L (grayscale).")
# Convert to RGB if it's P or L mode (palette-based or grayscale)
if img.mode == 'P' or img.mode == 'L':
img = img.convert('RGB')
# Convert to RGBA if not already in that format
if img.mode != 'RGBA':
img = img.convert('RGBA')
# This will give you the original format of the image
host_format = img.format
# If the format is None, try to determine it from the file extension
if host_format is None:
file_extension = os.path.splitext(image_path)[1].lower()
extension_to_format = {
'.tga': 'TGA',
'.png': 'PNG',
'.bmp': 'BMP',
'.tif': 'TIFF',
'.tiff': 'TIFF',
}
host_format = extension_to_format.get(file_extension)
supported_formats = {'TGA', 'TIFF', 'BMP', 'PNG'}
if host_format not in supported_formats:
raise ValueError(f"Unsupported image format: {host_format}")
pixels = np.array(img)
# Read the file to hide
with open(file_to_hide, 'rb') as f:
file_bytes = f.read()
# Compress the file
compressed_data = zlib.compress(file_bytes)
# Encrypt the compressed data
encrypted_session_key, salt, iv, encrypted_data = encrypt_data(compressed_data, public_key)
# Get the filename to store
filename = os.path.basename(file_to_hide).encode()
filename_size = len(filename)
# Concatenate the encrypted session key, salt, iv, and encrypted data
data_to_encode = (filename_size.to_bytes(4, 'big') + filename +
encrypted_session_key + salt + iv + encrypted_data)
# Calculate the number of pixels needed
file_size = len(data_to_encode)
num_pixels_required = file_size * 8 # 8 bits per byte
if num_pixels_required > pixels.size // 4: # Divide by 4 for RGBA channels
raise ValueError("Image is not large enough to hide the file.")
# Generate a list of unique indices to hide the data
pixel_indices = list(range(pixels.size // 4))
prng.shuffle(pixel_indices) # Shuffle using the seeded PRNG
# Embed the file size in the first 64 pixels (8 bytes for file size)
for i in range(64):
idx = pixel_indices[i]
bit = (file_size >> (63 - i)) & 0x1
if (pixels[idx // pixels.shape[1], idx % pixels.shape[1], 0] & 0x1) != bit:
pixels[idx // pixels.shape[1], idx % pixels.shape[1], 0] ^= 0x1
# Embed each bit of the data to encode in the image using LSB matching
for i, byte in enumerate(data_to_encode):
for bit in range(8):
idx = pixel_indices[64 + i * 8 + bit]
if (pixels[idx // pixels.shape[1], idx % pixels.shape[1], 0] & 0x1) != ((byte >> (7 - bit)) & 0x1):
pixels[idx // pixels.shape[1], idx % pixels.shape[1], 0] ^= 0x1
# Check if the file already exists and prompt the user
if os.path.exists(output_image_path):
overwrite = input(f"The file '{output_image_path}' already exists. Overwrite? (y/n): ").lower()
if overwrite != 'y':
print("Extraction cancelled.")
return
# Save the new image
new_img = Image.fromarray(pixels, 'RGBA')
if host_format == 'PNG':
new_img.save(output_image_path, format='PNG', optimize=True)
elif host_format == 'BMP':
new_img.save(output_image_path, format='BMP', optimize=True)
elif host_format == 'TGA':
new_img.save(output_image_path, format='TGA', optimize=True)
elif host_format == 'TIFF':
new_img.save(output_image_path, format='TIFF', optimize=True)
else:
# If the format is not one of the supported/expected formats, raise an error.
raise ValueError(f"Unsupported image format: {host_format}")
print(f"File '{file_to_hide}' has been successfully hidden in '{output_image_path}'.")
def extract_file_from_png(image_path, output_file_path, private_key_path):
# Load the private key
passphrase = getpass("Enter the private key passphrase: ")
with open(private_key_path, 'rb') as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=passphrase.encode(),
backend=default_backend()
)
# Determine the size of the encrypted session key based on the private key size
encrypted_session_key_size = private_key.key_size // 8
# Use the sum of the image dimensions as the seed
seed = compute_seed_from_image_dimensions(image_path)
prng = random.Random(seed) # Create a new instance of a random number generator
# Read the steganographed image
img = Image.open(image_path)
if img.mode not in ['RGB', 'RGBA']:
raise ValueError("Image must be in RGB or RGBA format.")
# Convert to RGBA if not already in that format
if img.mode != 'RGBA':
img = img.convert('RGBA')
pixels = np.array(img)
# Flatten the image array for easier processing
flat_pixels = pixels.flatten()
# Use only the red channel for RGBA
channel_multiplier = 4
# Extract the file size from the first 64 pixels
file_size = 0
for i in range(64):
file_size = (file_size << 1) | (flat_pixels[i * channel_multiplier] & 0x1)
# Calculate the number of bytes that can be extracted
num_bytes_to_extract = file_size
# Prepare a list to store the extracted bytes
extracted_bytes = []
# Generate a list of unique indices to extract the data
pixel_indices = list(range(pixels.size // 4))
prng.shuffle(pixel_indices) # Shuffle using the seeded PRNG
# Extract the file size from the first 64 pixels
file_size = 0
for i in range(64):
idx = pixel_indices[i]
file_size = (file_size << 1) | (pixels[idx // pixels.shape[1], idx % pixels.shape[1], 0] & 0x1)
# Calculate the number of bytes that can be extracted
num_bytes_to_extract = file_size
# Extract the hidden bits and reconstruct the bytes using the same indices
extracted_bytes = []
for i in range(num_bytes_to_extract):
byte = 0
for bit in range(8):
idx = pixel_indices[64 + i * 8 + bit]
byte = (byte << 1) | (pixels[idx // pixels.shape[1], idx % pixels.shape[1], 0] & 0x1)
extracted_bytes.append(byte)
# Convert the extracted bytes to a byte array
data_to_decode = bytes(extracted_bytes)
# Extract the filename size and filename
filename_size = int.from_bytes(data_to_decode[:4], 'big')
filename = data_to_decode[4:4 + filename_size].decode()
# Extract the session key, salt, iv, and encrypted data
offset = 4 + filename_size
encrypted_session_key = data_to_decode[offset:offset + encrypted_session_key_size]
salt = data_to_decode[offset + encrypted_session_key_size:offset + encrypted_session_key_size + 16]
iv = data_to_decode[offset + encrypted_session_key_size + 16:offset + encrypted_session_key_size + 32]
encrypted_data = data_to_decode[offset + encrypted_session_key_size + 32:]
# Decrypt the data
decrypted_data = decrypt_data(encrypted_session_key, salt, iv, encrypted_data, private_key)
# Decompress the decrypted data
decompressed_data = zlib.decompress(decrypted_data)
# If no output file path is provided, use the extracted filename
if not output_file_path:
output_file_path = os.path.join(os.getcwd(), filename)
# Check if the file already exists and prompt the user
if os.path.exists(output_file_path):
overwrite = input(f"The file '{output_file_path}' already exists. Overwrite? (y/n): ").lower()
if overwrite != 'y':
print("Extraction cancelled.")
return
# Write the decompressed data to the output file
with open(output_file_path, 'wb') as f:
f.write(decompressed_data)
print(f"File extracted to {output_file_path}")
def main():
parser = argparse.ArgumentParser(description='SecretPixel - Advanced Steganography Tool', epilog="Example commands:\n"
" Hide: python secret_pixel.py hide host.png secret.txt mypublickey.pem output.png\n"
" Extract: python secret_pixel.py extract carrier.png myprivatekey.pem [extracted.txt]",
formatter_class=argparse.RawDescriptionHelpFormatter)
subparsers = parser.add_subparsers(dest='command')
# Subparser for hiding a file
hide_parser = subparsers.add_parser('hide', help='Hide a file inside an image', epilog="Example: python secret_pixel.py hide host.png secret.txt mypublickey.pem output.png", formatter_class=argparse.RawDescriptionHelpFormatter)
hide_parser.add_argument('host', type=str, help='Path to the host image')
hide_parser.add_argument('secret', type=str, help='Path to the secret file to hide')
hide_parser.add_argument('pubkey', type=str, help='Path to the public key for encryption')
hide_parser.add_argument('output', type=str, help='Path to the output image with embedded data')
# Subparser for extracting a file
extract_parser = subparsers.add_parser('extract', help='Extract a file from an image', epilog="Example: python secret_pixel.py extract carrier.png myprivatekey.pem [extracted.txt]",
formatter_class=argparse.RawDescriptionHelpFormatter)
extract_parser.add_argument('carrier', type=str, help='Path to the image with embedded data')
extract_parser.add_argument('privkey', type=str, help='Path to the private key for decryption')
extract_parser.add_argument('extracted', nargs='?', type=str, default=None, help='Path to save the extracted secret file (optional, defaults to the original filename)')
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
args = parser.parse_args()
if args.command == 'hide':
hide_file_in_png(args.host, args.secret, args.output, args.pubkey)
elif args.command == 'extract':
# If no output file path is provided, use None to trigger default behavior
output_file_path = args.extracted if args.extracted else None
extract_file_from_png(args.carrier, output_file_path, args.privkey)
else:
parser.print_help()
if __name__ == '__main__':
main()