Image Steganography: Hiding text in images using PHP
Steganography is the art and science of writing hidden message in such a way that no one, apart from the sender and intended recipient, suspects the existence of the message.
Image Steganography is the technique of hiding the data within the image in such a way that prevents the unintended user from the detection of the hidden messages or data.
Related Theory:
Images are made up small units of dots called as pixels. Each pixel is represented as 3 bytes : one for Red, one for Green and one for Blue. The composition of these three colors determines the actual color that pixel shows.
Red :
Binary: 11001001
Decimal: 201
Green:
Binary: 11111000
Decimal: 201
Blue:
Binary: 00000011
Decimal: 3
This composition gives rise to orange color.
The basic idea in Image Steganography lies in the fact that a change in the Least Significant Bit (LSB) is not detected by human eye. So we modify the LSB of RGB value to store the hidden message in the message without affecting the color of the image.
In this example, we change the LSB of Blue component only. But we can change the LSB of all Red, Green and Blue component if we want. As we change the LSB of blue component only, the amount of information we can hide is less. We can also store the length of the hidden message in the image which has not been done in this tutorial.
Suppose we want to hide 1101 in the image.
First we get RGB value of each pixel in the image. Since, we are hiding 4 bit data and we are changing blue component, so we would need at 4 pixels of the image.
Suppose we got the following RGB values in the first 4 pixels of the image:
11001100 10010001 00101011
00011000 11110000 11111110
11100010 00100101 01010101
11111101 00001010 01000011
Now, we will replace last bit of each pixel’s RGB value with 1101 consecutively.
So, the new RGB value becomes:
11001100 10010001 00101011
00011000 11110000 11111111
11100010 00100101 01010100
11111101 00001010 01000011
The highlighted bit represent the message we are hiding in the image.
Now we set the new RGB value to the pixel. This change is not detected by human eye and the image looks the same.
The encrypting potion is now complete and we will now decode the hidden message in the pic.
For this we fetch the RGB value of each pixel and then concat the LSB to get our hidden message.
Implementation with PHP
We need one image and 3 php scripts for implementing basic Image Steganography.
- encrypt.php
- decrypt.php
- functions.php
- 1.jpg
encrypt.php
<?php include('functions.php'); $message_to_hide = 'hello'; $binary_message = toBin($message_to_hide); $message_length = strlen($binary_message); $src = '1.jpg'; $im = imagecreatefromjpeg($src); for($x=0;$x<$message_length;$x++){ $y = $x; $rgb = imagecolorat($im,$x,$y); $r = ($rgb >>16) & 0xFF; $g = ($rgb >>8) & 0xFF; $b = $rgb & 0xFF; $newR = $r; $newG = $g; $newB = toBin($b); $newB[strlen($newB)-1] = $binary_message[$x]; $newB = toString($newB); $new_color = imagecolorallocate($im,$newR,$newG,$newB); imagesetpixel($im,$x,$y,$new_color); } echo $x; imagepng($im,'simple.png'); imagedestroy($im); ?>
decrypt.php
<?php include('functions.php'); $src = 'simple.png'; $im = imagecreatefrompng($src); $real_message = ''; for($x=0;$x<40;$x++){ $y = $x; $rgb = imagecolorat($im,$x,$y); $r = ($rgb >>16) & 0xFF; $g = ($rgb >>8) & 0xFF; $b = $rgb & 0xFF; $blue = toBin($b); $real_message .= $blue[strlen($blue)-1]; } $real_message = toString($real_message); echo $real_message; die; ?>
functions.php
<?php function toBin($str){ $str = (string)$str; $l = strlen($str); $result = ''; while($l--){ $result = str_pad(decbin(ord($str[$l])),8,"0",STR_PAD_LEFT).$result; } return $result; } function toString($binary){ return pack('H*',base_convert($binary,2,16)); } ?>
You can go through this video for detail information:
Good article, thanks
This post is great. It helped me a lot. Can you please post another post on how do we encrypt text in each rgb component and decrypt the text. That would really be helpful.
Good,
Can you Please Explain how to incease the Length of cover message??
is there any sample for hiding text in audio steganography?
Nice article Ritesh. Are you able to comment out your code as am new to some image manipulation with PHP .Also would you happen to know any sample php code with regards to steganalysis of an image using LSB . Would appreciate if you know how this can be achieved nawajoel(at)gmail.com
Nice keep it up 🙂
Does this works on UTF-8 character set or is it just for ASCII ?
Hey,
Cool article, thank you. But aren’t you going through the picture diagonnally when you set $y = $x at the beginning of the for loop and doesn’t this drastically reduce the amount of information that could be stored inside the image?
real message prints only 5 digit only. How to print all values in the real message??