Пример #1
0
def hide_data(UID, files, modelDirectory, newDirectory):
    i=0
    for i in range(0,len(files)):
        file=modelDirectory + "/" + files[i]
        newfile = (newDirectory + "/" + files[i])
        print("File name: ", file)
        if file.lower().endswith(('.png', '.jpg', '.jpeg')):
            print("image mode: ",(tools.open_image(file)).mode)
            if ((tools.open_image(file)).mode not in ["RGB", "RGBA"]):
                file = convert_image(file)
            fileObject = lsb.hide(file, UID)
            fileObject.save(newfile)
        else:
            copyfile(file,newfile)
Пример #2
0
def reveal(input_image: Union[str, IO[bytes]],
           encoding: str = "UTF-32LE",
           shift: int = 0):
    """Find a message in an image (with the LSB technique).
    """
    img = tools.open_image(input_image)
    width, height = img.size
    buff, count = 0, 0
    bitab = []
    limit = None
    for row in range(height):
        for col in range(width):
            if shift != 0:
                shift -= 1
                continue
            # pixel = [r, g, b] or [r,g,b,a]
            pixel = img.getpixel((col, row))
            if img.mode == "RGBA":
                pixel = pixel[:3]  # ignore the alpha
            for color in pixel:
                buff += (color & 1) << (tools.ENCODINGS[encoding] - 1 - count)
                count += 1
                if count == tools.ENCODINGS[encoding]:
                    bitab.append(chr(buff))
                    buff, count = 0, 0
                    if bitab[-1] == ":" and limit is None:
                        try:
                            limit = int("".join(bitab[:-1]))
                        except:
                            pass

            if len(bitab) - len(str(limit)) - 1 == limit:
                img.close()
                return "".join(bitab)[len(str(limit)) + 1:]
Пример #3
0
def hide(input_image: Union[str, IO[bytes]], message: str):
    """
    Hide a message (string) in an image.

    Use the red portion of a pixel (r, g, b) tuple to
    hide the message string characters as ASCII values.
    The red value of the first pixel is used for message_length of the string.
    """
    message_length = len(message)
    assert message_length != 0, "message message_length is zero"
    assert message_length < 255, "message is too long"
    img = tools.open_image(input_image)
    # Use a copy of image to hide the text in
    encoded = img.copy()
    width, height = img.size
    index = 0
    for row in range(height):
        for col in range(width):
            (r, g, b) = img.getpixel((col, row))
            # first value is message_length of message
            if row == 0 and col == 0 and index < message_length:
                asc = message_length
            elif index <= message_length:
                c = message[index - 1]
                asc = ord(c)
            else:
                asc = r
            encoded.putpixel((col, row), (asc, g, b))
            index += 1
    img.close()
    return encoded
def reveal(
    input_image: Union[str, IO[bytes]],
    generator: Iterator[int],
    shift: int = 0,
    encoding: str = "UTF-8",
):
    """Find a message in an image (with the LSB technique).
    """
    img = tools.open_image(input_image)
    img_list = list(img.getdata())
    width, height = img.size
    buff, count = 0, 0
    bitab = []
    limit = None

    while shift != 0:
        next(generator)
        shift -= 1

    while True:
        generated_number = next(generator)
        # color = [r, g, b]
        for color in img_list[generated_number][:3]:  # ignore the alpha
            buff += (color & 1) << (tools.ENCODINGS[encoding] - 1 - count)
            count += 1
            if count == tools.ENCODINGS[encoding]:
                bitab.append(chr(buff))
                buff, count = 0, 0
                if bitab[-1] == ":" and limit == None:
                    if "".join(bitab[:-1]).isdigit():
                        limit = int("".join(bitab[:-1]))
                    else:
                        raise IndexError("Impossible to detect message.")
        if len(bitab) - len(str(limit)) - 1 == limit:
            return "".join(bitab)[len(str(limit)) + 1:]
