Image Steganography using LSB?

I have coded a LSB algorithm for Image Steganography. During retrieval process i'm getting different msg. Can anyone correct this code please!
Embedding code
c = imread('image.bmp');
message = 'hellokarthick'
message = strtrim(message);
m = length(message) * 8;
AsciiCode = uint8(message);
binaryString = transpose(dec2bin(AsciiCode,8));
binaryString = binaryString(:);
N = length(binaryString);
b = zeros(N,1); %b is a vector of bits
for k = 1:N
if(binaryString(k) == '1')
b(k) = 1;
else
b(k) = 0;
end
end
s = c;
height = size(c,1);
width = size(c,2);
k = 1;
for i = 1 : height
for j = 1 : width
LSB = mod(double(c(i,j)), 2);
if (k>m || LSB == b(k))
s(i,j) = c(i,j);
else
if(LSB == 1)
s(i,j) = c(i,j) - 1;
else
s(i,j) = c(i,j) + 1;
end
k = k + 1;
end
end
end
imwrite(s, 'hiddenmsgimage.bmp');
Retriever coding
s = imread('hiddenmsgimage.bmp');
height = size(s,1);
width = size(s,2);
%For this example the max size is 100 bytes, or 800 bits, (bytes * = bits
m = 800;
k = 1;
for i = 1 : height
for j = 1 : width
if (k <= m)
b(k) = mod(double(s(i,j)),2);
k = k + 1;
end
end
end
binaryVector = b;
binValues = [ 128 64 32 16 8 4 2 1 ];
binaryVector = binaryVector(:);
if mod(length(binaryVector),8) ~= 0
error('Length of binary vector must be a multiple of 8.');
end
binMatrix = reshape(binaryVector,8,100);
display(binMatrix);
textString = char(binValues*binMatrix);
disp(textString);

16 comentarios

supriya
supriya el 10 de Mzo. de 2013
Hey KARTHICK,i tried to find the flaw in ur code but couldn't! Have u found it yet??
gokul
gokul el 21 de Mzo. de 2013
im working on the same code and experience same problem in decryption.. have u corrected d code. if so plz send me d correction.. plz do help me..
arundhati guchait
arundhati guchait el 7 de Mzo. de 2015
Editada: Image Analyst el 7 de Mzo. de 2015
In the retrieving part, just do a change,
for k = 1 : m
binaryVector(k) = b(k);
end
it will work.
thank u for the code. it helped us a lot.
Abhishek Barla
Abhishek Barla el 7 de Mayo de 2015
The code is correct except for one small change.
You never added the b(k) to the LSB. So, basically, you never encoded the message. Then how will you decode it?
In the encoding process, instead of this:
if (k>m LSB == b(k)) s(i,j) = c(i,j); else if(LSB == 1) s(i,j) = c(i,j) - 1; else s(i,j) = c(i,j) + 1; end k = k + 1;
You should have:
if (k>m LSB == b(k)) s(i,j) = c(i,j); else s(i,j) = c(i,j) + b(k); k = k + 1;
Everything else remains the same. It works!!
If anything else, then feel free to reply back to this comment. Hope it eases someone's life. Good luck!
ARJUN K P
ARJUN K P el 16 de Mayo de 2015
Editada: ARJUN K P el 16 de Mayo de 2015
the output of extraction is wrong...
pls help to to retrieve embedded data..
my email id : arjunkppc@gmail.com
JIJO JOSE
JIJO JOSE el 12 de Nov. de 2015
in the encoding part inside the two for loops make the following change and you can decode the message accurately,
if(k<=m)
LSB = mod(double(s(i,j)), 2);
s(i,j)=s(i,j)+LSB+b(k);
k=k+1;
end
Snehal Parekh
Snehal Parekh el 6 de Oct. de 2016
I can not hide the text information more then one page by using this code.....so please some one help me for hidding the text information in image more than one page
Walter Roberson
Walter Roberson el 6 de Oct. de 2016
Reshape() all of the pages together into a 2D array. Use this routine to hide the text. Reshape() back to 3D afterwards.
Sahana Konchada
Sahana Konchada el 9 de Mzo. de 2018
we are geeting the error while running this code please suggest how to recover the codes
Tommy Halim
Tommy Halim el 4 de Abr. de 2019
I just copying your code and I got error in my program, can you help me?
for i = 1 : height
for j = 1 : width
LSB = mod(double(c(i,j)), 2);
a1.JPG
Walter Roberson
Walter Roberson el 4 de Abr. de 2019
Tommy Halim, are you using the code posted by HARTHICK ? In that code, c would be the result of imread(), and imread() never returns a struct.
Tommy Halim
Tommy Halim el 5 de Abr. de 2019
ah, sorry my fault. when read an image
c = imread('image.bmp');
I change it with
c = getframe(handles.axes1);
Walter Roberson
Walter Roberson el 5 de Abr. de 2019
You will need c(index).cdata
Tommy Halim
Tommy Halim el 5 de Abr. de 2019
I has been change from
c = getframe(handles.axes1);
to
c = getimage(handles.axes1);
Walter Roberson
Walter Roberson el 5 de Abr. de 2019
getframe() takes a copy of whatever has been rendered into the frame, no matter what kind of graphics objects.
getimage() look specifically for image objects, such as created by image() or imagesc() or imshow() . Anything else that is part of the axes will be ignored.
Tommy Halim
Tommy Halim el 5 de Abr. de 2019
Very helpful, thank you Walter Roberson

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Mzo. de 2013

0 votos

3 comentarios

faiz ul amin
faiz ul amin el 20 de Abr. de 2024
% Access x y z coordinates and RGB color
% Read the point cloud from the 'Axle shaft.ply' file
ptCloud = pcread('Axle shaft.ply');
pcshow(ptCloud);
title('Axle shaft');
% Add labels to the axes
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
% Access the location (coordinates) of all points in the point cloud
pointLocations = ptCloud.Location;
% Access the RGB color information of all points in the point cloud
pointColors = ptCloud.Color;
% Round the coordinates to the nearest integer
pointLocations = round(pointLocations);
% Display all points in the point cloud with RGB intensities
disp('All points in the point cloud (rounded to integers) with RGB intensities:');
disp([pointLocations, pointColors]);
disp('Axle shaft.ply');
faiz ul amin
faiz ul amin el 20 de Abr. de 2024
dear walter roberson this is a code that i write for hiding data in 3d image.this code is not complete .i acces the points ,rgb intensity.now i hide the data in rgb intensity .i dont know how to complete code.
Image Analyst
Image Analyst el 20 de Abr. de 2024
@faiz ul amin If you have any questions, then ask them in a new discussion thread (not here), and attach your data and code to read it in with the paperclip icon after you read this:

Iniciar sesión para comentar.

Más respuestas (11)

Maged Rawash
Maged Rawash el 25 de Abr. de 2015

2 votos

it gives you different msg because you are using JPG and jpg using lossy mode to compress .. which change the pixel value and return different msg ..
you can use that code in imwrite();
imwrite(s,'img.jpg', 'Mode','lossless' );
% not all image application will read JPG lossless
so just work on PNG, TIFF it works with me ...
to retrieve the correct massage without guessing the char number of the massage ... just store the msg length in first pixel and get it in retrieving code like this ....
c = imread(image);
c(1:1:1)= length(msg) ; %to count massage Char to easly retrive all the massage
c=imresize(c,[size(c,1) size(c,2)],'nearest');
message = msg ; %add ' .' to prevint lossing one char
message = strtrim(message);
m = length(message) * 8;
AsciiCode = uint8(message);
binaryString = transpose(dec2bin(AsciiCode,8));
binaryString = binaryString(:);
N = length(binaryString);
b = zeros(N,1);
for k = 1:N
if(binaryString(k) == '1')
b(k) = 1;
else
b(k) = 0;
end
end
s = c;
height = size(c,1);
width = size(c,2);
k = 1;
for i = 1 : height
for j = 1 : width
LSB = mod(double(c(i,j)), 2);
if (k>m || LSB == b(k))
s(i,j) = c(i,j);
elseif(LSB == 1)
s(i,j) = (c(i,j) - 1);
elseif(LSB == 0)
s(i,j) = (c(i,j) + 1);
end
k = k + 1;
end
end
imgWTxt = 'msgimage.png';
imwrite(s,imgWTxt);
----------------------------------- Retriever coding
s = imread(image);
height = size(s,1);
width = size(s,2);
m = double( s(1:1:1) ) * 8 ;
k = 1;
for i = 1 : height
for j = 1 : width
if (k <= m)
b(k) = mod(double(s(i,j)),2);
k = k + 1;
end
end
end
binaryVector = b;
binValues = [ 128 64 32 16 8 4 2 1 ];
binaryVector = binaryVector(:);
if mod(length(binaryVector),8) ~= 0
error('Length of binary vector must be a multiple of 8.');
end
binMatrix = reshape(binaryVector,8,[]);
textString = char(binValues*binMatrix);
disp(textString);
thanks For the Code ...

5 comentarios

rini lya
rini lya el 4 de Jun. de 2016
Editada: Walter Roberson el 4 de Jun. de 2016
Retrieval coding
Your code works even when you comment the following lines and run
s = imread(image);
height = size(s,1);
width = size(s,2);
m = double( s(1:1:1) ) * 8 ;
k = 1;
for i = 1 : height
for j = 1 : width
if (k <= m)
b(k) = mod(double(s(i,j)),2);
k = k + 1;
end
end
end
How is the hidden text retrieved then???
rose del
rose del el 12 de Mzo. de 2019
hello .. i'm looking for a code of styganography using linear codes like hamming or simplexe thnx .
Walter Roberson
Walter Roberson el 13 de Mzo. de 2019
That sounds like a university assignment that you would be expected to do your own work for.
Naveen Gunnam
Naveen Gunnam el 5 de Mzo. de 2022
Editada: Naveen Gunnam el 5 de Mzo. de 2022
thank you so much dude ,
faiz ul amin
faiz ul amin el 20 de Abr. de 2024
dear walter roberson i need some help.i need code for my project.
project title:data hiddng using 3d image as cover.) i want to hide data in 3d image.

