Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Hide the message or image inside an image

                                Steganography is a technique that hides secret information inside another document or image and it is extracted by different person.
In this post, we will see how to hide text message or image inside an image without tampering the original or cover image.  Learn about bit plane slicing to know more about the background,

EXAMPLE 1: Hide Secret Message inside an image




DECRYPTION:

It is straight-forward reverse process. The secret message is obtained by extracting the least significant bits from the encrypted matrix


MATLAB CODE:
%ENCRYPTION

clear all
%READ THE INPUT IMAGE
A = imread('cameraman.tif');

%
Output = A;

%MESSAGE TO BE HIDDEN INSIDE THE IMAGE
Str = 'Image Processing! Let us learn together. Visit angeljohnsy.blogspot.com';

%NUMBER OF CHARACTERS IN THE MESSAGE
numLetters = numel(Str);

%REPRESENT THE LENGTH OF THE MESSAGE IN BINARY FORMAT
Bi_Total = de2bi(numLetters,8);
Bi_Msg = de2bi(double(Str),8)';

Encrypt_msg = Bi_Msg(:)';

%FIND THE LENGTH OF THE MESSAGE IN THE BINARY FORMAT
num_En_msg = numel(Encrypt_msg);


%DEFINE THE INTERVAL BETWEEN EACH VALUE
dt=floor(numel(A)/num_En_msg);

%BINARY VALUE OF THE INTERVAL
Bi_dt = de2bi(dt,8);

% FIRST 8 BITS - INTERVAL
% SECOND 8 BITS - TOTAL LENGTH OF THE BINARY VALUES
Info_data = [Bi_dt,Bi_Total];
Output(1:16) = bitset(A(1:16),1,Info_data);

%REPLACE THE LEAST SIGNIFICANT BIT IN THE IMAGE
Start_pt =17;
End_pt = (num_En_msg*dt)+16;
Output(Start_pt:dt:End_pt) = bitset(A(Start_pt:dt:End_pt),1,Encrypt_msg);


figure,subplot(121),imshow(A);title('Original Image');
subplot(122),imshow(Output);title('Image with the hidden message');

%SHOW THE DIFFERENCE BETWEEN THE ORIGINAL AND THE ENCRYPTED IMAGE
figure,imagesc(double(A)-double(Output));colormap(jet);colorbar;

%WRITE THE ENCRYPTED IMAGE IN THE PNG FORMAT
imwrite(Output,'Encrypt1.png');

display('Message is hidden inside the image!');



EXPLANATION:
A message is hidden inside the image by converting the message into binary format and placing it in the Least Significant Bit of the image pixels. The encrypted image is saved as ‘PNG’ image file in order to have lossless compression. With the lossless compression technique, the pixel values will not be altered so the information embedded in the LSB will not be lost.
The maximum absolute difference in value between the original and the encrypted image is one. This is obvious due to the fact that we are dealing only with the LSB bit of the image.
We can place the bits of the secret message in a specific pattern or with regular intervals instead of replacing the pixels adjacent to each other. The image shown below indicates that the bits are placed with specific interval values.


The number of characters in the secret message should be less than 10% of the cover image. This is due to the fact that for each character we need 8 bits and to place these 8 bits we need 8 pixels.
MATLAB CODE:
%DECRYPT THE MESSAGE
clear all
clc
%READ THE ENCRYPTED IMAGE
A = imread('Encrypt1.png');

%FIRST 8 BITS REPRESENT THE INTERVAL
Dt = bitget(A(1:8),1);
Dt = double(bi2de(Dt));


%LENGTH OF THE TEXT MESSAGE
Len = bitget(A(9:16),1);
Len = double(bi2de(Len));

%FETCH THE LEAST SIGNIFICANT BIT FROM THE ENCRYPTD IMAGE
Start_pt = 17;
End_pt = (Len*Dt*8)+16;
Decrypt_msg = bitget(A(Start_pt:Dt:End_pt),1);

%CONVERT THE IMAGE INTO DECIMAL
Decrypt_msg = reshape(Decrypt_msg,8,[])';
Dec_Char   = bi2de(Decrypt_msg)';

display('Text Message inside the image:');
display(char(Dec_Char));


EXPLANATION:
Decryption is the reverse process. First the interval between the pixels is fetched, followed by the length of the message.  Based on the length of the message, the bits are extracted and the final message is obtained.




like button Like "IMAGE PROCESSING" page

3 comments:

Unknown said... Reply to comment

Hey it works. Successfully decrypted. Thanks a lot!!!

Aaron Angel said... Reply to comment

@Adaline Christy

Happy to hear that. Thanks for visiting the blog.

Mohammed Othman said... Reply to comment

Hopefully you will respond. When I run the encryption code it says in the bitset best part, ASSUMEDTYPE must be an integer type name. How can I fix this?

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com