2015-10-03 23:29:52 +00:00
|
|
|
'''
|
|
|
|
For each color channel of each pixel in an Image, modify the least significant bit to represent a bit of the Secret file.
|
|
|
|
This changes the RGB value of the pixel by a usually-imperceptible amount.
|
|
|
|
The first 32 bits (10.66 pixels) will be used to store the length of the Secret content in big endian.
|
|
|
|
Then, the Secret's extension is stored. A null byte indicates the end of the extension. This section is of variable length.
|
2015-10-04 03:57:26 +00:00
|
|
|
A file with no extension requires only that null byte. A file with an extension requires 1 additional byte per character.
|
2015-10-03 23:29:52 +00:00
|
|
|
|
2015-10-09 21:57:03 +00:00
|
|
|
Smallest image possible = 16 pixels with 48 bit secret: 32 for header; 8 for null extension; 8 for data.
|
|
|
|
Each Image pixel holds 3 Secret bits, so the Image must have at least ((secretbytes * (8 / 3)) + 14) pixels.
|
2015-10-04 03:57:26 +00:00
|
|
|
An Image can hold ((3 * (pixels - 14)) / 8) Secret bytes.
|
2015-10-03 23:29:52 +00:00
|
|
|
|
|
|
|
Usage:
|
2015-10-09 21:57:03 +00:00
|
|
|
> steganographic.py encode imagefilename.png secretfilename.ext
|
|
|
|
> steganographic.py decode lacedimagename.png
|
2015-10-04 03:57:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
Reference table for files with NO EXTENSION.
|
|
|
|
For each extension character, subtract 1 byte from secret size
|
|
|
|
|
2015-10-07 04:45:38 +00:00
|
|
|
pixels | example dimensions | Secret file size
|
|
|
|
100 | 10 x 10 | 32 bytes
|
|
|
|
400 | 20 x 20 | 144 bytes
|
|
|
|
2,500 | 50 x 50 | 932 bytes
|
|
|
|
10,000 | 100 x 100 | 3,744 bytes
|
|
|
|
40,000 | 200 x 200 | 14,994 bytes
|
|
|
|
25,000 | 500 x 500 | 93,744 bytes (91 kb)
|
|
|
|
1,000,000 | 1,000 x 1,000 | 374,994 bytes (366 kb)
|
|
|
|
4,000,000 | 2,000 x 2,000 | 1,499,994 bytes (1.43 mb)
|
|
|
|
25,000,000 | 5,000 x 5,000 | 9,374,994 bytes (8.94 mb)
|
|
|
|
100,000,000 | 10,000 x 10,000 | 37,499,994 bytes (35.7 mb)
|
|
|
|
|
|
|
|
pixels | example dimensions | Secret file size
|
|
|
|
100 | 10 x 10 | 32 bytes
|
|
|
|
697 | 25 x 28 (700) | 256 bytes
|
|
|
|
2,745 | 50 x 55 (2,750) | 1,024 bytes (1 kb)
|
|
|
|
21,860 | 142 x 154 (21,868) | 8,192 bytes (8 kb)
|
|
|
|
87,396 | 230 x 380 (87,400) | 32,768 bytes (32 kb)
|
|
|
|
349,540 | 463 x 755 (349,565) | 131,072 bytes (128 kb)
|
|
|
|
1,398,116 | 1146 x 1120 (1,398,120) | 524,288 bytes (512 kb)
|
|
|
|
2,796,217 | 1621 x 1725 (2,796,225) | 1,048,576 bytes (1 mb)
|
|
|
|
11,184,825 | 3500 x 3200 (11,200,000) | 4,194,304 bytes (4 mb)
|
|
|
|
44,739,257 | 6700 x 6700 (44,890,000) | 16,777,216 bytes (16 mb)
|
|
|
|
89,478,500 | 9500 x 9500 (90,250,000) | 33,554,432 bytes (32 mb)
|
2015-10-04 03:57:26 +00:00
|
|
|
|
2015-10-03 23:29:52 +00:00
|
|
|
'''
|
|
|
|
from PIL import Image
|
|
|
|
import binascii
|
|
|
|
import math
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
# 11 pixels for the secret file size
|
2015-10-04 03:57:26 +00:00
|
|
|
HEADER_SIZE = 11
|
2015-10-03 23:29:52 +00:00
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
FILE_READ_SIZE = 4 * 1024
|
|
|
|
|
2015-10-03 23:29:52 +00:00
|
|
|
class StegError(Exception):
|
|
|
|
pass
|
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
class BitsToImage:
|
|
|
|
def __init__(self, image, bitness):
|
|
|
|
self.image = image
|
|
|
|
self.bitness = bitness
|
|
|
|
self.width = image.size[0]
|
|
|
|
self.pixel_index = -1
|
|
|
|
self.channel_index = 0
|
|
|
|
self.bit_index = self.bitness - 1
|
|
|
|
self.active_pixel = None
|
|
|
|
self.x = 0
|
|
|
|
self.y = 0
|
|
|
|
|
|
|
|
def _write(self, bit):
|
|
|
|
if self.active_pixel is None:
|
|
|
|
self.pixel_index += 1
|
|
|
|
self.channel_index = 0
|
|
|
|
self.bit_index = self.bitness - 1
|
|
|
|
(self.x, self.y) = index_to_xy(self.pixel_index, self.width)
|
|
|
|
self.active_pixel = list(self.image.getpixel((self.x, self.y)))
|
|
|
|
|
|
|
|
channel = self.active_pixel[self.channel_index]
|
|
|
|
channel = set_bit(channel, self.bit_index, int(bit))
|
|
|
|
self.active_pixel[self.channel_index] = channel
|
|
|
|
self.bit_index -= 1
|
|
|
|
|
|
|
|
if self.bit_index < 0:
|
|
|
|
# We have exhausted our bitness for this channel.
|
|
|
|
self.bit_index = self.bitness - 1
|
|
|
|
self.channel_index += 1
|
|
|
|
if self.channel_index == 3:
|
|
|
|
# We have exhausted the channels for this pixel.
|
|
|
|
self.image.putpixel((self.x, self.y), tuple(self.active_pixel))
|
|
|
|
self.active_pixel = None
|
|
|
|
|
|
|
|
def write(self, bits):
|
|
|
|
for bit in bits:
|
|
|
|
self._write(bit)
|
|
|
|
|
|
|
|
|
|
|
|
class ImageToBits:
|
|
|
|
def __init__(self, image, bitness):
|
|
|
|
self.image = image
|
2016-01-24 20:48:39 +00:00
|
|
|
self.bitness = bitness
|
2016-01-17 01:43:17 +00:00
|
|
|
self.width = image.size[0]
|
2016-01-24 20:48:39 +00:00
|
|
|
self.height = image.size[1]
|
2016-01-17 01:43:17 +00:00
|
|
|
self.pixel_index = -1
|
2016-01-24 20:48:39 +00:00
|
|
|
self.bit_index = bitness
|
2016-01-17 01:43:17 +00:00
|
|
|
self.active_byte = []
|
2016-01-24 20:48:39 +00:00
|
|
|
self.pixels = self.image.getdata()
|
|
|
|
#self.bits = ''
|
|
|
|
#for pixel in self.pixels:
|
|
|
|
# for channel in pixel:
|
|
|
|
# self.bits += binary(channel)[-bitness:]
|
|
|
|
#print(len(self.bits))
|
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
|
|
|
|
def _read(self):
|
|
|
|
if len(self.active_byte) == 0:
|
|
|
|
self.pixel_index += 1
|
2016-01-24 20:48:39 +00:00
|
|
|
self.active_byte = self.pixels[self.pixel_index]
|
2016-01-17 01:43:17 +00:00
|
|
|
self.active_byte = self.active_byte[:3]
|
|
|
|
self.active_byte = [binary(channel) for channel in self.active_byte]
|
|
|
|
self.active_byte = [channel[-bitness:] for channel in self.active_byte]
|
|
|
|
self.active_byte = ''.join(self.active_byte)
|
|
|
|
self.active_byte = list(self.active_byte)
|
|
|
|
|
|
|
|
ret = self.active_byte.pop(0)
|
2016-01-24 20:48:39 +00:00
|
|
|
self.bit_index += 1
|
2016-01-17 01:43:17 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def read(self, bits=1):
|
|
|
|
return ''.join(self._read() for x in range(bits))
|
|
|
|
|
|
|
|
|
2015-10-03 23:29:52 +00:00
|
|
|
def binary(i):
|
|
|
|
return bin(i)[2:].rjust(8, '0')
|
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
def chunk_iterable(iterable, chunk_length, allow_incomplete=True):
|
2015-10-03 23:29:52 +00:00
|
|
|
'''
|
2016-01-17 01:43:17 +00:00
|
|
|
Given an iterable, divide it into chunks of length `chunk_length`.
|
|
|
|
If `allow_incomplete` is True, the final element of the returned list may be shorter
|
|
|
|
than `chunk_length`. If it is False, those items are discarded.
|
2015-10-03 23:29:52 +00:00
|
|
|
'''
|
2016-01-17 01:43:17 +00:00
|
|
|
if len(iterable) % chunk_length != 0 and allow_incomplete:
|
|
|
|
overflow = 1
|
|
|
|
else:
|
|
|
|
overflow = 0
|
|
|
|
|
|
|
|
steps = (len(iterable) // chunk_length) + overflow
|
|
|
|
return [iterable[chunk_length * x : (chunk_length * x) + chunk_length] for x in range(steps)]
|
|
|
|
|
|
|
|
def index_to_xy(index, width):
|
|
|
|
x = index % width
|
|
|
|
y = index // width
|
|
|
|
return (x, y)
|
2015-10-03 23:29:52 +00:00
|
|
|
|
2015-10-04 03:57:26 +00:00
|
|
|
def bytes_to_pixels(bytes):
|
|
|
|
return ((bytes * (8 / 3)) + 14)
|
|
|
|
|
|
|
|
def pixels_to_bytes(pixels):
|
|
|
|
return ((3 * (pixels - 14)) / 8)
|
2015-10-03 23:29:52 +00:00
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
def set_bit(number, index, newvalue):
|
|
|
|
# Thanks unwind
|
|
|
|
# http://stackoverflow.com/a/12174051/5430534
|
|
|
|
mask = 1 << index
|
|
|
|
number &= ~mask
|
|
|
|
if newvalue:
|
|
|
|
number |= mask
|
|
|
|
return number
|
|
|
|
|
2015-10-03 23:29:52 +00:00
|
|
|
############## #### #### ######## ###### ########## ##############
|
|
|
|
#### ## #### #### #### #### #### #### #### #### #### ##
|
|
|
|
#### ###### #### #### #### #### #### #### #### ####
|
|
|
|
#### ## ######## #### #### #### #### #### #### #### ##
|
|
|
|
########## ############## #### #### #### #### #### ##########
|
|
|
|
#### ## #### ######## #### #### #### #### #### #### ##
|
|
|
|
#### #### ###### #### #### #### #### #### #### ####
|
|
|
|
#### ## #### #### #### #### #### #### #### #### #### ##
|
|
|
|
############## #### #### ######## ###### ########## ##############
|
2016-01-17 01:43:17 +00:00
|
|
|
def encode(imagefilename, secretfilename, bitness=1):
|
2015-10-03 23:29:52 +00:00
|
|
|
pixel_index = 0
|
|
|
|
channel_index = 0
|
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
if bitness < 1:
|
|
|
|
raise ValueError('Cannot modify less than 1 bit per channel')
|
|
|
|
if bitness > 8:
|
|
|
|
raise ValueError('Cannot modify more than 8 bits per channel')
|
2015-10-04 03:57:26 +00:00
|
|
|
|
2015-10-03 23:29:52 +00:00
|
|
|
print('Hiding "%s" within "%s"' % (secretfilename, imagefilename))
|
2016-01-17 01:43:17 +00:00
|
|
|
secret_size = os.path.getsize(secretfilename)
|
|
|
|
if secret_size == 0:
|
|
|
|
raise StegError('The Secret can\'t be 0 bytes.')
|
|
|
|
|
2015-10-03 23:29:52 +00:00
|
|
|
image = Image.open(imagefilename)
|
2016-01-17 01:43:17 +00:00
|
|
|
image_steg = BitsToImage(image, bitness)
|
2015-10-03 23:29:52 +00:00
|
|
|
|
2016-05-21 19:51:36 +00:00
|
|
|
total_pixels = image.size[0] * image.size[1]
|
|
|
|
if total_pixels < HEADER_SIZE:
|
2015-10-03 23:29:52 +00:00
|
|
|
raise StegError('Image cannot have fewer than %d pixels. They are used to store Secret\'s length' % HEADER_SIZE)
|
|
|
|
|
|
|
|
secret_extension = os.path.splitext(secretfilename)[1][1:]
|
2016-01-17 01:43:17 +00:00
|
|
|
secret_content_length = (secret_size) + (len(secret_extension)) + 1
|
|
|
|
requiredpixels = math.ceil(((secret_content_length * 8) + 32) / (3 * bitness))
|
2016-05-21 19:51:36 +00:00
|
|
|
if total_pixels < requiredpixels:
|
2016-01-24 20:48:39 +00:00
|
|
|
raise StegError('Image does not have enough pixels to store the Secret. '
|
2015-10-03 23:29:52 +00:00
|
|
|
'Must have at least %d pixels' % requiredpixels)
|
|
|
|
|
2016-05-21 19:51:36 +00:00
|
|
|
print('%d pixels available, %d required' % (total_pixels, requiredpixels))
|
2015-10-03 23:29:52 +00:00
|
|
|
|
|
|
|
# --> YOU ARE HERE <--
|
2016-01-17 01:43:17 +00:00
|
|
|
|
2015-10-03 23:29:52 +00:00
|
|
|
pixel = list(image.getpixel((0, 0)))
|
|
|
|
|
|
|
|
# Write secret length
|
2015-10-07 04:45:38 +00:00
|
|
|
secret_content_length_b = binary(secret_content_length).rjust(32, '0')
|
2016-01-17 01:43:17 +00:00
|
|
|
print('Content bytes:', secret_content_length)
|
|
|
|
image_steg.write(secret_content_length_b)
|
2015-10-03 23:29:52 +00:00
|
|
|
|
|
|
|
# Write the secret extension
|
|
|
|
for character in (secret_extension + chr(0)):
|
2016-01-17 01:43:17 +00:00
|
|
|
image_steg.write(binary(ord(character)))
|
2015-10-03 23:29:52 +00:00
|
|
|
|
|
|
|
# Write the secret data
|
2016-01-17 01:43:17 +00:00
|
|
|
bytes_written = 0
|
2016-05-21 19:51:36 +00:00
|
|
|
one_complete_file = False
|
|
|
|
|
|
|
|
# Yes, we're assuming the whole file fits in memory.
|
|
|
|
secret_file = open(secretfilename, 'rb')
|
|
|
|
secret_data = secret_file.read()
|
|
|
|
secret_data_size = len(secret_data)
|
|
|
|
secret_index = 0
|
|
|
|
for pixel_number in range(total_pixels):
|
|
|
|
if pixel_number % 4 == 0:
|
|
|
|
#percentage = (bytes_written + 1) / secret_size
|
|
|
|
#percentage = '%07.3f%%\r' % (100 * percentage)
|
|
|
|
#print(percentage, end='')
|
|
|
|
print(pixel_number)
|
|
|
|
|
|
|
|
secret_chunk = secret_data[secret_index:secret_index+FILE_READ_SIZE]
|
|
|
|
secret_index += FILE_READ_SIZE
|
|
|
|
if len(secret_chunk) < FILE_READ_SIZE:
|
|
|
|
one_complete_file = True
|
|
|
|
#secret_index = 0
|
|
|
|
rs = FILE_READ_SIZE - len(secret_chunk)
|
|
|
|
secret_chunk += os.urandom(rs)
|
|
|
|
#secret_chunk += secret_data[secret_index:secret_index+rs]
|
|
|
|
|
|
|
|
secret_chunk = list(secret_chunk)
|
|
|
|
secret_chunk = [binary(byte) for byte in secret_chunk]
|
|
|
|
bytes_written += len(secret_chunk)
|
|
|
|
secret_chunk = ''.join(secret_chunk)
|
|
|
|
try:
|
|
|
|
image_steg.write(secret_chunk)
|
|
|
|
except IndexError:
|
|
|
|
if one_complete_file:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise
|
2016-01-17 01:43:17 +00:00
|
|
|
|
|
|
|
# haha
|
|
|
|
print('100.000%')
|
2015-10-03 23:29:52 +00:00
|
|
|
|
|
|
|
if channel_index != 0:
|
|
|
|
# The Secret data has finished, but we still have an unsaved pixel
|
|
|
|
# (because channel_index is set to 0 when we save the active pixel above)
|
2016-01-17 01:43:17 +00:00
|
|
|
(x, y) = index_to_xy(pixel_index, image.size[0])
|
|
|
|
image.putpixel((x, y), tuple(pixel))
|
2015-10-03 23:29:52 +00:00
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
new_name = os.path.splitext(imagefilename)[0]
|
|
|
|
original_name = os.path.basename(secretfilename).replace('.', '_')
|
|
|
|
newname = '%s (%s) (%d).png' % (new_name, original_name, bitness)
|
|
|
|
print('Writing:', newname)
|
2015-10-03 23:29:52 +00:00
|
|
|
image.save(newname)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########## ############## ######## ###### ########## ##############
|
|
|
|
#### #### #### ## #### #### #### #### #### #### #### ##
|
|
|
|
#### #### #### #### #### #### #### #### #### ####
|
|
|
|
#### #### #### ## #### #### #### #### #### #### ##
|
|
|
|
#### #### ########## #### #### #### #### #### ##########
|
|
|
|
#### #### #### ## #### #### #### #### #### #### ##
|
|
|
|
#### #### #### #### #### #### #### #### #### ####
|
|
|
|
#### #### #### ## #### #### #### #### #### #### #### ##
|
|
|
|
########## ############## ######## ###### ########## ##############
|
2016-01-17 01:43:17 +00:00
|
|
|
def decode(imagefilename, bitness=1):
|
2015-10-03 23:29:52 +00:00
|
|
|
print('Extracting content from "%s"' % imagefilename)
|
|
|
|
image = Image.open(imagefilename)
|
2016-01-17 01:43:17 +00:00
|
|
|
image_steg = ImageToBits(image, bitness)
|
2015-10-03 23:29:52 +00:00
|
|
|
|
|
|
|
# determine the content length
|
2016-01-17 01:43:17 +00:00
|
|
|
content_length = image_steg.read(32)
|
2015-10-03 23:29:52 +00:00
|
|
|
content_length = int(content_length, 2)
|
2015-10-07 04:45:38 +00:00
|
|
|
print('Content bytes:', content_length)
|
2015-10-03 23:29:52 +00:00
|
|
|
|
|
|
|
# determine secret extension
|
|
|
|
extension = ''
|
|
|
|
while extension[-8:] != '00000000' or len(extension) % 8 != 0:
|
2016-01-17 01:43:17 +00:00
|
|
|
extension += image_steg.read()
|
|
|
|
|
|
|
|
extension = chunk_iterable(extension, 8)
|
|
|
|
extension.remove('00000000')
|
2015-10-03 23:29:52 +00:00
|
|
|
extension = [chr(int(x, 2)) for x in extension]
|
|
|
|
extension = ''.join(extension)
|
|
|
|
print('Extension:', extension)
|
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
# Remove the extension length, and null byte
|
2015-10-07 04:45:38 +00:00
|
|
|
content_length -= 1
|
|
|
|
content_length -= len(extension)
|
2015-10-03 23:29:52 +00:00
|
|
|
|
|
|
|
# Prepare writes
|
|
|
|
newname = os.path.splitext(imagefilename)[0]
|
|
|
|
if extension != '':
|
|
|
|
extension = '.' + extension
|
|
|
|
newname = '%s (extracted)%s' % (newname, extension)
|
|
|
|
outfile = open(newname, 'wb')
|
|
|
|
|
|
|
|
# extract data
|
2016-01-17 01:43:17 +00:00
|
|
|
bytes_written = 0
|
|
|
|
while bytes_written < content_length:
|
|
|
|
if bytes_written % 1024 == 0:
|
|
|
|
percentage = (bytes_written + 1) / content_length
|
2015-10-03 23:29:52 +00:00
|
|
|
percentage = '%07.3f%%\r' % (100 * percentage)
|
|
|
|
print(percentage, end='')
|
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
byte = image_steg.read(8)
|
|
|
|
byte = '%02x' % int(byte, 2)
|
|
|
|
outfile.write(binascii.a2b_hex(byte))
|
|
|
|
bytes_written += 1
|
|
|
|
|
|
|
|
# I'm on fire
|
|
|
|
print('100.000%')
|
|
|
|
print('Wrote', newname)
|
2015-10-03 23:29:52 +00:00
|
|
|
outfile.close()
|
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
def listget(li, index, fallback=None):
|
|
|
|
try:
|
|
|
|
return li[index]
|
|
|
|
except IndexError:
|
|
|
|
return fallback
|
|
|
|
|
2015-10-03 23:29:52 +00:00
|
|
|
if __name__ == '__main__':
|
2016-01-17 01:43:17 +00:00
|
|
|
command = listget(sys.argv, 1, '').lower()
|
|
|
|
if command not in ['encode', 'decode']:
|
2015-10-03 23:29:52 +00:00
|
|
|
print('Usage:')
|
2016-05-21 19:51:36 +00:00
|
|
|
print('> steganographic.py encode imagefilename.png secretfilename.ext bitness')
|
|
|
|
print('> steganographic.py decode lacedimagename.png bitness')
|
2016-01-17 01:43:17 +00:00
|
|
|
quit()
|
2015-10-03 23:29:52 +00:00
|
|
|
|
|
|
|
imagefilename = sys.argv[2]
|
|
|
|
|
2016-01-17 01:43:17 +00:00
|
|
|
if command == 'encode':
|
2015-10-03 23:29:52 +00:00
|
|
|
secretfilename = sys.argv[3]
|
2016-05-21 19:51:36 +00:00
|
|
|
bitness = int(sys.argv[4])
|
2016-01-17 01:43:17 +00:00
|
|
|
encode(imagefilename, secretfilename, bitness)
|
2015-10-03 23:29:52 +00:00
|
|
|
else:
|
2016-05-21 19:51:36 +00:00
|
|
|
bitness = int(sys.argv[3])
|
2016-01-17 01:43:17 +00:00
|
|
|
decode(imagefilename, bitness)
|