LSB加密和解密

问题描述 投票:0回答:1

我正在使用LSB加密方法来隐藏图像中的数据。当我尝试获取解密的消息时,输出消息看起来与原始消息不一样。我在做什么错以及如何解决?

function imagehide()
% hide message in image
m = 'computer';
im = imread('lena_1.png');
im = rgb2gray(im);
imtool(im);
a = dec2bin(m,8);
a = a(:);
m = dec2bin(im);
for i=1:size(a)
    m(1,8)=a(i);
end;
k = bin2dec(m);
k = reshape(k,256,256);
k = uint8(k);
imwrite(k,'ste.png');
imtool(k);
% get message from image%
a = imread('ste.png');
sm = 8;
s = dec2bin(a);
for i = 1:sm*8;
    l(i) = s(i,8);
end
n = reshape(l,sm,8);
n = bin2dec(n);
n = char(n);
n = n';
disp(n);
end
matlab encryption
1个回答
1
投票

您有一个小错误:

for i=1:size(a)中,您正在使用m(1,8)=a(i);而不是m(i,8)=a(i);

我使用了cameraman.tif,因为我没有lena_1.png

%Encryption
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
m = 'computer';
% im = imread('lena_1.png');
% im = rgb2gray(im);
% imtool(im);
im = imread('cameraman.tif');
a = dec2bin(m,8);
a = a(:);
m = dec2bin(im);
for i=1:size(a)
    %m(1,8)=a(i); %Bug!!!
    m(i,8)=a(i);
end
k = bin2dec(m);
k = reshape(k,256,256);
k = uint8(k);
imwrite(k,'ste.png');
%imtool(k);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Decryption
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get message from image
a = imread('ste.png');
sm = 8;
s = dec2bin(a);
l = char(zeros(1, sm*8)); %Just nicer - allocate space for l
for i = 1:sm*8
    l(i) = s(i,8);
end
n = reshape(l,sm,8);
n = bin2dec(n);
n = char(n);
n = n';
disp(n);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

输出:

computer
© www.soinside.com 2019 - 2024. All rights reserved.