Iniciar sesión para comentar.

supriya
supriya el 10 de Mzo. de 2013
Editada: supriya el 10 de Mzo. de 2013

1 voto

Actually wat i did is..made an array of the the positions of the pixels whose lsb wud be changed due to the encryption method and then did some changes in ur decryption method..see
c = imread('C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg');
message = 'hellokarthick'
message = strtrim(message);
m = length(message) * 8;
AsciiCode = uint8(message);
binaryString = transpose(dec2bin(AsciiCode,8));
binaryString = binaryString(:);
N = length(binaryString);
b = zeros(N,1); %b is a vector of bits
for k = 1:N
if(binaryString(k) == '1')
b(k) = 1;
else
b(k) = 0;
end
end
s = c;
height = size(c,1);
width = size(c,2);
k = 1; Array=[];l=1;my=1;
for i = 1 : height
for j = 1 : width
LSB = mod(double(c(i,j)), 2);
if (k>m || LSB == b(k))
s(i,j) = c(i,j);
l=k+1;
else
if(LSB == 1)
s(i,j) = c(i,j) - 1;
else
s(i,j) = c(i,j) + 1;
Array(my)=l;
l=l+1;
my= my + 1;
end
k = k + 1;
end
end
end
imwrite(s, 'hiddenmsgimage.bmp');
Retriever code changes:
k = 1;my=1;ur=1;
for i = 1 : height
for j = 1 : width
if( k<=m )
if (my<numel(Array) && Array(my)==ur)
b(k)=~(mod(double(s(i,j)),2));
else
b(k) = mod(double(s(i,j)),2);
end
k = k + 1;
my= my + 1;
end
ur=ur+1;
end
end

