public
Last active

Python script to encrypt and append a zipfile to an image file.

  • Download Gist
steganography_v1.py
Python
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
'''Hide a zipfile in an image or unpack an image with a hidden archive in it'''
import argparse
from zipfile import ZipFile
def hide(image_file, archive_file):
'''Append an archive onto the end of an image'''
# open the image file in "append binary" mode
with open(image_file, "ab") as image:
# open the archive file in "read only binary" mode
with open(archive_file, "rb") as archive:
# Append a newline and the contents of the archive onto the image
image.write("\n" + archive.read())
def unhide(unhide_file):
'''Unpack a zipfile'''
zip = ZipFile(unhide_file)
zip.extractall()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--hide', nargs=2)
parser.add_argument('-U', '--unhide')
args = parser.parse_args()
if args.hide:
hide(*args.hide)
if args.unhide:
unhide(args.unhide)
steganography_v3.py
Python
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
'''Encrypt and hide a zipfile in an image or unpack an image with a hidden archive in it'''
 
import argparse
from zipfile import ZipFile
import base64
import StringIO
import sys
from getpass import getpass
from bcrypt import gensalt, hashpw
from nacl.secret import SecretBox
import nacl.utils
 
 
def key_32(salt):
'''Returns 32 byte key'''
return hashpw(getpass(), salt)[-32:]
 
 
def hide(image_file, archive_file):
'''Append an encrypted archive onto the end of an image'''
 
salt = gensalt()
box = SecretBox(key_32(salt))
nonce = "dg1" + nacl.utils.random(21)
 
# open the image file in "append binary" mode
with open(image_file, "ab") as image:
# open the archive file in "read only binary" mode
with open(archive_file, "rb") as a:
archive = a.read()
# Append a newline and the encrypted contents of the archive onto the image
image.write("EOF" + box.encrypt(archive, nonce) + "EOF" + salt)
 
 
def unhide(unhide_file):
'''Unpack a zipfile'''
 
with open(unhide_file, "rb") as f:
hidden_file = f.read()
image, encrypted, salt = hidden_file.split("EOF")
box = SecretBox(key_32(salt))
# zipfile needs a file-like object
e_string = StringIO.StringIO()
# decrypt the file
e_string.write(box.decrypt(encrypted))
archive = ZipFile(e_string)
archive.extractall()
sys.stdout.write("File sucessfully extracted\n")
 
 
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--hide', nargs=2)
parser.add_argument('-U', '--unhide')
args = parser.parse_args()
if args.hide:
hide(*args.hide)
if args.unhide:
unhide(args.unhide)
steganograpy_v2.py
Python
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
'''Encrypt and hide a zipfile in an image or unpack an image with a hidden archive in it'''
 
import argparse
from zipfile import ZipFile
import nacl.secret
import nacl.utils
import base64
import StringIO
import getpass
 
 
def key_32():
'''Returns 32 byte key'''
 
BLOCK_SIZE = 32
PADDING = "0"
 
# pad string to 32 byes
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
return pad(base64.b64encode(getpass.getpass("Password? ")))
 
 
def hide(image_file, archive_file):
'''Append an encrypted archive onto the end of an image'''
 
box = nacl.secret.SecretBox(key_32())
nonce = "dg1" + nacl.utils.random(21)
 
# open the image file in "append binary" mode
with open(image_file, "ab") as image:
# open the archive file in "read only binary" mode
with open(archive_file, "rb") as a:
archive = a.read()
# Append a newline and the encrypted contents of the archive onto the image
image.write("EOF" + box.encrypt(archive, nonce))
 
 
def unhide(unhide_file):
'''Unpack a zipfile'''
 
with open(unhide_file, "rb") as f:
hidden_file = f.read()
encrypted = hidden_file.split('EOF')
box = nacl.secret.SecretBox(key_32())
# zipfile needs a file-like object
e_string = StringIO.StringIO()
e_string.write(box.decrypt(encrypted[1]))
zip = ZipFile(e_string)
zip.extractall()
print "files extracted"
 
 
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--hide', nargs=2)
parser.add_argument('-U', '--unhide')
args = parser.parse_args()
if args.hide:
hide(*args.hide)
if args.unhide:
unhide(args.unhide)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.