Introduce a new argument in a2bits_list in order to specify the encoding of the string (unicode)

This commit is contained in:
Cédric Bonhomme 2017-05-04 13:05:56 +02:00
parent 49964d4f16
commit 5f5c07493c
No known key found for this signature in database
GPG key ID: A1CB94DE57B7A70D
6 changed files with 77 additions and 28 deletions

View file

@ -20,8 +20,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>
__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:

View file

@ -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:

View file

@ -20,9 +20,9 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>
__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.