Last active
January 3, 2016 04:59
-
-
Save dangayle/8412202 to your computer and use it in GitHub Desktop.
Python script to encrypt and append a zipfile to an image file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment