LSB算法BMP图片信息隐藏技术 c语言

LSB算法是将信息的每一位隐藏到图片RGB单元的最后一位,由于一位的改变对于颜色影响不大,人的肉眼难以识别,从而达到信息隐藏的效果。

具体需要了解BMP文件格式,前54个字节为图片信息,不能修改,所以从第55个字节开始进行隐藏。具体代码为:

#include
#include
#include
int judge(FILE *in,FILE *fil,int &inlen,int &fillen)
{
fseek(in,0,2);
fseek(fil,0,2);
inlen = ftell(in);
fillen = ftell(fil);
if(fillen * 8 > inlen) return 0;
else return 1;
}
int main()
{
FILE *in,*out,*fil;
int i,j;
int piclen,fillen;
char inFileName[90],outFileName[90],file[90];
printf("请输入位图文件名:\n");
scanf("%s",inFileName);
printf("请输入生成位图文件名:\n");
scanf("%s",outFileName);
printf("请输入加密文件名\n");
scanf("%s",file); 
if((in = fopen(inFileName,"rb")) == NULL)
{
printf("无法打开原图文件\n");return 0;
}
if((out = fopen(outFileName,"wb")) == NULL)
{
printf("无法打开输入文件\n");return 0;
}
if((fil = fopen(file,"rb")) == NULL)
{
printf("无法打开输入文件\n");return 0;
}
if(!judge(in,fil,piclen,fillen))
{
printf("The file is too large\n");return 0;
}
fseek(in,0,0);
fseek(fil,0,0);
char ch;
char *temp = (char *)malloc(8 * fillen);
i = 0;
while(!feof(fil))
{
ch = fgetc(fil);
for(j = 0;j < 8;j ++) temp[i ++] = 0x01 & (ch >> j);
}
j = 0;
for(i = 1;!feof(in);i ++)
{
if(i <= 54) fputc(fgetc(in),out);//前54个保持不变
else
{
if(j < fillen * 8) fputc((fgetc(in) & 0xfe) + temp[j ++],out);
else fputc(fgetc(in) & 0xfe,out);
}
}
return 0;
}


解密代码原理类似,代码为

#include
#include
int main()
{
FILE *in,*out;
char inf[90],outf[90];
char temp,ch;
printf("请输入位图名称:\n");
scanf("%s",inf); 
printf("请输入提取出保存的文件名\n"); 
scanf("%s",outf);
if((in = fopen(inf,"rb")) == NULL)
{
printf("位图文件无法打开\n");
return 0; 

if((out = fopen(outf,"wb")) == NULL)
{
printf("rar无法打开\n");
return 0; 

fseek(in,54L,0);
ch = fgetc(in);
while(ch)
{
for(int j = 0;j < 8;j ++)
{
temp += (ch & 0x01) << j;
ch = fgetc(in);
}
fputc(temp,out);
temp = 0;
}
return 0;
}


你可能感兴趣的:(C语言,图片信息隐藏)