Image Steganography : Hiding Text in Images using PHP

0
620

Steganography is the practice of concealing a message or information within another non-secret or unsuspecting medium, such as an image, audio, video, or text, without any apparent indication that a message is being transmitted. The goal of steganography is to hide the existence of the secret message in such a way that it can only be detected by the intended recipient, who knows where and how to look for it, while remaining undetectable by anyone else who might intercept the transmission.

Steganography is different from cryptography, which involves transforming the original message into an encrypted form that can only be deciphered with a secret key or password. While cryptography protects the content of the message from being understood by unauthorized parties, steganography protects the fact that a message is being transmitted at all.

Image Steganography

Image steganography is a technique of hiding secret information or messages within an image without changing its visual appearance or quality. The image acts as a cover or carrier for the secret message, and only the intended recipient who has the knowledge of the steganographic technique can extract the hidden message from the image.

There are various methods of image steganography, such as LSB (Least Significant Bit) embedding, which involves replacing the least significant bits of an image’s pixels with the secret message bits. Another popular method is the DCT (Discrete Cosine Transform) technique, which embeds the message in the frequency domain of the image by altering the DCT coefficients of the image.

Image steganography has several applications, including data hiding, digital watermarking, and secret communication. It can be used in various fields, such as law enforcement, military, journalism, and entertainment, to protect sensitive information or to ensure the authenticity of digital media. However, it can also be used for illegal activities, such as terrorism, cybercrime, and espionage, which can pose a significant threat to national security and privacy.

Least Significant Bit (LSB) Algorithm

The Least Significant Bit (LSB) algorithm is one of the most popular and simplest image steganography techniques used for embedding secret messages in digital images. The algorithm works by modifying the least significant bit of each pixel of the cover image to represent a bit of the secret message. Since the least significant bit has the least impact on the image quality, the change is not noticeable to the human eye.

Here is a basic implementation of the LSB algorithm for image steganography:

  1. Convert the secret message into binary form.
  2. Open the cover image in binary mode.
  3. For each pixel in the cover image:
    • Extract the RGB values of the pixel.
    • Modify the least significant bit of each color channel to the corresponding bit of the secret message.
  4. Save the modified image as the stego image.

To extract the hidden message from the stego image, the LSB algorithm follows a similar process:

    1. Open the stego image in binary mode.
    2. For each pixel in the stego image:
      • Extract the least significant bit of each color channel.
      • Concatenate the extracted bits to form the binary representation of the secret message.
    3. Convert the binary representation of the secret message back to its original form.

While the LSB algorithm is simple to implement, it is not very secure since it can be easily detected and removed by attackers using statistical analysis techniques. Therefore, more advanced steganography algorithms, such as the DCT-based algorithm, are often used for more secure and robust image steganography.

LSB Implementation for Image Steganography

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.

Follow this video for complete guidance :

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));
}
?>

 

Comments are closed.