9 comentarios

supriya
supriya el 10 de Mzo. de 2013
but all in vain!!
Kailas
Kailas el 12 de Abr. de 2013
Editada: Kailas el 12 de Abr. de 2013
why the msg converted is not in textual format.? plz tell me why u took binValues = [ 128 64 32 16 8 4 2 1 ]; and binMatrix = reshape(binaryVector,8,100);
Pradeep Mallick
Pradeep Mallick el 15 de Ag. de 2014
Also I am facing this same problem i.e. ?? Error using ==> reshape To RESHAPE the number of elements must not change.
Error in ==> newsteganpgraphy at 97 binMatrix = reshape(binaryVector,8,100);
Your binaryVector is not 800 elements. What does
numberOfElements = numel(binaryVector) % no semicolon.
report?
oleti kameswari
oleti kameswari el 19 de Nov. de 2017
I am not getting correct message
Sahana Konchada
Sahana Konchada el 12 de Mzo. de 2018
I am getting while running the code in the line 29 as
Error using dec2bin Too many input arguments. Error in Receiverside (line 29) binaryString = transpose(dec2bin(AsciiCode,8));
what should I replace to get the correct output??
Which MATLAB version are you using? What shows up for
which -all dec2bin
Hammad RIaz
Hammad RIaz el 2 de Mayo de 2022
where to put this code, I need the complete code please. So far Not even a single submitted code worked for me. Can any body provide a single working code along with cover/ sego images please.
Retriever code changes:
k = 1;my=1;ur=1;
for i = 1 : height
for j = 1 : width
if( k<=m )
if (my<numel(Array) && Array(my)==ur)
b(k)=~(mod(double(s(i,j)),2));
else
b(k) = mod(double(s(i,j)),2);
end
k = k + 1;
my= my + 1;
end
ur=ur+1;
end
end
Image Analyst
Image Analyst el 2 de Mayo de 2022
@Hammad RIaz, here is a version to hide text:
and attached is a version for images, and for audio. I know they both work as-is. No guarantees after you start making any modifications of your own.
If you have trouble I suggest you start your own thread and post your code and data.