Пример #5
0
def hide(
    input_image_file,
    img_enc,
    secret_message=None,
    secret_file=None,
    img_format=None,
):
    """Hide a message (string) in an image."""
    from zlib import compress
    from base64 import b64encode

    if secret_file is not None:
        with open(secret_file, "r") as f:
            secret_message = f.read()

    try:
        text = compress(b64encode(bytes(secret_message, "utf-8")))
    except Exception:
        text = compress(b64encode(secret_message))

    img = tools.open_image(input_image_file)

    if img_format is None:
        img_format = img.format

    if "exif" in img.info:
        exif_dict = piexif.load(img.info["exif"])
    else:
        exif_dict = {}
        exif_dict["0th"] = {}
    exif_dict["0th"][piexif.ImageIFD.ImageDescription] = text
    exif_bytes = piexif.dump(exif_dict)
    img.save(img_enc, format=img_format, exif=exif_bytes)
    img.close()
    return img
Пример #6
0
 def open_reveal(self, path, filename):
     try:
         with open(os.path.join(path, filename[0])) as f:
             # display the image
             self.ids.rimage.source = filename[0]
             offs = self.ids.offsetintr.text
             theimg = tools.open_image(os.path.join(path, filename[0]))
             revealed = lsb.reveal(theimg, "UTF-8", int(offs))
             # PRINT THE RESPONSE
             if revealed:
                 self.ids.revmsg.text = revealed
             else:
                 self.ids.revmsg.text = "Nothing found."
     except:
         self.ids.revmsg.text = "Nothing Found."
         print("Error in REVEAL")
Пример #7
0
    def open_embed(self, path, filename):
        try:
            with open(os.path.join(path, filename[0])) as f:

                # display the image
                self.ids.eimage.source = filename[0]

                offs = self.ids.offsetinte.text
                # use Stegano's tools to open the image for analysis
                theimg = tools.open_image(os.path.join(path, filename[0]))
                secret = lsb.hide(theimg, self.ids.secmsg.text, "UTF-8",
                                  int(offs))
                head, tail = os.path.split(filename[0])
                secret.save(os.path.join(path, "STEG-" + tail))
                print("FILE SAVED AS: STEG-" + tail)
                self.ids.savedmsg.text = "FILE SAVED AS:  STEG-" + tail
        except:
            print("Error in EMBED")
Пример #8
0
def reveal(input_image_file):
    """Find a message in an image."""
    from base64 import b64decode
    from zlib import decompress

    img = tools.open_image(input_image_file)

    try:
        if img.format in ["JPEG", "TIFF"]:
            if "exif" in img.info:
                exif_dict = piexif.load(img.info.get("exif", b""))
                description_key = piexif.ImageIFD.ImageDescription
                encoded_message = exif_dict["0th"][description_key]
            else:
                encoded_message = b""
        else:
            raise ValueError("Given file is neither JPEG nor TIFF.")
    finally:
        img.close()

    return b64decode(decompress(encoded_message))
Пример #9
0
def reveal(input_image: Union[str, IO[bytes]]):
    """
    Find a message in an image.

    Check the red portion of an pixel (r, g, b) tuple for
    hidden message characters (ASCII values).
    The red value of the first pixel is used for message_length of string.
    """
    img = tools.open_image(input_image)
    width, height = img.size
    message = ""
    index = 0
    for row in range(height):
        for col in range(width):
            r, g, b = img.getpixel((col, row))
            # First pixel r value is length of message
            if row == 0 and col == 0:
                message_length = r
            elif index <= message_length:
                message += chr(r)
            index += 1
    img.close()
    return message
