Guest User

Untitled

a guest
Nov 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. /*******************************************************************************
  2. * This code is to take a text document and using steganography techniques, hide
  3. * the text within a bmp. It will take each character of the text, parse it into
  4. * four 2 bit pieces and inject those bits into the two least significant bits
  5. * of each pixel color (BGR) byte as well as the line padding.
  6. ******************************************************************************/
  7. #include <stdio.h>
  8.  
  9. /*******************************************************************************
  10. * getIntFromArray (borrowed from class notes). Takes unsigned character array
  11. * and assembles/returns an int value using bit shifting with OR.
  12. ******************************************************************************/
  13. int getIntFromArray(unsigned char bytes[])
  14. {
  15. int n =
  16. bytes[0] |
  17. bytes[1] << 8 |
  18. bytes[2] << 16 |
  19. bytes[3] << 24;
  20. return n;
  21. }
  22.  
  23. /*******************************************************************************
  24. * bitWise. Take unsigned char pointer and character, parses the character
  25. * using bitwise manipulation and injects 2 bits into the 2 least significant
  26. * bits of each pixel color byte as well as padding.
  27. ******************************************************************************/
  28. void bitWise(unsigned char* bytes, char character)
  29. {
  30. int i;
  31. char tmpChar;
  32. for(i = 0; i < 4; ++i)
  33. {
  34. tmpChar = character;
  35. tmpChar &= 3;
  36. bytes[i] &= 252;
  37. bytes[i] |= tmpChar;
  38. character = character >> 2;
  39. }
  40. }
  41.  
  42. int flag = 0;
  43.  
  44. int main(int argc, char **argv)
  45. {
  46. char *infilename = argv[1];
  47. char *outfilename = argv[2];
  48.  
  49. unsigned char header[54];
  50.  
  51. FILE *in = fopen(infilename, "rb");/*Command line input.*/
  52. FILE *out = fopen(outfilename, "wb");/*Command line input.*/
  53.  
  54. int pixelWidth;
  55. int pixelHeight;
  56. int i;
  57. int j;
  58.  
  59. fread(header, 1, 54, in);/* read header into array */
  60.  
  61. pixelWidth = getIntFromArray(&header[18]);
  62. pixelHeight = getIntFromArray(&header[22]);
  63.  
  64. fwrite(header, 1, sizeof(header), out);/* write header to output file */
  65.  
  66. for(i = 0; i < pixelHeight; ++i)/*Loop to read pixel data from bmp.*/
  67. {
  68. for(j = 0; j < pixelWidth; ++j)
  69. {
  70. unsigned char bytes[4];
  71. unsigned char character = 0;
  72.  
  73. fread(&bytes, 1, 4, in);/*Reads sequentially pixel and padding bytes.*/
  74. if(flag == 0)/*Breakout flag, initially set to 0.*/
  75. {
  76. character = getchar();/*Takes in characters from stdin.*/
  77. if(character != EOF)/*Breakout if EOF.*/
  78. {
  79. bitWise(bytes, character);
  80. }
  81. else
  82. {
  83. bitWise(bytes, 0);/*Sets end of hidden text with 4 bytes LSB to 0.*/
  84. flag = 1;
  85. }
  86. }
  87. fwrite(&bytes, 1, 4, out);
  88. }
  89. }
  90. fclose(in);
  91. fclose(out);
  92. return 0;
  93. }
Add Comment
Please, Sign In to add comment