Iniciar sesión para comentar.

ARJUN K P
ARJUN K P el 16 de Mayo de 2015

0 votos

this code is not actally work correctly.. the extraction text is wrong...pls help me
the output is:

1 comentario

Image Analyst
Image Analyst el 16 de Mayo de 2015
What's that garbage at the end? Don't paste all that stuff into the command window. Learn how to use a script.

Iniciar sesión para comentar.

Mariam Chatha
Mariam Chatha el 18 de Sept. de 2016

0 votos

how would the code change if we were doing MSB?

3 comentarios

Walter Roberson
Walter Roberson el 18 de Sept. de 2016
MSB = c(i,j) / 128;
and the value to add or subtract would be 128 instead of 1.
Shah
Shah el 17 de Sept. de 2017
I need the MATLAB code for MSB hiding text in image stegnography
Image Analyst
Image Analyst el 18 de Sept. de 2017
See my code for hiding in the LSB http://www.mathworks.com/matlabcentral/fileexchange/54155-encoding-text-into-image-gray-levels Of course if you hid it in the most significant bit, that would corrupt the image quite a bit. But nonetheless, you can use it to do that if you want.

Iniciar sesión para comentar.

Aishwarya Rajan
Aishwarya Rajan el 30 de Nov. de 2016

0 votos

Can anyone please explain how the LSB embedding part of the above code works?
Swati Nagpal
Swati Nagpal el 21 de Jul. de 2018

0 votos

Their is problem with the retrieval code it is showing different unexpected results. So if anyone got the correct result please do share the code. swatinagpal087@gmail.com
Muhammad Asfandyar Shahid
Muhammad Asfandyar Shahid el 9 de Ag. de 2018

0 votos

please someone send correct code of image stegnography i will be thankfull

5 comentarios

Muhammad Asfandyar Shahid
Muhammad Asfandyar Shahid el 9 de Ag. de 2018
Editada: Walter Roberson el 9 de Ag. de 2018
i have code of lsb but in retrieving garbage data has come kindly correct it.
whats wrong in retrieving code??
s = imread('image1.jpg');
height = size(s,1);
width = size(s,2);
m = double( s(1:1:1) ) * 8 ;
k = 1;
for i = 1 : height
for j = 1 : width
if (k <= m)
b(k) = mod(double(s(i,j)),2);
k = k + 1;
end
end
end
binaryVector = b;
binValues = [ 128 64 32 16 8 4 2 1 ];
binaryVector = binaryVector(:);
if mod(length(binaryVector),8) ~= 0
error('Length of binary vector must be a multiple of 8.');
end
binMatrix = reshape(binaryVector,8,[]);
textString = char(binValues*binMatrix);
disp(textString);
Walter Roberson
Walter Roberson el 9 de Ag. de 2018
The most obvious problem you are having is that you are using jpg . jpg uses lossy compression unless you specifically ask otherwise, so the data you write into a jpg is not the data you get back out.
Muhammad Asfandyar Shahid
Muhammad Asfandyar Shahid el 9 de Ag. de 2018
ok correct my code and send it back.
Walter Roberson
Walter Roberson el 9 de Ag. de 2018
"ok correct my code and send it back."
No. I posted the link that will get you to a list of over 300 steganography postings. Some of them have complete code, and others have discussions of how you would need to deal with situations such as yours. I have no reason to do your work.

