From 04984d5e88393b9874bacf440059a760273d2615 Mon Sep 17 00:00:00 2001 From: cedricbonhomme Date: Tue, 5 Oct 2010 07:25:00 +0200 Subject: [PATCH] Added conversion to base64 file for binary file. --- basic.py | 2 +- lsb-s.py | 10 ++++++---- tools.py | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/basic.py b/basic.py index 1b8e3fb..68ec9fe 100755 --- a/basic.py +++ b/basic.py @@ -65,7 +65,7 @@ if __name__ == '__main__': encoded_image_file = "./pictures/Lenna_enc.png" # at this point don't exceed 255 characters secret_message = "Parce que je le vaut bien!" - + img1 = Image.open(original_image_file) img_encoded = hide(img1, secret_message) diff --git a/lsb-s.py b/lsb-s.py index c8a9302..65f9d8a 100755 --- a/lsb-s.py +++ b/lsb-s.py @@ -19,10 +19,12 @@ def hide(img, message): index = 0 message = message + '~~~' - message_bits = tools.a2bits(message) + #message_bits = tools.a2bits(message) + message_bits = "".join(tools.a2bits_list(message)) npixels = width * height if len(message_bits) > npixels * 3: + print """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): @@ -70,10 +72,10 @@ def reveal(img): if __name__ == '__main__': # Point of entry in execution mode - original_image_file = "./pictures/montenach.png" - encoded_image_file = "./pictures/montenach_enc.png" + original_image_file = "./pictures/free.png" + encoded_image_file = "./pictures/free_enc.png" secret_message = "Avec la technique LSB (Least Significant Bit) l'oeil humain (un normal ;-)) ne voit plus la difference" - with open("./lorem_ipsum.txt", "r") as f: + with open("./Portishead_-_Deep_Water.ogg_b64.txt", "r") as f: secret_message = f.read() img1 = Image.open(original_image_file) diff --git a/tools.py b/tools.py index a7f92d5..cc152b4 100755 --- a/tools.py +++ b/tools.py @@ -2,11 +2,10 @@ # -*- coding: utf-8 -*- - def a2bits(chars): """ Converts a string to its bits representation as a string of 0's and 1's. - + >>> a2bits("Hello World!") '010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001' """ @@ -15,7 +14,7 @@ def a2bits(chars): def a2bits_list(chars): """ Convert a string to its bits representation as a list of 0's and 1's. - + >>> a2bits_list("Hello World!") ['01001000', '01100101', @@ -50,9 +49,35 @@ def n_at_a_time(items, n, fillvalue): """ Returns an iterator which groups n items at a time. Any final partial tuple will be padded with the fillvalue - + >>> list(n_at_a_time([1, 2, 3, 4, 5], 2, 'X')) [(1, 2), (3, 4), (5, 'X')] """ it = iter(items) - return its.izip_longest(*[it] * n, fillvalue=fillvalue) \ No newline at end of file + return its.izip_longest(*[it] * n, fillvalue=fillvalue) + +def binary2base64(binary_file, output_file): + """ + """ + # use mode = "rb" to read binary file + fin = open(binary_file, "rb") + binary_data = fin.read() + fin.close() + + # encode binary to base64 string (printable) + b64_data = base64.b64encode(binary_data) + + fout = open(output_file, "w") + fout.write(b64_data) + fout.close + +def base642binary(b64_fname): + """ + """ + # read base64 string + fin = open(b64_fname, "r") + b64_str = fin.read() + fin.close() + + # decode base64 string to original binary sound object + return base64.b64decode(b64_str) \ No newline at end of file