345 views (last 30 days)

Show older comments

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

Maged Rawash
on 25 Apr 2015

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 ...

Walter Roberson
on 13 Mar 2019

That sounds like a university assignment that you would be expected to do your own work for.

supriya
on 10 Mar 2013

Edited: supriya
on 10 Mar 2013

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

Walter Roberson
on 12 Mar 2018

Which MATLAB version are you using? What shows up for

which -all dec2bin

ARJUN K P
on 16 May 2015

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

the output is:

Image Analyst
on 16 May 2015

Mariam Chatha
on 18 Sep 2016

how would the code change if we were doing MSB?

Image Analyst
on 18 Sep 2017

Aishwarya Rajan
on 30 Nov 2016

Can anyone please explain how the LSB embedding part of the above code works?

Swati Nagpal
on 21 Jul 2018

Muhammad Asfandyar Shahid
on 9 Aug 2018

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

Walter Roberson
on 9 Aug 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.

Souradeep Mukhopadhyay
on 31 Jul 2020

Edited: Walter Roberson
on 19 Jan 2021

% 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');

Walter Roberson
on 19 Jan 2021

Hassan Vakani
on 15 Mar 2021

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

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

Start Hunting!