/* * * The code assumes that the message to hide and the recovered message * are both short enough to store and retrieve within the dimensions of the image * */ void decode(char image[]){ string recoveredMessage; // Recover the message int maxMessageLength = width*height*3 / 8; // Determine the max length of the message (It can't be larger than the file itself) for (int i = 0; i < maxMessageLength; i++){ char ch = '\0'; // set the internal value of ch to 00000000 // There are 8 bits per character in the message we want to recover, // so lets read 8 bits at a time. Each individual bit will come from one element // of the image character array. That is to say, every 8 elements of image will comprise // a single character in our output message for( int bit = 0; bit < 8; bit++){ ch <<= 1; ch |= image[i * 8 + bit] & 1 ? 1 : 0; } // If after reading 8 bits the value of ch is still 00000000 // then we've reached the null character signaling the end of our message if (ch == '\0') break; // Add the newly recovered character to our recovered message string recoveredMessage += (unsigned char)ch; } } void hideData(char image[], string messageToHide){ // Hide the data, one character at a time for (int i = 0; i < messageToHide.size(); i++){ image[i * 8 + 0] >>= 1; image[i * 8 + 0] <<= 1; image[i * 8 + 0] |= (messageToHide[i] & 1<<7) ? 1 : 0; image[i * 8 + 1] >>= 1; image[i * 8 + 1] <<= 1; image[i * 8 + 1] |= (messageToHide[i] & 1<<6) ? 1 : 0; image[i * 8 + 2] >>= 1; image[i * 8 + 2] <<= 1; image[i * 8 + 2] |= (messageToHide[i] & 1<<5) ? 1 : 0; image[i * 8 + 3] >>= 1; image[i * 8 + 3] <<= 1; image[i * 8 + 3] |= (messageToHide[i] & 1<<4) ? 1 : 0; image[i * 8 + 4] >>= 1; image[i * 8 + 4] <<= 1; image[i * 8 + 4] |= (messageToHide[i] & 1<<3) ? 1 : 0; image[i * 8 + 5] >>= 1; image[i * 8 + 5] <<= 1; image[i * 8 + 5] |= (messageToHide[i] & 1<<2) ? 1 : 0; image[i * 8 + 6] >>= 1; image[i * 8 + 6] <<= 1; image[i * 8 + 6] |= (messageToHide[i] & 1<<1) ? 1 : 0; image[i * 8 + 7] >>= 1; image[i * 8 + 7] <<= 1; image[i * 8 + 7] |= (messageToHide[i] & 1<<0) ? 1 : 0; } // Set the null character int i = messageToHide.size(); image[i * 8 + 0] >>= 1; image[i * 8 + 0]; image[i * 8 + 1] >>= 1; image[i * 8 + 1]; image[i * 8 + 2] >>= 1; image[i * 8 + 2]; image[i * 8 + 3] >>= 1; image[i * 8 + 3]; image[i * 8 + 4] >>= 1; image[i * 8 + 4]; image[i * 8 + 5] >>= 1; image[i * 8 + 5]; image[i * 8 + 6] >>= 1; image[i * 8 + 6]; image[i * 8 + 7] >>= 1; image[i * 8 + 7]; }