⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 基于BMP图像的信息隐藏
💻 CPP
📖 第 1 页 / 共 4 页
字号:
unsigned long int result=0; 

dataread=fscanf(strm,"%c",&tmpchar); 
if (dataread==1) 
result+=((unsigned long int)tmpchar*1); 
dataread=fscanf(strm,"%c",&tmpchar); 
if (dataread==1) 
result+=((unsigned long int)tmpchar*256); 
dataread=fscanf(strm,"%c",&tmpchar); 
if (dataread==1) 
result+=((unsigned long int)tmpchar*65536); 
dataread=fscanf(strm,"%c",&tmpchar); 
if (dataread==1) 
result+=((unsigned long int)tmpchar*16777216); 

return result; 

} 

unsigned int calcEOLpad(int bitlen, long linelen) { 
unsigned long int bitsperline; 
unsigned int padder; 

bitsperline=(unsigned long)bitlen*(unsigned long)linelen; 
padder=bitsperline%32; 
padder=32-padder; 

if (padder==32) 
padder=0; 

return (int)(padder/8); 
} 

PIXEL getIndexedPixel(unsigned long int idx, BITMAP* bmp) { 
unsigned long int offset; 
PIXEL result; 
unsigned int EOLpad; 
unsigned long int x; 
unsigned long int y; 

EOLpad=calcEOLpad(24,bmp->x_size); 
x=idx%(bmp->x_size); 
y=(idx-x)/(bmp->x_size); 

y=(bmp->y_size-1)-y; 

offset=(y*((bmp->x_size*3)+EOLpad))+(x*3); 

result.blue=*(bmp->raster->dataspace+(offset++)); 
result.green=*(bmp->raster->dataspace+(offset++)); 
result.red=*(bmp->raster->dataspace+(offset)); 

return result; 
} 

void setIndexedPixel(unsigned long int idx, BITMAP* bmp, PIXEL pix) { 
unsigned long int offset; 
unsigned int EOLpad; 
unsigned long int x; 
unsigned long int y; 

EOLpad=calcEOLpad(24,bmp->x_size); 
x=idx%(bmp->x_size); 
y=(idx-x)/(bmp->x_size); 

y=(bmp->y_size-1)-y; 

offset=(y*((bmp->x_size*3)+EOLpad))+(x*3); 

*(bmp->raster->dataspace+(offset++))=pix.blue; 
*(bmp->raster->dataspace+(offset++))=pix.green; 
*(bmp->raster->dataspace+(offset))=pix.red; 

} 
/****************DATA CODING **********************************/ 
void encodeData(BITMAP* dest, HEAP* data, LOOKUP* hotspots) { 
/* encodes data bit by bit into dest */ 
/* hotspots contains actual data storage locations */ 

unsigned long int i; 
unsigned long int heapitem; 
PIXEL blititem; 
int bitshift; 
unsigned char theBool; 
unsigned char dom; 

bitshift=0; 
heapitem=0; 

for (i=0; i< hotspots->curitem; i++) { 
/* get next bit */ 
theBool=getNextHeapBit(heapitem,bitshift,data); 
bitshift++; 
if (bitshift==8) { 
bitshift=0; 
heapitem++; 
if (heapitem >data->heaplen) 
break; 
} 
blititem=getIndexedPixel(*(hotspots->dataspace+i),dest); 
dom=biggest_of_3(blititem.red, blititem.green, blititem.blue); 
if (dom==blititem.red) { 
if (theBool==1) { 
/* R */ 
if (dom==255) 
dom--; 
else 
dom++; 
blititem.red=dom; 
} 
} 
else if (dom==blititem.green) { 
if (theBool==1) { 
/* G */ 
if (dom==255) 
dom--; 
else 
dom++; 
blititem.green=dom; 
} 
} 
else { 
if (theBool==1) { 
/* B */ 
if (dom==255) 
dom--; 
else 
dom++; 
blititem.blue=dom; 
} 
} 
setIndexedPixel(*(hotspots->dataspace+i),dest,blititem); 
} 
} 


unsigned char getNextHeapBit (unsigned long int item, int bit, HEAP* data) { 
unsigned char thisitem; 
int bitshift; 

bit=7-bit; /* reverse bit order */ 

thisitem=*(data->dataspace+item); 
bitshift=pow(2,bit); 

thisitem=thisitem&bitshift; 
thisitem=thisitem >>bit; 

return thisitem; 
} 

