我正在使用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发布于 2019-12-26 05:15:47
你有一个小错误:
在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);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%输出:
computerhttps://stackoverflow.com/questions/59481178
复制相似问题