1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include <iostream> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; // Function to hide a message in an image void encodeMessage(Mat& image, const string& message) { // Ensure message fits in the image int msgLength = message.size(); if (msgLength > image.rows * image.cols / 8) { cerr << "Error: Message is too long to encode in the image!" << endl; return; } // Convert message to binary string binaryMessage; for (char c : message) { for (int i = 7; i >= 0; --i) { binaryMessage += (c & (1 << i)) ? '1' : '0'; } } binaryMessage += '0'; // Add a terminator for the message // Encode binary message into the image's LSB int bitIndex = 0; for (int y = 0; y < image.rows; ++y) { for (int x = 0; x < image.cols; ++x) { Vec3b& pixel = image.at<Vec3b>(y, x); for (int c = 0; c < 3; ++c) { if (bitIndex < binaryMessage.size()) { pixel[c] = (pixel[c] & ~1) | (binaryMessage[bitIndex] - '0'); ++bitIndex; } } } } } // Function to extract a message from an image string decodeMessage(const Mat& image) { string binaryMessage; for (int y = 0; y < image.rows; ++y) { for (int x = 0; x < image.cols; ++x) { Vec3b pixel = image.at<Vec3b>(y, x); for (int c = 0; c < 3; ++c) { binaryMessage += (pixel[c] & 1) ? '1' : '0'; if (binaryMessage.size() >= 8 && binaryMessage.substr(binaryMessage.size() - 8) == "00000000") { return binaryMessage.substr(0, binaryMessage.size() - 8); } } } } return ""; } int main() { // Load an image from file Mat image = imread("path/to/your/image.png"); if (image.empty()) { cerr << "Error: Could not load image!" << endl; return -1; } // Encode a message into the image string message = "Hello, Steganography!"; encodeMessage(image, message); // Save the modified image imwrite("encoded_image.png", image); // Load the modified image Mat encodedImage = imread("encoded_image.png"); // Decode the message from the image string decodedMessage = decodeMessage(encodedImage); cout << "Decoded Message: " << decodedMessage << endl; return 0; } |
Explanation
- Include Libraries: The program includes the OpenCV library for image processing and standard I/O libraries for handling input and output.
- Encode Message:
- Message Length Check: Verifies that the message can fit within the image.
- Convert Message to Binary: Converts each character of the message to an 8-bit binary representation and concatenates them.
- Add Terminator: Appends a binary terminator (
00000000
) to mark the end of the message. - Hide Binary Message: Embeds each bit of the binary message into the least significant bit (LSB) of the pixel values of the image. Each color channel of each pixel is modified accordingly.
- Decode Message:
- Extract Binary Message: Reads the LSB of each color channel to reconstruct the binary message.
- Terminate Reading: Stops reading when the terminator (
00000000
) is detected and converts the binary message back to text.
- Main Function:
- Load Image: Loads an image from a file.
- Encode Message: Calls
encodeMessage
to hide a predefined message in the image. - Save and Reload Image: Saves the image with the hidden message and then reloads it.
- Decode Message: Calls
decodeMessage
to extract and print the hidden message.