From 6ad140bdfb64e30a543e3c7d5c43217f2725b6ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bonhomme?= Date: Mon, 13 Mar 2017 09:34:43 +0100 Subject: [PATCH 1/4] Management of unicode. It would be perfect to manage ASCII and unicode in the same time (so 8 bits to 32 bits caracters). --- stegano/lsb/lsb.py | 4 ++-- stegano/lsbset/lsbset.py | 4 ++-- stegano/tools.py | 3 ++- tests/test_lsb.py | 12 ++++++------ 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/stegano/lsb/lsb.py b/stegano/lsb/lsb.py index a2bd745..ff64bdf 100755 --- a/stegano/lsb/lsb.py +++ b/stegano/lsb/lsb.py @@ -106,9 +106,9 @@ def reveal(input_image_file): if img.mode == 'RGBA': pixel = pixel[:3] # ignore the alpha for color in pixel: - buff += (color&1)<<(7-count) + buff += (color&1)<<(31-count) count += 1 - if count == 8: + if count == 32: bitab.append(chr(buff)) buff, count = 0, 0 if bitab[-1] == ":" and limit == None: diff --git a/stegano/lsbset/lsbset.py b/stegano/lsbset/lsbset.py index 0dd72f5..b611d38 100644 --- a/stegano/lsbset/lsbset.py +++ b/stegano/lsbset/lsbset.py @@ -104,9 +104,9 @@ def reveal(input_image_file, generator): generated_number = next(generator) # color = [r, g, b] for color in img_list[generated_number]: - buff += (color&1)<<(7-count) + buff += (color&1)<<(31-count) count += 1 - if count == 8: + if count == 32: bitab.append(chr(buff)) buff, count = 0, 0 if bitab[-1] == ":" and limit == None: diff --git a/stegano/tools.py b/stegano/tools.py index acb9693..3740b18 100755 --- a/stegano/tools.py +++ b/stegano/tools.py @@ -57,7 +57,8 @@ def a2bits_list(chars: str) -> List[str]: >>> "".join(a2bits_list("Hello World!")) '010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001' """ - return [bin(ord(x))[2:].rjust(8,"0") for x in chars] + #return [bin(ord(x))[2:].rjust(8,"0") for x in chars] + return [bin(ord(x))[2:].rjust(32,"0") for x in chars] def bs(s: int) -> str: """Converts an int to its bits representation as a string of 0's and 1's. diff --git a/tests/test_lsb.py b/tests/test_lsb.py index 939f24d..9672267 100644 --- a/tests/test_lsb.py +++ b/tests/test_lsb.py @@ -43,7 +43,7 @@ class TestLSB(unittest.TestCase): secret = lsb.hide("./tests/sample-files/Lenna.png", "") def test_hide_and_reveal(self): - messages_to_hide = ["a", "foo", "Hello World!", ":Python:"] + messages_to_hide = ['πŸ•', 'a', 'foo', 'Hello World!', ':Python:'] for message in messages_to_hide: secret = lsb.hide("./tests/sample-files/Lenna.png", message) secret.save("./image.png") @@ -53,7 +53,7 @@ class TestLSB(unittest.TestCase): self.assertEqual(message, clear_message) def test_with_transparent_png(self): - messages_to_hide = ["a", "foo", "Hello World!", ":Python:"] + messages_to_hide = ['πŸ•', 'a', 'foo', 'Hello World!', ':Python:'] for message in messages_to_hide: secret = lsb.hide("./tests/sample-files/transparent.png", message) secret.save("./image.png") @@ -64,24 +64,24 @@ class TestLSB(unittest.TestCase): @patch('builtins.input', return_value='y') def test_manual_convert_rgb(self, input): - message_to_hide = "I love πŸ• and 🍫!" + message_to_hide = 'I love πŸ• and 🍫!' secret = lsb.hide("./tests/sample-files/Lenna-grayscale.png", message_to_hide) @patch('builtins.input', return_value='n') def test_refuse_convert_rgb(self, input): - message_to_hide = "I love πŸ• and 🍫!" + message_to_hide = 'I love πŸ• and 🍫!' with self.assertRaises(Exception): secret = lsb.hide("./tests/sample-files/Lenna-grayscale.png", message_to_hide) def test_auto_convert_rgb(self): - message_to_hide = "I love πŸ• and 🍫!" + message_to_hide = 'I love πŸ• and 🍫!' secret = lsb.hide("./tests/sample-files/Lenna-grayscale.png", message_to_hide, True) def test_with_text_file(self): - text_file_to_hide = "./tests/sample-files/lorem_ipsum.txt" + text_file_to_hide = './tests/sample-files/lorem_ipsum.txt' with open(text_file_to_hide) as f: message = f.read() secret = lsb.hide("./tests/sample-files/Lenna.png", message) From 4fc8eac172c041f1fa2c659e55942d79eaf1c606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bonhomme?= Date: Mon, 20 Mar 2017 22:53:55 +0100 Subject: [PATCH 2/4] a2bits_list now return a list of 32 bits characters --- setup.py | 2 +- tests/test_tools.py | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/setup.py b/setup.py index 7633f21..4e0cc34 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ with open('CHANGELOG.rst', 'r') as f: setup( name='Stegano', - version='0.6.9', + version='0.6.10', author='CΓ©dric Bonhomme', author_email='cedric@cedricbonhomme.org', packages=packages, diff --git a/tests/test_tools.py b/tests/test_tools.py index 61654c1..520859c 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -39,18 +39,18 @@ class TestTools(unittest.TestCase): def test_a2bits_list(self): list_of_bits = tools.a2bits_list("Hello World!") - self.assertEqual(list_of_bits, ['01001000', - '01100101', - '01101100', - '01101100', - '01101111', - '00100000', - '01010111', - '01101111', - '01110010', - '01101100', - '01100100', - '00100001']) + self.assertEqual(list_of_bits, ['00000000000000000000000001001000', + '00000000000000000000000001100101', + '00000000000000000000000001101100', + '00000000000000000000000001101100', + '00000000000000000000000001101111', + '00000000000000000000000000100000', + '00000000000000000000000001010111', + '00000000000000000000000001101111', + '00000000000000000000000001110010', + '00000000000000000000000001101100', + '00000000000000000000000001100100', + '00000000000000000000000000100001']) def test_n_at_a_time(self): result = tools.n_at_a_time([1, 2, 3, 4, 5], 2, 'X') From 5f5c07493ce6747d465cbb1b71c439607ae6f965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bonhomme?= Date: Thu, 4 May 2017 13:05:56 +0200 Subject: [PATCH 3/4] Introduce a new argument in a2bits_list in order to specify the encoding of the string (unicode) --- stegano/lsb/lsb.py | 16 ++++++++++------ stegano/lsbset/lsbset.py | 14 +++++++++----- stegano/tools.py | 14 +++++++++----- tests/test_lsb.py | 23 ++++++++++++++++------- tests/test_lsbset.py | 21 +++++++++++++++++---- tests/test_tools.py | 17 ++++++++++++++++- 6 files changed, 77 insertions(+), 28 deletions(-) diff --git a/stegano/lsb/lsb.py b/stegano/lsb/lsb.py index ff64bdf..fc4774e 100755 --- a/stegano/lsb/lsb.py +++ b/stegano/lsb/lsb.py @@ -20,8 +20,9 @@ # along with this program. If not, see __author__ = "Cedric Bonhomme" -__version__ = "$Revision: 0.2.2 $" +__version__ = "$Revision: 0.3 $" __date__ = "$Date: 2016/08/04 $" +__revision__ = "$Date: 2017/05/04 $" __license__ = "GPLv3" import sys @@ -30,7 +31,10 @@ from PIL import Image from stegano import tools -def hide(input_image_file: str, message, auto_convert_rgb: bool = False): +def hide(input_image_file: str, + message, + encoding='UTF-8', + auto_convert_rgb: bool = False): """Hide a message (string) in an image with the LSB (Least Significant Bit) technique. """ @@ -53,7 +57,7 @@ def hide(input_image_file: str, message, auto_convert_rgb: bool = False): index = 0 message = str(message_length) + ":" + str(message) - message_bits = "".join(tools.a2bits_list(message)) + message_bits = "".join(tools.a2bits_list(message, encoding)) message_bits += '0' * ((3 - (len(message_bits) % 3)) % 3) npixels = width * height @@ -90,7 +94,7 @@ def hide(input_image_file: str, message, auto_convert_rgb: bool = False): return encoded -def reveal(input_image_file): +def reveal(input_image_file, encoding='UTF-8'): """Find a message in an image (with the LSB technique). """ img = Image.open(input_image_file) @@ -106,9 +110,9 @@ def reveal(input_image_file): if img.mode == 'RGBA': pixel = pixel[:3] # ignore the alpha for color in pixel: - buff += (color&1)<<(31-count) + buff += (color&1)<<(tools.ENCODINGS[encoding]-1 - count) count += 1 - if count == 32: + if count == tools.ENCODINGS[encoding]: bitab.append(chr(buff)) buff, count = 0, 0 if bitab[-1] == ":" and limit == None: diff --git a/stegano/lsbset/lsbset.py b/stegano/lsbset/lsbset.py index b611d38..cac7d32 100644 --- a/stegano/lsbset/lsbset.py +++ b/stegano/lsbset/lsbset.py @@ -32,7 +32,11 @@ from PIL import Image from stegano import tools from . import generators -def hide(input_image_file, message, generator, auto_convert_rgb=False): +def hide(input_image_file, + message, + generator, + encoding='UTF-8', + auto_convert_rgb=False): """Hide a message (string) in an image with the LSB (Least Significant Bit) technique. """ @@ -55,7 +59,7 @@ def hide(input_image_file, message, generator, auto_convert_rgb=False): index = 0 message = str(message_length) + ":" + str(message) - message_bits = "".join(tools.a2bits_list(message)) + message_bits = "".join(tools.a2bits_list(message, encoding)) message_bits += '0' * ((3 - (len(message_bits) % 3)) % 3) npixels = width * height @@ -90,7 +94,7 @@ def hide(input_image_file, message, generator, auto_convert_rgb=False): return encoded -def reveal(input_image_file, generator): +def reveal(input_image_file, generator, encoding='UTF-8'): """Find a message in an image (with the LSB technique). """ img = Image.open(input_image_file) @@ -104,9 +108,9 @@ def reveal(input_image_file, generator): generated_number = next(generator) # color = [r, g, b] for color in img_list[generated_number]: - buff += (color&1)<<(31-count) + buff += (color&1)<<(tools.ENCODINGS[encoding]-1 - count) count += 1 - if count == 32: + if count == tools.ENCODINGS[encoding]: bitab.append(chr(buff)) buff, count = 0, 0 if bitab[-1] == ":" and limit == None: diff --git a/stegano/tools.py b/stegano/tools.py index 3740b18..59c6b33 100755 --- a/stegano/tools.py +++ b/stegano/tools.py @@ -20,9 +20,9 @@ # along with this program. If not, see __author__ = "Cedric Bonhomme" -__version__ = "$Revision: 0.2 $" +__version__ = "$Revision: 0.3 $" __date__ = "$Date: 2010/10/01 $" -__revision__ = "$Date: 2016/08/03 $" +__revision__ = "$Date: 2017/05/04 $" __license__ = "GPLv3" import base64 @@ -30,6 +30,11 @@ import itertools from typing import List, Iterator, Tuple, Union from functools import reduce +ENCODINGS = { + 'UTF-8': 8, + 'UTF-32LE': 32 +} + def a2bits(chars: str) -> str: """Converts a string to its bits representation as a string of 0's and 1's. @@ -38,7 +43,7 @@ def a2bits(chars: str) -> str: """ return bin(reduce(lambda x, y : (x<<8)+y, (ord(c) for c in chars), 1))[3:] -def a2bits_list(chars: str) -> List[str]: +def a2bits_list(chars: str, encoding: str ='UTF-8') -> List[str]: """Convert a string to its bits representation as a list of 0's and 1's. >>> a2bits_list("Hello World!") @@ -57,8 +62,7 @@ def a2bits_list(chars: str) -> List[str]: >>> "".join(a2bits_list("Hello World!")) '010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001' """ - #return [bin(ord(x))[2:].rjust(8,"0") for x in chars] - return [bin(ord(x))[2:].rjust(32,"0") for x in chars] + return [bin(ord(x))[2:].rjust(ENCODINGS[encoding],"0") for x in chars] def bs(s: int) -> str: """Converts an int to its bits representation as a string of 0's and 1's. diff --git a/tests/test_lsb.py b/tests/test_lsb.py index 9672267..601c6b3 100644 --- a/tests/test_lsb.py +++ b/tests/test_lsb.py @@ -43,22 +43,31 @@ class TestLSB(unittest.TestCase): secret = lsb.hide("./tests/sample-files/Lenna.png", "") def test_hide_and_reveal(self): - messages_to_hide = ['πŸ•', 'a', 'foo', 'Hello World!', ':Python:'] + messages_to_hide = ['a', 'foo', 'Hello World!', ':Python:'] for message in messages_to_hide: secret = lsb.hide("./tests/sample-files/Lenna.png", message) secret.save("./image.png") clear_message = lsb.reveal("./image.png") - self.assertEqual(message, clear_message) + def test_hide_and_reveal_UTF32LE(self): + messages_to_hide = 'I love πŸ• and 🍫!' + secret = lsb.hide("./tests/sample-files/Lenna.png", + messages_to_hide, 'UTF-32LE') + secret.save("./image.png") + + clear_message = lsb.reveal("./image.png", 'UTF-32LE') + self.assertEqual(messages_to_hide, clear_message) + def test_with_transparent_png(self): messages_to_hide = ['πŸ•', 'a', 'foo', 'Hello World!', ':Python:'] for message in messages_to_hide: - secret = lsb.hide("./tests/sample-files/transparent.png", message) + secret = lsb.hide("./tests/sample-files/transparent.png", + message, 'UTF-32LE') secret.save("./image.png") - clear_message = lsb.reveal("./image.png") + clear_message = lsb.reveal("./image.png", 'UTF-32LE') self.assertEqual(message, clear_message) @@ -66,19 +75,19 @@ class TestLSB(unittest.TestCase): def test_manual_convert_rgb(self, input): message_to_hide = 'I love πŸ• and 🍫!' secret = lsb.hide("./tests/sample-files/Lenna-grayscale.png", - message_to_hide) + message_to_hide, 'UTF-32LE') @patch('builtins.input', return_value='n') def test_refuse_convert_rgb(self, input): message_to_hide = 'I love πŸ• and 🍫!' with self.assertRaises(Exception): secret = lsb.hide("./tests/sample-files/Lenna-grayscale.png", - message_to_hide) + message_to_hide, 'UTF-32LE') def test_auto_convert_rgb(self): message_to_hide = 'I love πŸ• and 🍫!' secret = lsb.hide("./tests/sample-files/Lenna-grayscale.png", - message_to_hide, True) + message_to_hide, 'UTF-32LE', True) def test_with_text_file(self): text_file_to_hide = './tests/sample-files/lorem_ipsum.txt' diff --git a/tests/test_lsbset.py b/tests/test_lsbset.py index 05ae71f..bf6bc74 100644 --- a/tests/test_lsbset.py +++ b/tests/test_lsbset.py @@ -54,6 +54,18 @@ class TestLSBSet(unittest.TestCase): self.assertEqual(message, clear_message) + def test_hide_and_reveal_UTF32LE(self): + messages_to_hide = 'I love πŸ• and 🍫!' + secret = lsbset.hide("./tests/sample-files/Lenna.png", + messages_to_hide, + generators.eratosthenes(), + 'UTF-32LE') + secret.save("./image.png") + + clear_message = lsbset.reveal("./image.png", generators.eratosthenes(), + 'UTF-32LE') + self.assertEqual(messages_to_hide, clear_message) + def test_with_transparent_png(self): messages_to_hide = ["a", "foo", "Hello World!", ":Python:"] for message in messages_to_hide: @@ -68,21 +80,22 @@ class TestLSBSet(unittest.TestCase): @patch('builtins.input', return_value='y') def test_manual_convert_rgb(self, input): - message_to_hide = "I love πŸ• and 🍫!" + message_to_hide = "Hello World!" secret = lsbset.hide("./tests/sample-files/Lenna-grayscale.png", message_to_hide, generators.eratosthenes()) @patch('builtins.input', return_value='n') def test_refuse_convert_rgb(self, input): - message_to_hide = "I love πŸ• and 🍫!" + message_to_hide = "Hello World!" with self.assertRaises(Exception): secret = lsbset.hide("./tests/sample-files/Lenna-grayscale.png", message_to_hide, generators.eratosthenes()) def test_auto_convert_rgb(self): - message_to_hide = "I love πŸ• and 🍫!" + message_to_hide = "Hello World!" secret = lsbset.hide("./tests/sample-files/Lenna-grayscale.png", - message_to_hide, generators.eratosthenes(), True) + message_to_hide, generators.eratosthenes(), + auto_convert_rgb=True) def test_with_too_long_message(self): with open("./tests/sample-files/lorem_ipsum.txt") as f: diff --git a/tests/test_tools.py b/tests/test_tools.py index 520859c..1fa5951 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -37,8 +37,23 @@ class TestTools(unittest.TestCase): bits = tools.a2bits("Hello World!") self.assertEqual(bits, '010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001') - def test_a2bits_list(self): + def test_a2bits_list_UTF8(self): list_of_bits = tools.a2bits_list("Hello World!") + self.assertEqual(list_of_bits, ['01001000', + '01100101', + '01101100', + '01101100', + '01101111', + '00100000', + '01010111', + '01101111', + '01110010', + '01101100', + '01100100', + '00100001']) + + def test_a2bits_list_UTF32LE(self): + list_of_bits = tools.a2bits_list("Hello World!", 'UTF-32LE') self.assertEqual(list_of_bits, ['00000000000000000000000001001000', '00000000000000000000000001100101', '00000000000000000000000001101100', From 6be5c32fe7212c4a67b6bc664e9a64f0d2f485f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bonhomme?= Date: Thu, 4 May 2017 13:08:20 +0200 Subject: [PATCH 4/4] Updated revision date of changed Python files. --- stegano/lsbset/lsbset.py | 4 ++-- tests/test_lsb.py | 4 ++-- tests/test_lsbset.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/stegano/lsbset/lsbset.py b/stegano/lsbset/lsbset.py index cac7d32..9ebdc9b 100644 --- a/stegano/lsbset/lsbset.py +++ b/stegano/lsbset/lsbset.py @@ -20,9 +20,9 @@ # along with this program. If not, see __author__ = "Cedric Bonhomme" -__version__ = "$Revision: 0.4.2 $" +__version__ = "$Revision: 0.5 $" __date__ = "$Date: 2016/03/13 $" -__revision__ = "$Date: 2016/05/22 $" +__revision__ = "$Date: 2017/05/04 $" __license__ = "GPLv3" import sys diff --git a/tests/test_lsb.py b/tests/test_lsb.py index 601c6b3..6c2fde9 100644 --- a/tests/test_lsb.py +++ b/tests/test_lsb.py @@ -20,9 +20,9 @@ # along with this program. If not, see __author__ = "Cedric Bonhomme" -__version__ = "$Revision: 0.2 $" +__version__ = "$Revision: 0.3 $" __date__ = "$Date: 2016/04/12 $" -__revision__ = "$Date: 2017/02/22 $" +__revision__ = "$Date: 2017/05/04 $" __license__ = "GPLv3" import io diff --git a/tests/test_lsbset.py b/tests/test_lsbset.py index bf6bc74..19f45dc 100644 --- a/tests/test_lsbset.py +++ b/tests/test_lsbset.py @@ -20,9 +20,9 @@ # along with this program. If not, see __author__ = "Cedric Bonhomme" -__version__ = "$Revision: 0.3 $" +__version__ = "$Revision: 0.4 $" __date__ = "$Date: 2016/04/13 $" -__revision__ = "$Date: 2017/02/22 $" +__revision__ = "$Date: 2017/05/04 $" __license__ = "GPLv3" import os