Overview
Thepng_steg.h module provides steganography functionality for hiding and extracting secret messages in PNG images. Steganography is the practice of hiding information within other data in a way that is not easily detectable.
This implementation supports two different encoding methods depending on the image’s color type:
- Non-palette images (color types 0, 2, 4, 6): Uses the least significant bit (LSB) of the first channel of each pixel
- Palette images (color type 3): Uses identical-color pair index swapping for visually undetectable encoding
Functions
png_encode_lsb
Path to the input PNG file
Path where the output PNG file with hidden message will be written
The secret message to hide (null-terminated string)
Returns
0 on success, -1 on error (invalid parameters, file I/O errors, message too long, palette manipulation failure, etc.)Example
Encoding Algorithm
High-Level Process:- Open and validate the input PNG file
- Read and parse the IHDR chunk to determine image properties (width, height, color type)
- For palette images (color type 3):
- Extract the PLTE chunk
- Build a table of identical-color pairs in the palette
- If no pairs exist, append duplicate colors to create pairs (up to 256 entries)
- Create mapping:
pair[i] = jif colors at indices i and j are identical
- Decompress all IDAT chunks into a single buffer
- Calculate encoding capacity:
width × heightpixels - Check if message fits:
(strlen(secret) + 1) * 8bits ≤ capacity - Encode the message:
- For non-palette images: modify LSB of first channel of each pixel
- For palette images: swap between identical-color pair indices
- Re-compress the modified image data using PNG-compatible zlib settings
- Write output PNG, preserving all chunks except IDAT (replaced) and PLTE (if modified)
Technical Details
- Only supports 8-bit images (
bit_depth == 8) - Message capacity: 1 bit per pixel
- Filter bytes (first byte of each scanline) are never modified
- Uses
util_deflate_data_png()for PNG-compatible compression (windowBits=15) - Preserves all ancillary chunks from the input file
- For palette images, output is visually identical (uses identical colors)
- For non-palette images, changes are typically imperceptible (single LSB modification)
Error Conditions
- Returns
-1if any parameter is NULL - Returns
-1if image is not 8-bit - Returns
-1if message is too long for the image capacity - Returns
-1on file I/O errors - Returns
-1if palette manipulation fails (e.g., palette exceeds 256 entries)
png_extract_lsb
Path to the PNG file containing the hidden message
Output buffer where the extracted string will be written (must be at least
max_len bytes)Maximum length of the output buffer (reserves space for null terminator)
Returns the length of the extracted string on success (excluding null terminator),
-1 on errorExample
Extraction Algorithm
High-Level Process:- Open and validate the input PNG file
- Read and parse the IHDR chunk
- For palette images: extract PLTE and build identical-color pair table
- Decompress all IDAT chunks
- Extract bits from the image data:
- For non-palette images: read LSB from first channel of each pixel
- For palette images: determine bit by checking if index is “higher” of a pair
- Reconstruct bytes from bits (8 bits per byte)
- Stop extraction when null terminator (all zero bits) is found
- Return the length of the extracted string
Technical Details
- Only supports 8-bit images
- Extracts one bit per pixel
- For palette images: skips pixels whose index doesn’t have a pair
- Filter bytes are never read during extraction
- Output buffer must be at least
max_lenbytes - Returns the number of characters extracted (excluding null terminator)
Error Conditions
- Returns
-1if any parameter is NULL ormax_len == 0 - Returns
-1if image is not 8-bit - Returns
-1on file I/O errors
Palette Steganography Details
Identical-Color Pair Method
For palette images (color type 3), simple LSB modification would cause visible artifacts. The solution is to use identical-color pairs in the palette:- Build Pair Table: Find all pairs of palette indices that have identical RGB values
- Expand Palette: If insufficient pairs exist, append duplicate colors (up to 256 max)
- Index Mapping: Create mapping where
pair[i] = jif indices i and j are identical - Encoding: Swap between lower/higher index to encode 0/1 bits
- Decoding: Check if index is higher/lower of pair to extract bit
Why This Works
- Identical RGB values are visually indistinguishable
- Swapping indices between identical colors produces no visible change
- PNG specification allows duplicate colors in PLTE chunk
- Can encode up to 1 bit per pixel with zero visual artifacts
Message Capacity
The maximum message capacity depends on the image size:- 320×320 image: 102,400 bits = 12,800 bytes (12.5 KB)
- 640×480 image: 307,200 bits = 38,400 bytes (37.5 KB)
- 1920×1080 image: 2,073,600 bits = 259,200 bytes (253 KB)
Security Considerations
- LSB steganography is detectable with statistical analysis
- This implementation prioritizes simplicity over security
- For high-security applications, use encryption before embedding
- Palette method is more robust against visual detection
- Non-palette method may show minor artifacts under close inspection
Limitations
- Only supports 8-bit images (most common PNG format)
- Cannot encode in images with insufficient capacity
- Palette images limited to 256 color entries (PNG specification)
- No encryption (messages are hidden but not encrypted)
- No error correction (corrupted bits result in corrupted message)