Пример #10
0
def hide(
    input_image: Union[str, IO[bytes]],
    message: str,
    encoding: str = "UTF-32LE",
    shift: int = 0,
    auto_convert_rgb: bool = False,
):
    """Hide a message (string) in an image with the
    LSB (Least Significant Bit) technique.
    """
    message_length = len(message)
    assert message_length != 0, "message length is zero"

    img = tools.open_image(input_image)

    if img.mode not in ["RGB", "RGBA"]:
        if not auto_convert_rgb:
            print("The mode of the image is not RGB. Mode is {}".format(
                img.mode))
            answer = input("Convert the image to RGB ? [Y / n]\n") or "Y"
            if answer.lower() == "n":
                raise Exception("Not a RGB image.")
        img = img.convert("RGB")

    encoded = img.copy()
    width, height = img.size
    index = 0

    message = str(message_length) + ":" + str(message)
    message_bits = "".join(tools.a2bits_list(message, encoding))
    message_bits += "0" * ((3 - (len(message_bits) % 3)) % 3)

    npixels = width * height
    len_message_bits = len(message_bits)
    if len_message_bits > npixels * 3:
        raise Exception("The message you want to hide is too long: {}".format(
            message_length))
    for row in range(height):
        for col in range(width):
            if shift != 0:
                shift -= 1
                continue
            if index + 3 <= len_message_bits:

                # Get the colour component.
                pixel = img.getpixel((col, row))
                r = pixel[0]
                g = pixel[1]
                b = pixel[2]

                # Change the Least Significant Bit of each colour component.
                r = tools.setlsb(r, message_bits[index])
                g = tools.setlsb(g, message_bits[index + 1])
                b = tools.setlsb(b, message_bits[index + 2])

                # Save the new pixel
                if img.mode == "RGBA":
                    encoded.putpixel((col, row), (r, g, b, pixel[3]))
                else:
                    encoded.putpixel((col, row), (r, g, b))

                index += 3
            else:
                img.close()
                return encoded
def hide(
    input_image: Union[str, IO[bytes]],
    message: str,
    generator: Iterator[int],
    shift: int = 0,
    encoding: str = "UTF-8",
    auto_convert_rgb: bool = False,
):
    """Hide a message (string) in an image with the
    LSB (Least Significant Bit) technique.
    """
    message_length = len(message)
    assert message_length != 0, "message length is zero"

    img = tools.open_image(input_image)

    if img.mode not in ["RGB", "RGBA"]:
        if not auto_convert_rgb:
            print("The mode of the image is not RGB. Mode is {}".format(
                img.mode))
            answer = input("Convert the image to RGB ? [Y / n]\n") or "Y"
            if answer.lower() == "n":
                raise Exception("Not a RGB image.")
        img = img.convert("RGB")

    img_list = list(img.getdata())
    width, height = img.size
    index = 0

    message = str(message_length) + ":" + str(message)
    message_bits = "".join(tools.a2bits_list(message, encoding))
    message_bits += "0" * ((3 - (len(message_bits) % 3)) % 3)

    npixels = width * height
    len_message_bits = len(message_bits)
    if len_message_bits > npixels * 3:
        raise Exception("The message you want to hide is too long: {}".format(
            message_length))
    while shift != 0:
        next(generator)
        shift -= 1

    while index + 3 <= len_message_bits:
        generated_number = next(generator)
        r, g, b, *a = img_list[generated_number]

        # Change the Least Significant Bit of each colour component.
        r = tools.setlsb(r, message_bits[index])
        g = tools.setlsb(g, message_bits[index + 1])
        b = tools.setlsb(b, message_bits[index + 2])

        # Save the new pixel
        if img.mode == "RGBA":
            img_list[generated_number] = (r, g, b, *a)
        else:
            img_list[generated_number] = (r, g, b)

        index += 3

    # create empty new image of appropriate format
    encoded = Image.new(img.mode, (img.size))

    # insert saved data into the image
    encoded.putdata(img_list)

    return encoded
Пример #12
0
def convert_image(file):
    file = (tools.open_image(file)).convert("RGBA")
    print("converted file", file)
    return file
Пример #13
0
def main():
    parser = argparse.ArgumentParser(prog='stegano-lsb-set')
    subparsers = parser.add_subparsers(help='sub-command help', dest='command')

    # Subparser: Hide
    parser_hide = subparsers.add_parser('hide', help='hide help')
    # Original image
    parser_hide.add_argument("-i",
                             "--input",
                             dest="input_image_file",
                             required=True,
                             help="Input image file.")
    parser_hide.add_argument(
        "-e",
        "--encoding",
        dest="encoding",
        choices=tools.ENCODINGS.keys(),
        default='UTF-8',
        help="Specify the encoding of the message to hide." +
        " UTF-8 (default) or UTF-32LE.")

    # Generator
    parser_hide.add_argument("-g",
                             "--generator",
                             dest="generator_function",
                             action=ValidateGenerator,
                             nargs='*',
                             required=True,
                             help="Generator (with optional arguments)")
    parser_hide.add_argument("-s",
                             "--shift",
                             dest="shift",
                             default=0,
                             help="Shift for the generator")

    group_secret = parser_hide.add_mutually_exclusive_group(required=True)
    # Non binary secret message to hide
    group_secret.add_argument("-m",
                              dest="secret_message",
                              help="Your secret message to hide (non binary).")
    # Binary secret message to hide
    group_secret.add_argument(
        "-f",
        dest="secret_file",
        help="Your secret to hide (Text or any binary file).")

    # Image containing the secret
    parser_hide.add_argument("-o",
                             "--output",
                             dest="output_image_file",
                             required=True,
                             help="Output image containing the secret.")

    # Subparser: Reveal
    parser_reveal = subparsers.add_parser('reveal', help='reveal help')
    parser_reveal.add_argument("-i",
                               "--input",
                               dest="input_image_file",
                               required=True,
                               help="Input image file.")
    parser_reveal.add_argument(
        "-e",
        "--encoding",
        dest="encoding",
        choices=tools.ENCODINGS.keys(),
        default='UTF-8',
        help="Specify the encoding of the message to reveal." +
        " UTF-8 (default) or UTF-32LE.")
    parser_reveal.add_argument("-g",
                               "--generator",
                               dest="generator_function",
                               action=ValidateGenerator,
                               nargs='*',
                               required=True,
                               help="Generator (with optional arguments)")
    parser_reveal.add_argument("-s",
                               "--shift",
                               dest="shift",
                               default=0,
                               help="Shift for the generator")
    parser_reveal.add_argument(
        "-o",
        dest="secret_binary",
        help="Output for the binary secret (Text or any binary file).")

    # Subparser: List generators
    parser_list_generators = subparsers.add_parser('list-generators',
                                                   help='list-generators help')

    arguments = parser.parse_args()

    if arguments.command != 'list-generators':
        try:
            arguments.generator_function[0]
        except AttributeError:
            print('You must specify the name of a generator.')
            parser.print_help()
            exit(1)

        try:
            if (arguments.generator_function[0] == "LFSR"):
                # Compute the size of the image for use by the LFSR generator if needed
                tmp = tools.open_image(arguments.input_image_file)
                size = tmp.width * tmp.height
                tmp.close()
                arguments.generator_function.append(size)
            if (len(arguments.generator_function) > 1):
                generator = getattr(
                    generators, arguments.generator_function[0])(
                        *[int(e) for e in arguments.generator_function[1:]])
            else:
                generator = getattr(generators,
                                    arguments.generator_function[0])()

        except AttributeError as e:
            print("Unknown generator: {}".format(arguments.generator_function))
            exit(1)

    if arguments.command == 'hide':
        if arguments.secret_message != None:
            secret = arguments.secret_message
        elif arguments.secret_file != "":
            secret = tools.binary2base64(arguments.secret_file)

        img_encoded = lsbset.hide(arguments.input_image_file, secret,
                                  generator, int(arguments.shift))
        try:
            img_encoded.save(arguments.output_image_file)
        except Exception as e:
            # If hide() returns an error (Too long message).
            print(e)

    elif arguments.command == 'reveal':
        try:
            secret = lsbset.reveal(arguments.input_image_file, generator,
                                   int(arguments.shift))
        except IndexError:
            print("Impossible to detect message.")
            exit(0)
        if arguments.secret_binary != None:
            data = tools.base642binary(secret)
            with open(arguments.secret_binary, "w") as f:
                f.write(data)
        else:
            print(secret)

    elif arguments.command == 'list-generators':
        all_generators = inspect.getmembers(generators, inspect.isfunction)
        for generator in all_generators:
            print('Generator id:')
            print('    {}'.format(crayons.green(generator[0], bold=True)))
            print('Desciption:')
            print('    {}'.format(generator[1].__doc__))