Skip to content

ZIPFly

stegobox.codec.ZIPFly

Bases: BaseCodec

This module implements steganography of any file in zip file.

with code shamelessly taken from TomZurales/fly

Source code in stegobox/codec/zipfly.py
class ZIPFly(BaseCodec):
    """This module implements steganography of any file in `zip` file.

    with code shamelessly taken from
    [TomZurales/fly](https://github.com/TomZurales/fly)
    """

    def __init__(self) -> None:
        super().__init__()

    def encode(self, carrier: bytes, payload: bytes) -> bytes:
        """Encoder requires the carrier to be `zip` file in bytes and the payload to be
        any file in bytes.

        Args:
            carrier: `zip` file bytes. Read with `stegobox.io.txt.read_bytes()`.
            payload: Any file bytes. Payload (secret file) to be
                encoded. Also read with `stegobox.io.txt.read_bytes()`.

        Returns:
            Encrypted `zip` file bytes.
        """
        zip_file = carrier
        payload = b"".join([MAGIC, payload])

        payload_pos = zip_file.find(CENTRAL_DIRECTORY_HEADER)
        pre_payload = zip_file[:payload_pos]
        post_payload = zip_file[payload_pos:]
        payload_size = len(bytes(payload))
        central_directory_ptr = int.from_bytes(
            post_payload[CENTRAL_DIRECTORY_OFFSET : CENTRAL_DIRECTORY_OFFSET + 4],
            byteorder="little",
        )
        central_directory_ptr += payload_size
        post_payload = b"".join(
            [
                post_payload[:CENTRAL_DIRECTORY_OFFSET],
                central_directory_ptr.to_bytes(4, "little"),
                post_payload[CENTRAL_DIRECTORY_OFFSET + 2 :],
            ]
        )

        out = b"".join([pre_payload, payload, post_payload])

        return out

    def decode(self, carrier: bytes) -> bytes:
        """Decode the secret file from the carrier `zip` file in bytes.

        Args:
            carrier: Encrypted `zip` file bytes. Read with
                `stegobox.io.txt.read_bytes()`.

        Returns:
            The decoded file in bytes (secret file).
        """
        zip_file = carrier
        payload_begin = zip_file.find(MAGIC) + len(MAGIC)
        payload_end = zip_file.find(CENTRAL_DIRECTORY_HEADER)
        out = zip_file[payload_begin:payload_end]
        return out

encode(carrier, payload)

Encoder requires the carrier to be zip file in bytes and the payload to be any file in bytes.

Parameters:

Name Type Description Default
carrier bytes

zip file bytes. Read with stegobox.io.txt.read_bytes().

required
payload bytes

Any file bytes. Payload (secret file) to be encoded. Also read with stegobox.io.txt.read_bytes().

required

Returns:

Type Description
bytes

Encrypted zip file bytes.

Source code in stegobox/codec/zipfly.py
def encode(self, carrier: bytes, payload: bytes) -> bytes:
    """Encoder requires the carrier to be `zip` file in bytes and the payload to be
    any file in bytes.

    Args:
        carrier: `zip` file bytes. Read with `stegobox.io.txt.read_bytes()`.
        payload: Any file bytes. Payload (secret file) to be
            encoded. Also read with `stegobox.io.txt.read_bytes()`.

    Returns:
        Encrypted `zip` file bytes.
    """
    zip_file = carrier
    payload = b"".join([MAGIC, payload])

    payload_pos = zip_file.find(CENTRAL_DIRECTORY_HEADER)
    pre_payload = zip_file[:payload_pos]
    post_payload = zip_file[payload_pos:]
    payload_size = len(bytes(payload))
    central_directory_ptr = int.from_bytes(
        post_payload[CENTRAL_DIRECTORY_OFFSET : CENTRAL_DIRECTORY_OFFSET + 4],
        byteorder="little",
    )
    central_directory_ptr += payload_size
    post_payload = b"".join(
        [
            post_payload[:CENTRAL_DIRECTORY_OFFSET],
            central_directory_ptr.to_bytes(4, "little"),
            post_payload[CENTRAL_DIRECTORY_OFFSET + 2 :],
        ]
    )

    out = b"".join([pre_payload, payload, post_payload])

    return out

decode(carrier)

Decode the secret file from the carrier zip file in bytes.

Parameters:

Name Type Description Default
carrier bytes

Encrypted zip file bytes. Read with stegobox.io.txt.read_bytes().

required

Returns:

Type Description
bytes

The decoded file in bytes (secret file).

Source code in stegobox/codec/zipfly.py
def decode(self, carrier: bytes) -> bytes:
    """Decode the secret file from the carrier `zip` file in bytes.

    Args:
        carrier: Encrypted `zip` file bytes. Read with
            `stegobox.io.txt.read_bytes()`.

    Returns:
        The decoded file in bytes (secret file).
    """
    zip_file = carrier
    payload_begin = zip_file.find(MAGIC) + len(MAGIC)
    payload_end = zip_file.find(CENTRAL_DIRECTORY_HEADER)
    out = zip_file[payload_begin:payload_end]
    return out