int decodeData(BITMAP* src, HEAP* dest) { 
unsigned long offset,tmp; 
unsigned char thisR; 
unsigned char thisG; 
unsigned char thisB; 
unsigned char lookaheadR; 
unsigned char lookaheadG; 
unsigned char lookaheadB; 
int dom,lookaheaddom; 
int keepflag,keepflag2; 
unsigned long int hits; 
PIXEL getdata; 
PIXEL controlPixel, thisPixel; 
int theBool; 
unsigned long int longtmp; 
int currentBit; 
int bitshifts; 

offset=0; 
currentBit=0; 
bitshifts=0; 

while (offset<(src->x_size*src->y_size)) { 
getdata=getIndexedPixel(offset, src); 
thisR=getdata.red; 
thisG=getdata.green; 
thisB=getdata.blue; 

dom=biggest_of_3(thisR,thisG,thisB); 
keepflag=FALSE; 

/* If 1 col varies from others by at least 3 = > candidate */ 
if (dom==thisG && abs(thisG-thisR) >2 && abs(thisG-thisB) >2) 
keepflag=TRUE; 
if (dom==thisB && abs(thisB-thisR) >2 && abs(thisB-thisG) >2) 
keepflag=TRUE; 
if (dom==thisR && abs(thisR-thisB) >2 && abs(thisR-thisG) >2) 
keepflag=TRUE; 
if (keepflag==FALSE) { 
offset++; 
continue; 
} 

/* only one of the 3 cols=dom here, and that col is at least 
3 points greater than the others */ 

/* see if dominant col continues on more pixels(+ or - 1) and that other 
cols dont challenge its dominance */ 

tmp=offset+1; 
hits=1; 
while (tmp<(src->x_size*src->y_size)) { 
getdata=getIndexedPixel(tmp, src); 
lookaheadR=getdata.red; 
lookaheadG=getdata.green; 
lookaheadB=getdata.blue; 
keepflag=FALSE; 
if (dom==thisG && abs(lookaheadG-thisG)< 2) 
keepflag=TRUE; 
if (dom==thisR && abs(lookaheadR-thisR)< 2) 
keepflag=TRUE; 
if (dom==thisB && abs(lookaheadB-thisB)< 2) 
keepflag=TRUE; 

/* check dom is still dominant col by at least 3 */ 
lookaheaddom=biggest_of_3(lookaheadR,lookaheadG,lookaheadB); 
keepflag2=FALSE; 
if (thisR==dom && lookaheadR==lookaheaddom && abs(lookaheadR-lookaheadG) >2 && abs(lookaheadR-lookaheadB) >2) 
keepflag2=TRUE; 
if (thisG==dom && lookaheadG==lookaheaddom && abs(lookaheadG-lookaheadR) >2 && abs(lookaheadG-lookaheadB) >2) 
keepflag2=TRUE; 
if (thisB==dom && lookaheadB==lookaheaddom && abs(lookaheadB-lookaheadR) >2 && abs(lookaheadB-lookaheadG) >2) 
keepflag2=TRUE; 

if (keepflag==FALSE || keepflag2==FALSE) 
break; 
hits++; 
tmp++; 
} 

if (hits >2) { 
/* store in lookup table */ 
for (longtmp=offset+2; longtmp< offset+hits; longtmp++) { 
controlPixel=getIndexedPixel(offset,src); 
thisPixel=getIndexedPixel(longtmp,src); 
dom=biggest_of_3(controlPixel.red, controlPixel.green, controlPixel.blue); 
if (dom==controlPixel.red) { 
if (thisPixel.red-dom==0) 
theBool=0; 
else 
theBool=1; 
} 
if (dom==controlPixel.green) { 
if (thisPixel.green-dom==0) 
theBool=0; 
else 
theBool=1; 
} 
if (dom==controlPixel.blue) { 
if (thisPixel.blue-dom==0) 
theBool=0; 
else 
theBool=1; 
} 
currentBit=currentBit<< 1; 
currentBit+=theBool; 
bitshifts++; 
if (bitshifts==8) { 
if (!putToHeap((char)currentBit,dest)) 
return 2; 
bitshifts=0; 
currentBit=0; 
} 
} 
offset+=hits; 
} 
else { 
offset++; 
} 
} 

/* decoded and in heap */ 
if (dest->heaplen< 13) 
return 1; 
if (!(*(dest->dataspace)=='B') || (*(dest->dataspace+1)!='S' && *(dest->dataspace+1)!='E')) 
return 1; 

if (*(dest->dataspace+6) >THIS_PROTO_VER) 
return 3; 
/* set data length by truncation */ 
longtmp=0; 
longtmp+=((unsigned long int)(*(dest->dataspace+2))*1); 
longtmp+=((unsigned long int)(*(dest->dataspace+3))*256); 
longtmp+=((unsigned long int)(*(dest->dataspace+4))*65536); 
longtmp+=((unsigned long int)(*(dest->dataspace+5))*16777216); 
if (longtmp<=dest->heaplen) 
dest->heaplen=longtmp; 
else 
return 1; 

return 0; 
} 

