Scarica il sorgente database/100-hide_bytes_in_bmp.cpp
  1. //*************************************//
  2. //******** created by mamo139 *********//
  3. //*** http://mamo139.altervista.org ***//
  4. //*************************************//
  5.  
  6. #include <stdio.h>
  7. #include <windows.h>
  8.  
  9. unsigned char* dammibit(unsigned char numero);
  10. unsigned char dammichar(unsigned char *bits);
  11. /*
  12.   char *bits;
  13.   bits = new char [8];
  14.   bits = dammibit(154);
  15. */
  16.  
  17. int main (void){
  18.  
  19. char nomefileb[20];
  20. printf("nome del file base:");
  21. scanf("%s",nomefileb);
  22. printf("%s!\n",nomefileb);
  23. //peso supportabile dal file base con steganografia 3-3-2bit
  24. FILE * pFile;
  25. long lSize;
  26. pFile = fopen ( nomefileb , "rb" );
  27. fseek (pFile , 0 , SEEK_END);
  28. lSize = ftell (pFile);
  29. fclose (pFile);
  30. printf("massimo peso sostenibile: %d kb!\n",(lSize/(3*1024)));
  31.  
  32. char nomefiles[20];
  33. printf("nome del file da steganografare:");
  34. scanf("%s",nomefiles);
  35. printf("%s!\n",nomefiles);
  36. //peso del file da steganografare
  37. FILE * pFile2;
  38. long lSize2;
  39. pFile2 = fopen ( nomefiles , "rb" );
  40. fseek (pFile2 , 0 , SEEK_END);
  41. lSize2 = ftell (pFile2);
  42. fclose (pFile2);
  43. printf(" peso file da steganografare: %d kb... operazione ",(lSize2/1024));
  44. if((lSize2/1024)<(lSize/(3*1024))) printf("FATTIBILE \n\n");
  45. else {printf("NON FATTIBILE \n\n");return 0; }
  46.  
  47. //******************** inizio operazioni di steganografia ***************************//
  48. long lbuffer = 3; //byte letti alla volta nella base
  49. long lbuffer2 = lbuffer/3; //byte letti nel file
  50. printf("buffer impostato a %d bytes\n",lbuffer);
  51.  
  52. unsigned char *buf, *buf2; //buffer di lettura
  53. buf = new unsigned char [(lbuffer+54)]; //grandezza del buffer della base
  54. buf2 = new unsigned char [(lbuffer2+54)]; //grandezza del buffer del file
  55. FILE *base, *file, *f_out;
  56. if ((file = fopen(nomefiles, "rb")) &&(base = fopen(nomefileb, "rb")) && (f_out = fopen("cript.bmp", "wb"))){
  57.  
  58. printf("files aperti con successo!!\n");
  59.  
  60. size_t b,c; //byte letti
  61. long x=0; //variabile per il ciclo
  62. b = fread( buf, 1, 54, base);
  63. fwrite( buf, 1, b, f_out);
  64.  
  65. while( c = fread( buf2, 1, lbuffer2, file) ) {
  66. b = fread( buf, 1, lbuffer, base);
  67.  
  68. /* ciclo di steganografia */
  69. x=x+lbuffer; // x = numero di byte letti dalla base
  70.  
  71.  
  72. unsigned char *bits_f;
  73. bits_f = new unsigned char [8];
  74. bits_f = dammibit(buf2[0]);
  75.  
  76. unsigned char *bits_b1;
  77. bits_b1 = new unsigned char [8];
  78. bits_b1 = dammibit(buf[0]);
  79. unsigned char *bits_b2;
  80. bits_b2 = new unsigned char [8];
  81. bits_b2 = dammibit(buf[1]);
  82. unsigned char *bits_b3;
  83. bits_b3 = new unsigned char [8];
  84. bits_b3 = dammibit(buf[2]);
  85.  
  86. bits_b1[5] = bits_f[0];
  87. bits_b1[6] = bits_f[1];
  88. bits_b1[7] = bits_f[2];
  89. bits_b2[5] = bits_f[3];
  90. bits_b2[6] = bits_f[4];
  91. bits_b2[7] = bits_f[5];
  92. bits_b3[6] = bits_f[6];
  93. bits_b3[7] = bits_f[7];
  94.  
  95. buf[0] = dammichar(bits_b1);
  96. buf[1] = dammichar(bits_b2);
  97. buf[2] = dammichar(bits_b3);
  98.  
  99. if(x%1048575==0)
  100. printf("%d Mb\n",(x/1048576));
  101.  
  102. fwrite( buf, 1, b, f_out);
  103.  
  104.  
  105. }
  106.  
  107. //lbuffer = 120000;
  108. printf("buffer reimpostato a %d bytes\n",lbuffer);
  109. while( b = fread( buf, 1, lbuffer, base) ){
  110. fwrite( buf, 1, b, f_out);
  111. if(x%1048575==0)
  112. printf("%d Mb\n",(x/1048576));
  113. }
  114.  
  115. }
  116.  
  117. fclose(file);
  118. fclose(base);
  119. fclose(f_out);
  120.  
  121. system("PAUSE");
  122. return 1;
  123. }
  124.  
  125.  
  126. unsigned char* dammibit(unsigned char numero){
  127.  
  128. unsigned char *bits;
  129. bits = new unsigned char [8];
  130.  
  131. if(numero<128) bits[0] = 0; // cifra 1
  132. else { bits[0] = 1; numero = numero - 128; }
  133.  
  134. if(numero<64) bits[1] = 0; // cifra 2
  135. else { bits[1] = 1; numero = numero - 64; }
  136.  
  137. if(numero<32) bits[2] = 0; // cifra 3
  138. else { bits[2] = 1; numero = numero - 32; }
  139.  
  140. if(numero<16) bits[3] = 0; // cifra 4
  141. else { bits[3] = 1; numero = numero - 16; }
  142.  
  143. if(numero<8) bits[4] = 0; // cifra 5
  144. else { bits[4] = 1; numero = numero - 8; }
  145.  
  146. if(numero<4) bits[5] = 0; // cifra 6
  147. else { bits[5] = 1; numero = numero - 4; }
  148.  
  149. if(numero<2) bits[6] = 0; // cifra 7
  150. else { bits[6] = 1; numero = numero - 2; }
  151.  
  152. if(numero<1) bits[7] = 0; // cifra 8
  153. else { bits[7] = 1; numero = numero - 1; }
  154. /*
  155.   FILE *prova = fopen("output.txt","wb");
  156.   fwrite( bits, 1, 8, prova);
  157.   fclose(prova);
  158. */
  159. return bits;
  160.  
  161. }
  162.  
  163. unsigned char dammichar(unsigned char *bits){
  164.  
  165. unsigned char numero;
  166.  
  167. numero = bits[0]*128 + bits[1]*64 + bits[2]*32 + bits[3]*16 + bits[4]*8 + bits[5]*4 + bits[6]*2 + bits[7]*1;
  168.  
  169. return numero;
  170.  
  171. }