Possibility to hide binary file. Better command line.

This commit is contained in:
cedricbonhomme 2010-10-06 08:37:38 +02:00
parent 5212a6afe1
commit 12497ce88d
2 changed files with 42 additions and 18 deletions

View file

@ -47,8 +47,8 @@ def hide(img, message):
if len(message_bits) > npixels * 3: if len(message_bits) > npixels * 3:
return """Too long message (%s > %s).""" % (len(message_bits), npixels * 3) return """Too long message (%s > %s).""" % (len(message_bits), npixels * 3)
for row in range(height): for row in xrange(height):
for col in range(width): for col in xrange(width):
if index + 3 <= len(message_bits) : if index + 3 <= len(message_bits) :
@ -75,8 +75,8 @@ def reveal(img):
width, height = img.size width, height = img.size
buff, count = 0, 0 buff, count = 0, 0
bitab = [] bitab = []
for row in range(height): for row in xrange(height):
for col in range(width): for col in xrange(width):
r, g, b = img.getpixel((col, row)) r, g, b = img.getpixel((col, row))
buff += (r&1)<<(7-count) buff += (r&1)<<(7-count)
@ -108,8 +108,8 @@ def reveal_slow(img):
""" """
width, height = img.size width, height = img.size
bits = [] bits = []
for row in range(height): for row in xrange(height):
for col in range(width): for col in xrange(width):
r, g, b = img.getpixel((col, row)) r, g, b = img.getpixel((col, row))
bits.append([tools.bs(r)[-1], tools.bs(g)[-1], tools.bs(b)[-1]]) bits.append([tools.bs(r)[-1], tools.bs(g)[-1], tools.bs(b)[-1]])
@ -128,21 +128,39 @@ if __name__ == '__main__':
from optparse import OptionParser from optparse import OptionParser
usage = "usage: %prog hide|reveal [options]" usage = "usage: %prog hide|reveal [options]"
parser = OptionParser(usage) parser = OptionParser(usage)
# Original image
parser.add_option("-i", "--input", dest="input_image_file", parser.add_option("-i", "--input", dest="input_image_file",
help="Image file") help="Input image fil.e")
# Image containing the secret
parser.add_option("-o", "--output", dest="output_image_file", parser.add_option("-o", "--output", dest="output_image_file",
help="Image file") help="Output image containing the secret.")
parser.add_option("-s", "--secret", dest="secret",
help="Your secret (Message, Image, Music or any binary file.)") # Non binary secret message to hide
parser.add_option("-m", "--secret-message", dest="secret_message",
help="Your secret message to hide(non binary).")
# Binary secret to hide (OGG, executable, etc.)
parser.add_option("-f", "--secret-file", dest="secret_file",
help="Your secret to hide (Text or any binary file).")
# Output for the binary binary secret.
parser.add_option("-b", "--binary", dest="secret_binary",
help="Output for the binary secret (Text or any binary file).")
parser.set_defaults(input_image_file = './pictures/Lenna.png', parser.set_defaults(input_image_file = './pictures/Lenna.png',
output_image_file = './pictures/Lenna_enc.png', output_image_file = './pictures/Lenna_enc.png',
secret = 'Hello World!') secret_message = '', secret_file = '', secret_binary = "")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if sys.argv[1] == "hide": if sys.argv[1] == "hide":
if options.secret_message != "" and options.secret_file == "":
secret = options.secret_message
elif options.secret_message == "" and options.secret_file =! "":
secret = tools.binary2base64(options.secret_file)
img = Image.open(options.input_image_file) img = Image.open(options.input_image_file)
img_encoded = hide(img, options.secret) img_encoded = hide(img, secret)
try: try:
img_encoded.save(options.output_image_file) img_encoded.save(options.output_image_file)
except Exception, e: except Exception, e:
@ -151,4 +169,10 @@ if __name__ == '__main__':
elif sys.argv[1] == "reveal": elif sys.argv[1] == "reveal":
img = Image.open(options.input_image_file) img = Image.open(options.input_image_file)
print reveal(img) secret = reveal(img)
if options.secret_binary != "":
data = base64.b64decode(secret)
with open(options.secret_binary, "w") as f:
f.write(data)
else:
print secret

View file

@ -81,7 +81,7 @@ def n_at_a_time(items, n, fillvalue):
def binary2base64(binary_file, output_file): def binary2base64(binary_file, output_file):
""" """
Convert a binary file (OGG, executable, etc.) to a Convert a binary file (OGG, executable, etc.) to a
printable file. printable string.
""" """
# Use mode = "rb" to read binary file # Use mode = "rb" to read binary file
fin = open(binary_file, "rb") fin = open(binary_file, "rb")
@ -89,11 +89,11 @@ def binary2base64(binary_file, output_file):
fin.close() fin.close()
# Encode binary to base64 string (printable) # Encode binary to base64 string (printable)
b64_data = base64.b64encode(binary_data) return b64_data = base64.b64encode(binary_data)
fout = open(output_file, "w") """fout = open(output_file, "w")
fout.write(b64_data) fout.write(b64_data)
fout.close fout.close"""
def base642binary(b64_fname): def base642binary(b64_fname):
""" """