|
#include <iostream> |
|
#include <iomanip> |
|
#include <vector> |
|
#include <string> |
|
|
|
unsigned char xor_sum(unsigned char *buffer, unsigned int length) { |
|
unsigned char temp_sum = 0; |
|
|
|
while (length-- > 0) { |
|
temp_sum ^= *buffer++; |
|
} |
|
return temp_sum; |
|
} |
|
|
|
int main() { |
|
std::string userInput; |
|
std::cout << "Enter your input: "; |
|
std::getline(std::cin, userInput); |
|
|
|
std::vector<unsigned char> buffer1; |
|
int byteValue; |
|
for (char &c : userInput) { |
|
byteValue = int(c); |
|
buffer1.push_back(static_cast<unsigned char>(byteValue)); |
|
} |
|
|
|
// Construct the second buffer using a vector |
|
std::vector<unsigned char> buffer2 = {0x40, 0x4e, 0x54, 0x43, 0x00, 0x00, 0x00, 0x00, |
|
0x01, 0x00, 0x00, 0x00, static_cast<unsigned char>(buffer1.size()), 0x00}; |
|
|
|
// Calculate the XOR sum of the message data |
|
unsigned char xorResult = xor_sum(buffer1.data(), buffer1.size()); // checksum |
|
|
|
buffer2.push_back(xorResult); |
|
|
|
// Calculate the XOR sum of the header |
|
unsigned char xorResult2 = xor_sum(buffer2.data(), buffer2.size()); |
|
buffer2.push_back(xorResult2); |
|
|
|
// Construct the final message by concatenating buffer2 and buffer1 |
|
std::vector<unsigned char> finalBuffer = buffer2; |
|
finalBuffer.insert(finalBuffer.end(), buffer1.begin(), buffer1.end()); |
|
|
|
// Print the final buffer |
|
std::cout << "Final Buffer: "; |
|
for (const auto &byte : finalBuffer) { |
|
std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(byte) << ""; |
|
} |
|
std::cout << std::endl; |
|
|
|
return 0; |
|
} |