Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- * This code is to take a text document and using steganography techniques, hide
- * the text within a bmp. It will take each character of the text, parse it into
- * four 2 bit pieces and inject those bits into the two least significant bits
- * of each pixel color (BGR) byte as well as the line padding.
- ******************************************************************************/
- #include <stdio.h>
- /*******************************************************************************
- * getIntFromArray (borrowed from class notes). Takes unsigned character array
- * and assembles/returns an int value using bit shifting with OR.
- ******************************************************************************/
- int getIntFromArray(unsigned char bytes[])
- {
- int n =
- bytes[0] |
- bytes[1] << 8 |
- bytes[2] << 16 |
- bytes[3] << 24;
- return n;
- }
- /*******************************************************************************
- * bitWise. Take unsigned char pointer and character, parses the character
- * using bitwise manipulation and injects 2 bits into the 2 least significant
- * bits of each pixel color byte as well as padding.
- ******************************************************************************/
- void bitWise(unsigned char* bytes, char character)
- {
- int i;
- char tmpChar;
- for(i = 0; i < 4; ++i)
- {
- tmpChar = character;
- tmpChar &= 3;
- bytes[i] &= 252;
- bytes[i] |= tmpChar;
- character = character >> 2;
- }
- }
- int flag = 0;
- int main(int argc, char **argv)
- {
- char *infilename = argv[1];
- char *outfilename = argv[2];
- unsigned char header[54];
- FILE *in = fopen(infilename, "rb");/*Command line input.*/
- FILE *out = fopen(outfilename, "wb");/*Command line input.*/
- int pixelWidth;
- int pixelHeight;
- int i;
- int j;
- fread(header, 1, 54, in);/* read header into array */
- pixelWidth = getIntFromArray(&header[18]);
- pixelHeight = getIntFromArray(&header[22]);
- fwrite(header, 1, sizeof(header), out);/* write header to output file */
- for(i = 0; i < pixelHeight; ++i)/*Loop to read pixel data from bmp.*/
- {
- for(j = 0; j < pixelWidth; ++j)
- {
- unsigned char bytes[4];
- unsigned char character = 0;
- fread(&bytes, 1, 4, in);/*Reads sequentially pixel and padding bytes.*/
- if(flag == 0)/*Breakout flag, initially set to 0.*/
- {
- character = getchar();/*Takes in characters from stdin.*/
- if(character != EOF)/*Breakout if EOF.*/
- {
- bitWise(bytes, character);
- }
- else
- {
- bitWise(bytes, 0);/*Sets end of hidden text with 4 bytes LSB to 0.*/
- flag = 1;
- }
- }
- fwrite(&bytes, 1, 4, out);
- }
- }
- fclose(in);
- fclose(out);
- return 0;
- }
Add Comment
Please, Sign In to add comment