Extracting a hidden message from an image by using LSB algorithm

41 views (last 30 days)
I have an image that has a hidden message embedded in it, which should be extracted by using the LSB algorithm. However, the extracted hidden message that I obtained through the attached code does not make sense. It would be appreciated if anyone could point out what I am doing wrong here.
I used the following code to extract the hidden message:
s = imread('image_result.bmp');
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);
And the extracted message that I got:
  6 Comments
Rik
Rik on 1 Jun 2021
You already gave people permission to copy your code when you posted your question:
"You agree that Content that you contribute to Discussions will be licensed under the Creative Commons Attribution Share Alike 3.0 license." [Terms of Use]
If people pass off your code as their own they are violating the attribution part of the CC license, and they are probably commiting academic fraud. Neither of the two is necessarily your problem. If you didn't want your work being public, you shouldn't have posted it.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 18 May 2021
It looks like the hidden message is encoded into the last two rows of the image:
I tried a couple of ways of decoding it
  1. Using a single color channel
  2. Using all 3 color channels.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
workspace; % Make sure the workspace panel is showing.
powerOf2Values = [128 64 32 16 8 4 2 1];
rgbImage = imread('image_result.bmp');
nexttile
imshow(rgbImage, [], 'InitialMagnification', 800);
axis('on', 'image');
[r, g, b] = imsplit(rgbImage);
r2 = mod(r, 2);
nexttile
imshow(r2, [], 'InitialMagnification', 800);
axis('on', 'image');
g2 = mod(g, 2);
nexttile
imshow(g2, [], 'InitialMagnification', 800);
axis('on', 'image');
b2 = mod(g, 2);
nexttile
imshow(b2, [], 'InitialMagnification', 800);
axis('on', 'image');
% Get last 2 lines of each channel
last2LinesR = r2(end-1:end, :);
last2LinesG = g2(end-1:end, :);
last2LinesB = b2(end-1:end, :);
%-------------------------------------------------------------------
% Try to (guess at) extract message from a single color channels
% Reshape into one long row vector
last2LinesR = reshape(last2LinesR, 1, []);
% Reshape into one long column vector
% last2Lines = reshape(last2Lines, [], 1);
% Reshape into an N-by-8 matrix where each row is an character value.
last2LinesR = reshape(last2LinesR, [], 8)
for row = 1 : size(last2LinesR, 1)
thisRow = double(last2LinesR(row, :));
value = thisRow .* powerOf2Values;
asciiValue(row) = uint16(sum(value));
end
asciiValue
fprintf('------------------------------------------------------------\n');
fprintf('%c', asciiValue);
fprintf('\n');
fprintf('------------------------------------------------------------\n');
%-------------------------------------------------------------------
% Try to (guess at) extract message from all 3 channels
% Reshape into one long row vector
last2LinesR = reshape(last2LinesR, 1, []);
last2LinesG = reshape(last2LinesG, 1, []);
last2LinesB = reshape(last2LinesB, 1, []);
% Stack vertically
m = [last2LinesR; last2LinesG; last2LinesB];
% Reshape into one long row vector
m = reshape(m, 1, []);
% Reshape into an N-by-8 matrix where each row is an character value.
last2LinesR = reshape(m, [], 8)
for row = 1 : size(last2LinesR, 1)
thisRow = double(last2LinesR(row, :));
value = thisRow .* powerOf2Values;
asciiValue(row) = uint16(sum(value));
end
asciiValue
fprintf('------------------------------------------------------------\n');
fprintf('%c', asciiValue);
fprintf('\n');
fprintf('------------------------------------------------------------\n');
Neither way seems to produce a sensible message so I must now be guessing the correct way to decode it. Are you sure you don't know it either? We could spend hours guessing.
  2 Comments
Shahrin Rahman
Shahrin Rahman on 18 May 2021
Edited: Shahrin Rahman on 21 May 2021
Thank you again for taking a look into my problem. I appreciate it a lot that you are still helping me.
Image Analyst
Image Analyst on 20 May 2021
OK. Good luck. I tried that with the second half of my code where I took one bit from the red, one from the green, and one from the blue. It didn't make any sense. But maybe the bits need to be flipped from what I had, or maybe the order was BGR instead of RGB, so there are still some things you can try by adapting my code.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 16 May 2021
Maybe you're not extracting the hidden message in the proper way for the way that they hid it in the image. Do you have the code that was used to hide the watermark message in the cover image?
I'm attaching two demos for you to study and adapt if you want.
  3 Comments
Image Analyst
Image Analyst on 16 May 2021
Well you can get the LSB of an image with bitget(). See attached demo. But since it's not an image, but a text, you're going to have to take every 8 pixels (either in a row or column) and convert it to an ASCII character or a unicode character. You might have to try all 4 permutations to see which way gives sensible text. Then when you see the text stop making sense, that is probably where the hidden image ends and where the normal image values begin. I think you can do it, right?
Shahrin Rahman
Shahrin Rahman on 17 May 2021
Thank you again very much for such a broad answer. However, unfortunately I'm quite not sure how I can try the 4 permutations in my code. On the other hand, I tried to make modifications a number of times in my given code, but I still made no progress.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!