Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple steganography in Python
import sys
import string
def main():
if(len(sys.argv) > 2):
encrypt()
else:
decrypt()
def encrypt():
with open(sys.argv[1],'rb') as bmp_file:
bmp = bmp_file.read()
with open(sys.argv[2],'rb') as to_hide_file:
to_hide = to_hide_file.read()
hide_data(bmp,to_hide)
def decrypt():
with open(sys.argv[1],'rb') as bmp_file:
bmp = bmp_file.read()
start_offset = bmp[10] #The byte at position 10 tells us where the color data starts
#Deconstruct each byte and get its final bit
bits = []
for i in range(start_offset,len(bmp)):
bits.append(nth_bit_present(bmp[i],0))
#Combine our bit array into bytes
out_bytes = []
for i in range(0,len(bits),8): #Take each 8-bit chunk
if(len(bits) - i > 8):
out_bytes.append(bits_to_byte(bits[i:i+8]))
#Convert bytes to characters
out = []
for b in out_bytes:
out.append(chr(b))
print(''.join(out))
def hide_data(bmp,data):
start_offset = bmp[10] #The byte at position 10 tells us where the color data starts
bmpa = bytearray(bmp)
data_array = data_to_bits(data)
#We need to make sure there is enough space to hide our data
assert len(data_array) < len(bmpa) + start_offset
for i in range(len(data_array)):
bmpa[i + start_offset] = set_final_bit(bmpa[i + start_offset],data_array[i])
write_hidden_bmp(bmpa)
def data_to_bits(data):
bits = []
for i in range(len(data)):
#A byte can at max be 8 digits long, i.e. 0b11111111 = 255
#We start at the left most bit (position 7) and work down to 0
for j in range(7,-1,-1):
#Create the logic array of bits for our data
bits.append(nth_bit_present(data[i],j))
return bits
def set_final_bit(my_byte,ends_in_one):
new_byte = 0
if ends_in_one:
if(nth_bit_present(my_byte,0)):
new_byte = my_byte #No modification needed, it already ends in one
else:
new_byte = my_byte + 1
else:
if(nth_bit_present(my_byte,0)):
new_byte = my_byte - 1
else:
new_byte = my_byte #No modification needed, it already ends in zero
return new_byte
def nth_bit_present(my_byte,n):
#Bitwise check to see what the nth bit is
#If we get anything other than 0, it is TRUE else FALSE
return (my_byte & (1 << n)) != 0
def bits_to_byte(bits):
assert len(bits) == 8
new_byte = 0
for i in range(8):
if bits[i]==True:
#This bit==1 and the "position" we are at in the byte is 7-i
#Bitwise OR will insert a 1 a this position
new_byte |= 1 << 7 - i
else:
#This bit==0 and the "position" we are at in the byte is 7-i
#Bitwise OR will insert a 0 a this position
new_byte |= 0 << 7 - i
return new_byte
def write_hidden_bmp(bytes_to_write):
with open(sys.argv[1].replace('.bmp','_hidden.bmp'),'wb') as out:
out.write(bytes_to_write)
if __name__=="__main__":
main()
The MIT License (MIT)
Copyright (c) 2014 Phillip Johnson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment