JPEG Images and how to hide a image inside a image.

Hide a secret message in a image.

JPEG

JPEG or Joint Photographic Experts Group was introduced in 1972. We can easily understand the jpeg format as the word called image(picture) but the machines understand them as chunks of bytes (or) hexadecimal numbers. We can view these hexadecimal digits using a hex editor. In windows we can download HxD hex editor (or) in Linux, we can use the command hexeditor image.jpeg to open the hex editor in the terminal. A hex editor will look like this, JPEG and JPG are the same. The name difference exist because of the naming systems used in the past.

hex.png

File Structure of JPG image

As we can see, the hex starts with FF D8 FF , this is common in every JPG file. Every JPG file has two main segments header and data segments. The header has details about image and ends with the hexadecimal numbers FF DA. The data segment begins at FF DA and ends in FF D9, this marks the end of file. Between these numbers, the information in the image (RGB details etc.. ) is written. It's weird to saw a image is written, isn't it ?, but that's the normal for a computer. Every image is made of pixels and pixels mixture of three colors -> red, green, blue. We use these three colors because these colors are the most perceived colors. These use format called little Indian where small numbers are written first and large numbers are written next. The pixel information are stored as values in this segment.

Hide a image

As I explained above about the structure of jpg. A jpg file ends with the numbers FF D9, but after this segment any numbers written are ignored, but stored in the file and not displayed in the image. We use this to store a image inside a image.

Python

We can use python to do this, and it is very simple. Libraries used,

Note: I have a image called main_image.jpg and hidden_image.jpg in same folder as my program

import PIL.Image as pl
import io
image=pl.open(hidden.jpg) 
byte=io.BytesIO()
image.save(byte,"Format") # convert the hidden image to bytes to be stored
with open('main_image.jpg' , 'ab') as i:  # open images in append mode 
    i.write(byte.getvalue())  # write to the main image

Finding hidden files

These kind of hidden files can be found by a tool called binwalk. This tool can extract any hidden files in a image. This article is only for learning and sharing knowledge.

Did you find this article valuable?

Support hari hara sankar by becoming a sponsor. Any amount is appreciated!