/*************************** ENCRYPTION ROUTINES ***********************/ 
int hash_func(unsigned char* password) { 

/* This hash function steps thru the string, storing the */ 
/* lowest 4 bits, and shifting the running total left 4 bits */ 
/* The shift is a rotational one, e.g. the top nibble comes */ 
/* back in the bottom (LSB) nibble */ 


unsigned int hash_ttl=0; 
unsigned int ptr=0; 
char this_chr; 

while (password[ptr] != '\0') { 

/* Step through string one char at a time */ 
this_chr=password[ptr++]; 

/* Add lowest 4 bits of character */ 
hash_ttl=hash_ttl + (this_chr & 0x0f); 

/* Rotate hash total 4 bits to left (to MSB) */ 
hash_ttl = rotl4 (hash_ttl); 
} 


return (hash_ttl); 
} 





int rotl4(int inval) { 

/* Rotate 16bit integer INVAL left by 4 bits */ 

unsigned int result; 
unsigned int tmp; 

tmp = inval & 0xf000; /* MSB nibble of the 4 */ 
result = inval & 0xfff; /* 3 LSB nibbles of the 4 */ 
result <<= 4; /* Shift nibs 2-0 to 3-1 posns */ 
tmp >>= 12; /* Shift nib 3 to nib 0 posn */ 

result = result + tmp; /* Add nib 0 into 3-1 value */ 
return (result); 

} 

/*void cryptoData(HEAP* data, unsigned char* password) { 
unsigned long int i; 
unsigned int passhash; 
unsigned char pupper,plower; 
int alternhash=FALSE; 


passhash=hash_func(password); 

pupper=passhash&0xff00; 
plower=passhash&0xff; 

*(data->dataspace+1)='E'; 

for (i=7; i< data->heaplen; i++) { 
if (alternhash) { 
*(data->dataspace+i)=xor(*(data->dataspace+i),pupper); 
alternhash=FALSE; 
} 
else { 
*(data->dataspace+i)=xor(*(data->dataspace+i),plower); 
alternhash=TRUE; 
} 
} 


} 
*/
unsigned char xor(unsigned char A, unsigned char B) { 
unsigned char t1,t2,t3; 

t1=A|B; 
t2=A&B; 
t3=t1-t2; 
return ~t3; 

} 

/************************************POW******************************/ 

int pow (int a, int b) { 

int result=1; 
int lp; 

for (lp=b; lp >0; lp--) { 
result=result*a; 
} 

return result; 

} 

/*******************************STRICMP*******************************/ 
int stricmp (const char* a, const char* b) { 

int i; 
char acmp,bcmp; 

if (strlen(a)!=strlen(b)) 
return 1; 

for (i=0; i< strlen(a); i++) { 
acmp=a[i]; 
bcmp=b[i]; 
if (acmp >='a' && acmp<='z') 
acmp=acmp-0x20; 
if (bcmp >='a' && bcmp<='z') 
bcmp=bcmp-0x20; 
if (acmp!=bcmp) 
return 1; 
} 
return 0; 


} 

/**********************ARCHIVE MANAGER*******************************/ 
int formatDataspace(BITMAP* BMPworking, HEAP* filespace) { 

LOOKUP* hotspots; 
unsigned long int bit1,bit2,bit3,bit4; 
unsigned long int maxencode,longtemp,i; 

putToHeap('B',filespace); 
putToHeap('S',filespace); 
putToHeap('\0',filespace); /* archive total length inc all hdr:- changed later LONG */ 
putToHeap('\0',filespace); 
putToHeap('\0',filespace); 
putToHeap('\0',filespace); 
putToHeap(THIS_PROTO_VER,filespace); 
putToHeap('O',filespace); 
putToHeap('K',filespace); 
putToHeap('\0',filespace); /* length of this file block inc filename hdr */ 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -