基于LSB图像隐藏和解密算法深度讲解
本人错误之处,欢迎指正。
LSB(LeastSignificant Bits)加密算法:将秘密信息嵌入到载体图像像素值的最低有效位,也称最不显著位,改变这一位置对载体图像的品质影响最小。
普及一下常识:RGB图像,蓝色,红色,绿色混合而成的彩色图像,读取后由三个八位m*n的矩阵组成,m*n代表图像的大小,也就是像素的多少,八位
指的是一个像素占了八位二进制的大小,转为十进制也就是0-255。最低有效位是低四位,本系统使用低四位进行储存数据。
本次仿真实验通过MATALB进行仿真,并且只分析灰色图像,原因是因为彩色图像是三个灰度图像代表了三种颜色混合而成。编写两个界面,如下图所示:


本次讲解给出的代码加密算法代码和解密算法代码,涉及到的界面算法不与给出和解释。
算法思想:1,读入一个m*n的灰度图像,把每一位的像素的低四位用或运算清0,得到我称之为的“载体图像”。
2,读入信息图像,也就是需要隐藏的图像(也可以是其他的信息),将图像的八位拆开成高四位和低四位。
3,在载体前6个的低四位数据中,放入信息图像的大小信息。
3,按照先高后低的顺序,存入载体图像,先从左到右,再换下一行。
4,解密无非就是上面步骤的逆序而已。
看完思想后,一般情况下就可以自己尝试写程序了,其实用C语言对位操作更加方便,但是,考虑到需要显示图片,所以决定使用MATLAB进行仿真。
此处只提供加密和解密的算法,供需要的人学习,界面只是单纯的调用,所以在这没必要介绍。
%%%%%%%%%加密算法%%%%%%%%%%%
clc;
close all;
clear;
Inimage=imread('2222.jpg');
Inimage=rgb2gray(Inimage);%真彩转灰度
imwrite(Inimage,'car.jpg');
figure(1);subplot(221),imshow(Inimage);title('原图');
carrierl=bitand(Inimage,240);%清空低四位
subplot(222),imshow(carrierl),title('清空低四位');
%读取信息
%text=158;
text=imread('mess3.jpg');
text=rgb2gray(text);
imwrite(text,'messss.jpg');
subplot(223),imshow(text),title('信息图');
textu8=uint8(text);
%读取输入图片的大小
[m,n]=size(Inimage);
m=double(m);
n=double(n);
%清空低四位数据
after=carrierl;
%读取信息大小
[m1,n1]=size(textu8);
%计数器
count=1;
count=double(count);
%前六位用来放信息大小的信息m1,n1
bit4=m1;
%提出高高四位
hhtext=bitand(bit4,3840);
%右移八位位
hhtext=bitshift(hhtext,-8);
%提出高四位
hightext4=bitand(bit4,240);
%右移四位
hightext4=bitshift(hightext4,-4);
%提出低四位
lowtext=bitand(bit4,15);
%计算第几行
locationy=ceil(count/n);
%计算第几列
locationx=count-(locationy-1)*n;
%放入高四位
after(locationy,locationx)=bitor(carrierl(locationy,locationx),hhtext);
count=count+1;
%计算第几行
locationy=ceil(count/n);
%计算第几列
locationx=count-(locationy-1)*n;
%放入高四位
after(locationy,locationx)=bitor(carrierl(locationy,locationx),hightext4);
count=count+1;
locationy=ceil(count/n);
locationx=count-(locationy-1)*n;
%放入低四位
after(locationy,locationx)=bitor(carrierl(locationy,locationx),lowtext);
%计数
count=count+1;
bit4=n1;
%提出高高四位
hhtext=bitand(bit4,3840);
%右移八位位
hhtext=bitshift(hhtext,-8);
%提出高四位
hightext4=bitand(bit4,240);
%右移四位
hightext4=bitshift(hightext4,-4);
%提出低四位
lowtext=bitand(bit4,15);
%计算第几行
locationy=ceil(count/n);
%计算第几列
locationx=count-(locationy-1)*n;
%放入高四位
after(locationy,locationx)=bitor(carrierl(locationy,locationx),hhtext);
count=count+1;
%计算第几行
locationy=ceil(count/n);
%计算第几列
locationx=count-(locationy-1)*n;
%放入高四位
after(locationy,locationx)=bitor(carrierl(locationy,locationx),hightext4);
count=count+1;
locationy=ceil(count/n);
locationx=count-(locationy-1)*n;
%放入低四位
after(locationy,locationx)=bitor(carrierl(locationy,locationx),lowtext);
%计数
count=count+1;
%开始载入数据
for i=1:m1
for j=1:n1
bit4=textu8(i,j);
%提出高四位
hightext4=bitand(bit4,240);
%右移四位
hightext4=bitshift(hightext4,-4);
%提出低四位
lowtext=bitand(bit4,15);
%计算第几行
locationy=ceil(count/n);
%计算第几列
locationx=count-(locationy-1)*n;
%放入高四位
after(locationy,locationx)=bitor(carrierl(locationy,locationx),hightext4);
%计数
count=count+1;
locationy=ceil(count/n);
locationx=count-(locationy-1)*n;
%放入低四位
after(locationy,locationx)=bitor(carrierl(locationy,locationx),lowtext);
%计数
count=count+1;
end
end
subplot(224),imshow(after),title('载入信息后');
%保存图片,为tif格式无损耗
imwrite(after,'after.tif');
capity=num2str(m*n+4);
messc=num2str(m1*n1);
xlabel(['总像素容量为:',capity,';载体信息大小为',messc],'Color','b');
%%%%%%%%解密算法%%%%%%%%%%%%%
clc;
clear;
close all;
Inimage2=imread('after.tif');
figure(1);subplot(221),imshow(Inimage2);title('原图');
%%%%%%读取图片的大小信息%%%%%%
%提取高高四位
m1hh=bitand(Inimage2(1,1),15);
%提取高四位
m1high=bitand(Inimage2(1,2),15);
%提取低四位
m1low=bitand(Inimage2(1,3),15);
%高四位左移
m1high=bitshift(m1high,4);
%高高四位左移八位
m1hh=double(m1hh);
m1hh2=bitshift(m1hh,8);
%合成数据
m1=double(m1high+m1low)+m1hh2;
%提取高高四位
n1hh=bitand(Inimage2(1,4),15);
%提取高四位
n1high=bitand(Inimage2(1,5),15);
%提取低四位
n1low=bitand(Inimage2(1,6),15);
%高四位左移
n1high=bitshift(n1high,4);
%高高四位左移八位
n1hh=double(n1hh);
n1hh=bitshift(n1hh,8);
%合成数据
n1=double(n1high+n1low)+n1hh;
m1=double(m1);
n1=double(n1);
%%%%%%%开始读出数据,生成图片%%%%%
%生成2个空白矩阵
messagehigh=zeros(m1,n1);
messagelow=zeros(m1,n1);
result=zeros(m1,n1);
%载体大小
[m,n]=size(Inimage2);
%初始化数值
count=1;
oddc=0;
evenc=0;
for i=1:m
for j=1:n
%信息读取结束自动跳出
if count==m1*n1*2+4
break;
%前四位已经读取
elseif(count>6)
dataout=bitand(Inimage2(i,j),15);
locount=count-6;
if rem(locount,2)==0
evenc=evenc+1;
%计算第几行
locationy=ceil(evenc/n1);
%计算第几列
locationx=evenc-(locationy-1)*n1;
messagelow(locationy,locationx)=dataout;
else
oddc=oddc+1;
%计算第几行
locationy=ceil(oddc/n1);
%计算第几列
locationx=oddc-(locationy-1)*n1;
messagehigh(locationy,locationx)=dataout;
end
end
count=count+1;
end
end
for i=1:m1
for j=1:n1
messagehigh(i,j)=bitshift(messagehigh(i,j),4);
result(i,j)=bitor( messagehigh(i,j),messagelow(i,j));
end
end
compare=imread('mess2.jpg');
compare=rgb2gray(compare);
result=uint8(result);
subplot(222),imshow(result);title('结果');
subplot(223),imshow(compare);title('信息');
compare=imread('mess.jpg');
compare=rgb2gray(compare);
浙公网安备 33010602011771号