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 82 83 84 85 86 87 88 |
#include <iostream> #include <fstream> #include <vector> #include <cstring> // Function to embed a message into WAV audio data void embedMessage(const std::string& inputWav, const std::string& outputWav, const std::string& message) { std::ifstream inFile(inputWav, std::ios::binary); if (!inFile) { std::cerr << "Error opening input WAV file." << std::endl; return; } std::ofstream outFile(outputWav, std::ios::binary); if (!outFile) { std::cerr << "Error opening output WAV file." << std::endl; return; } // Read WAV header std::vector<char> header(44); inFile.read(header.data(), 44); outFile.write(header.data(), 44); // Prepare the message for embedding std::string messageWithTerminator = message + '\0'; // Null-terminate the message std::vector<char> messageBits(messageWithTerminator.begin(), messageWithTerminator.end()); // Read audio data std::vector<char> audioData((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>()); inFile.close(); // Ensure the message fits into the audio data if (messageBits.size() * 8 > audioData.size()) { std::cerr << "Message is too long to embed in the audio file." << std::endl; return; } // Embed the message bits into the audio data for (size_t i = 0; i < messageBits.size() * 8; ++i) { char& audioByte = audioData[i]; char messageBit = (messageBits[i / 8] >> (i % 8)) & 1; audioByte = (audioByte & ~1) | messageBit; // Modify the least significant bit } // Write the modified audio data to the output file outFile.write(audioData.data(), audioData.size()); outFile.close(); } // Function to extract a message from WAV audio data std::string extractMessage(const std::string& inputWav, size_t messageLength) { std::ifstream inFile(inputWav, std::ios::binary); if (!inFile) { std::cerr << "Error opening input WAV file." << std::endl; return ""; } // Skip WAV header inFile.seekg(44); // Read audio data std::vector<char> audioData((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>()); inFile.close(); std::vector<char> messageBits(messageLength); for (size_t i = 0; i < messageLength * 8; ++i) { char& audioByte = audioData[i]; char messageBit = audioByte & 1; messageBits[i / 8] |= (messageBit << (i % 8)); // Extract the LSB } return std::string(messageBits.begin(), messageBits.end()); } int main() { std::string inputWav = "input.wav"; std::string outputWav = "output.wav"; std::string message = "Hidden Message"; embedMessage(inputWav, outputWav, message); std::cout << "Message embedded into audio file." << std::endl; std::string extractedMessage = extractMessage(outputWav, message.size() + 1); std::cout << "Extracted Message: " << extractedMessage << std::endl; return 0; } |
Explanation
- Include Libraries:
#include <iostream>
: For input and output operations.#include <fstream>
: For file handling.#include <vector>
: For using vectors to manage data.#include <cstring>
: For C-string manipulation functions (though not used in this example).
- Embed Message Function (
embedMessage
):- Opens the input WAV file for reading and the output WAV file for writing.
- Reads the WAV header and writes it to the output file.
- Prepares the message to embed by null-terminating it and converting it into a bit vector.
- Reads the audio data from the WAV file into a vector.
- Embeds the message into the least significant bits (LSBs) of the audio data.
- Writes the modified audio data to the output file.
- Extract Message Function (
extractMessage
):- Opens the WAV file and skips the header.
- Reads the audio data into a vector.
- Extracts the message from the LSBs of the audio data.
- Converts the extracted bits back into a string.
- Main Function (
main
):- Defines file names and a message.
- Calls
embedMessage
to embed the message into the audio file. - Calls
extractMessage
to extract and print the message from the modified audio file.