LSB演算法BMP圖片資訊隱藏技術 c語言

NO IMAGE

LSB演算法是將資訊的每一位隱藏到圖片RGB單元的最後一位,由於一位的改變對於顏色影響不大,人的肉眼難以識別,從而達到資訊隱藏的效果。

具體需要了解BMP檔案格式,前54個位元組為圖片資訊,不能修改,所以從第55個位元組開始進行隱藏。具體程式碼為:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
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<stdio.h>
#include<stdlib.h>
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;
}