Skip to main content
1 of 3
  1. The hidden quote is "Three may keep a secret, if two of them are dead. - Benjamin Franklin".
  2. A key is on the line

Solution Code:

import cv2 as cv
import numpy as np

WEIGHTS = 1 << np.arange(7, -1, -1)


def get_uint16(bits: np.ndarray) -> int:
    uint16_bytes = bits[:16].reshape(-1, 8) @ WEIGHTS
    return (uint16_bytes[0] << 8) | uint16_bytes[1]


def decode1(data: np.ndarray) -> str:
    bits = data.flatten() & np.uint8(1)
    assert bits[0] == bits[1] == 0
    length = get_uint16(bits[2:18])
    data_bytes = bits[18 : 18 + length].reshape(-1, 8) @ WEIGHTS
    return "".join(map(chr, data_bytes))


def decode2(data: np.ndarray) -> np.ndarray:
    bits = data.flatten() & np.uint8(1)
    assert bits[0] == 1 and bits[1] == 0
    width = get_uint16(bits[2:18])
    height = get_uint16(bits[18:34])
    img_bits = bits[34 : 34 + width * height]
    img = (img_bits.reshape(height, width) * 255).astype(np.uint8)
    return img


def main() -> None:
    img1 = cv.imread("img1.png")
    img1 = cv.cvtColor(img1, cv.COLOR_BGR2RGB)
    print(decode1(img1))

    img2 = cv.imread("img2.png")
    img2 = cv.cvtColor(img2, cv.COLOR_BGR2RGB)
    img2 = decode2(img2)
    cv.imshow("Decoded Image", img2)
    cv.waitKey(0)
    cv.destroyAllWindows()


if __name__ == "__main__":
    main()

Decoded message:

The first part of the sentence is "Three may keep a secret, ...".

### Task 2: Decode a binary image

A binary image is hidden in the least significant bits of the butterfly image, the encoding scheme is:

- Simple LSB Encoding**
- Header: `[1, 0]`
- Next 16 bits: image `width` in pixels
- Next 16 bits: image `height` in pixels
- Raw pixels of the binary image

Hint: Create and fill an image array of the derived `width` and `height` with the encoded hidden pixels.
In order to view your image in classic viewers, it might be helpful to map 0 -> 0 and 1 -> 255.
Derive the second part of the sentence and answer the question about the image