LSB算法隐藏图片信息MATLAB

Github里的LSB代码

嵌入算法

function [] = LSB_embed(host, data)
% [] = LSB_embed(host, data)
% host: the host picture's path and name
% data: the data picture's path and name
% LSB in steganography (embed)
%
% Author: Moming
% 2016-03-21

lsb = 3;
host_image = imread(host);
data_image = imread(data);

len = length(dec2bin(max(size(data_image))));
len = dec2bin(len, 9);  % the length of max(height, width) < 2^999
image_info = dec2bin(size(data_image));
info = strjoin(cellstr(image_info)', '');

info_len = length(info) / lsb;

% is host picture big enough?

if numel(data_image) * 3 + info_len + 1 > numel(host_image) warning('The host picture is too small to hide the data picture!'); return;endmsg_bin = dec2bin(data_image, 8); % convert to binarymsg = blanks(9);for i = 1 : size(msg_bin, 1) msg(i, :) = strcat(msg_bin(i, :), char(mod(i, 2) + '0'));endmsg = strjoin(cellstr(msg)', '');
% change the last bit as the data end tag
msg(end) = char(mod(size(msg_bin, 1) + 1, 2) + '0'); 

tmp_len = blanks(3);
for i = 1 : 3
    % convert to decimal (len)
    tmp_len(i) = char(bin2dec(len((i - 1) * lsb + 1 : i * lsb)) + '0');
end

tmp_info = blanks(info_len);
for i = 1 : info_len
    % convert to decimal (info)
    tmp_info(i) = char(bin2dec(info((i - 1) * lsb + 1 : i * lsb)) + '0'); 
end

data_len = length(msg) / lsb;
tmp_data = blanks(data_len);
for i = 1 : data_len
    % convert to decimal (data)
    tmp_data(i) = char(bin2dec(msg((i - 1) * lsb + 1 : i * lsb)) + '0'); 
end

result = host_image;
rgb = 1;
[len_R, len_G, len_B] = size(result);

% hide len
for i = 1 : 3
    result(len_R, len_G, i) = result(len_R, len_G, i) - ...
        mod(result(len_R, len_G, i), 2^lsb) + double(tmp_len(i) - '0');
end

% hide info
for R = len_R : -1 : 1
    for G = len_G : -1 : 1
        if R == len_R && G == len_G
            continue;
        end
        for B = len_B : -1 : 1
            if rgb <= info_len
                result(R, G, B) = result(R, G, B) - mod(result(R, G, B),...
                    2^lsb) + double(tmp_info(rgb) - '0');
                rgb = rgb + 1;
            end
        end
    end
end

% hide data
rgb = 1;
for R = 1 : len_R
    for G = 1 : len_G
        for B = 1 : len_B
            if rgb <= data_len
                % only to be consistent with front: '0'
                result(R, G, B) = result(R, G, B) - mod(result(R, G, B),...
                    2^lsb) + double(tmp_data(rgb) - '0');
                rgb = rgb + 1;
            end
        end
    end
end

imshow(result);
imwrite(result, 'result.png');  % do not use jpg

end

提取算法

function [] = LSB_extract(name)
% LSB_extract(name)
% name: the picture's path and name
% LSB in steganography (extract)
%
% Author: Moming
% 2016-03-21

image = imread(name);

lsb = 3;
[len_R, len_G, len_B] = size(image);
flag = char('0');

tmp_len = blanks(3);
for i = 1 : 3
    tmp_len = strcat(tmp_len, mod(image(len_R, len_G, i), 2^lsb) + '0');
end
len = sum(bin2dec(strjoin(cellstr(dec2bin(tmp_len - '0', 3))', '')));% get the size of hide pictureindex = 1;tmp_info = blanks(len);for R = len_R : -1 : 1 if index > len break; end for G = len_G : -1 : 1 if index > len break; end if R == len_R && G == len_G continue; end for B = len_B : -1 : 1 if index > len break; end tmp_info(index) = mod(image(R, G, B), 2^lsb) + '0'; index = index + 1; end endendcmd = strjoin(cellstr(dec2bin(tmp_info - '0'))', '');
image_size = zeros(1, 3);
for i = 1 : 3
    image_size(i) = bin2dec(cmd((i - 1) * len + 1 : i * len));
end

% get the hide picture
index = 1;
picture = zeros(image_size);
for R = 1 : len_R
    for G = 1 : len_G
        tmp = blanks(0);
        for B = 1 : len_B
             % '0' is useful!!! Placeholder...
            tmp = strcat(tmp, mod(image(R, G, B), 2^lsb) + '0');
        end
        tmp_bin = dec2bin(tmp - '0', 3)'; picture(index) = bin2dec(tmp_bin(1 : 8)); if flag + tmp_bin(9) ~= 97 % '0'/'1' is the end tag % recover the picture picture = uint8(picture); % !!! very important !!! imwrite(picture, 'recover.png');
            imshow(picture);
            return;
        end
        index = index + 1;
        flag = tmp_bin(9);
    end
end

end

二维嵌入算法

function [] = LSB_embed(name, message, lsb, color)
% LSBembed(name, message, lsb) LSB in steganography (embed)
% name: the picture's path and name
% message: the data you want to hide in the picture
% lsb: lsb-rightmost LSBs
% color: 1-red, 2-green, 3-blue
%
% Author: Moming
% 2016-03-16

image = imread(name);
msg_origin = unicode2native(strcat(message, char(4)), 'UTF-8');  % UTF-8 encode, 'EOT' is the end tag
msg_bin = dec2bin(msg_origin, 8);  % convert to binary
msg = strjoin(cellstr(msg_bin)','');

len = length(msg) / lsb;

while len ~= fix(len) strcat(msg, char(4)); 

len = length(msg) / lsb;

endtmp = blanks(len);

for i = 1 : len 

tmp(i) = char(bin2dec(msg((i - 1) * lsb + 1 : i * lsb)) + '0'); 

% '0' is a kind of placeholderend

% use Red, Green or Bluelayer = image(:, :, color);

for i = 1 : len 

layer(i) = layer(i) - mod(layer(i), 2^lsb) + double(tmp(i) - '0'); 

% only to be consistent with frontend

% save the pictureimage

猜你喜欢

转载自blog.csdn.net/weixin_39338645/article/details/79911682