Iniciar sesión para comentar.

Souradeep Mukhopadhyay
Souradeep Mukhopadhyay el 31 de Jul. de 2020
Editada: Walter Roberson el 19 de En. de 2021

0 votos

% Clear the existing workspace
clear all;
% Clear the command window
clc;
% Read the input image
input = imread('peppers.png');
% Convert image to greyscale
input=rgb2gray(input);
% Resize the image to required size
input=imresize(input, [512 512]);
% Message to be embedded
message='geeksforgeeks';
% Length of the message where each character is 8 bits
len = length(message) * 8;
% Get all the ASCII values of the characters of the message
ascii_value = uint8(message);
% Convert the decimal values to binary
bin_message = transpose(dec2bin(ascii_value, 8));
% Get all the binary digits in separate row
bin_message = bin_message(:);
% Length of the binary message
N = length(bin_message);
% Converting the char array to numeric array
bin_num_message=str2num(bin_message);
% Initialize output as input
output = input;
% Get height and width for traversing through the image
height = size(input, 1);
width = size(input, 2);
% Counter for number of embedded bits
embed_counter = 1;
% Traverse through the image
for i = 1 : height
for j = 1 : width
% If more bits are remaining to embed
if(embed_counter <= len)
% Finding the Least Significant Bit of the current pixel
LSB = mod(double(input(i, j)), 2);
% Find whether the bit is same or needs to change
temp = double(xor(LSB, bin_num_message(embed_counter)));
% Updating the output to input + temp
output(i, j) = input(i, j)+temp;
% Increment the embed counter
embed_counter = embed_counter+1;
end
end
end
% Write both the input and output images to local storage
% Mention the path to a folder here.
imwrite(input, 'path_to_folder\originalImage.png');
imwrite(output, 'path_to_folder\stegoImage.png');

2 comentarios

Ishara Kariyawasam
Ishara Kariyawasam el 19 de En. de 2021
How to decrypt these??
Walter Roberson
Walter Roberson el 19 de En. de 2021
There is a legal distinction between steganography, which "hides" data, and "encryption". If you need to decrypt data then you need to have encrypted it... and we cannot talk about encryption here for legal reasons.

Iniciar sesión para comentar.

Hassan Vakani
Hassan Vakani el 15 de Mzo. de 2021

0 votos

I know it is a quite late. First of all thank you for the code. Your logic doesn't have any problem. It is just the placement of the counter which is k=k+1. It should outside the outer if statement and it works.
for i = 1 : height
for j = 1 : width
LSB = mod(double(c(i,j)), 2);
if (k>m || LSB == b(k))
s(i,j) = c(i,j);
else
if(LSB == 1)
s(i,j) = c(i,j) - 1;
else
s(i,j) = c(i,j) + 1;
end
end
k = k + 1;
end
end
imwrite(s, 'hiddenmsgimage.bmp');
No need for adding the b(k) or anything else
PoXiao
PoXiao el 7 de Mzo. de 2022

0 votos

Why did I run Retriever Coding and get garbled code instead of my original string, and how did I extract the original image and display it? I'm really at my wit's end
s = imread('C:\Users\hiddenmsgimage.png');
height = size(s,1);
width = size(s,2);
m = double( s(1:1:1) ) * 8 ;
k = 1;
for i = 1 : height
for j = 1 : width
if (k <= m)
b(k) = mod(double(s(i,j)),2);
k = k + 1;
end
end
end
binaryVector = b;
binValues = [ 128 64 32 16 8 4 2 1 ];
binaryVector = binaryVector(:);
if mod(length(binaryVector),8) ~= 0
error('Length of binary vector must be a multiple of 8.');
end
binMatrix = reshape(binaryVector,8,[]);
textString = char(binValues*binMatrix);
msgbox(textString, 'message');
% disp(textString);

Preguntada:

el 3 de Mzo. de 2013

Comentada:

el 20 de Abr. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by