#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// Function to encrypt or decrypt data using XOR cipher
void xorCipher(vector<unsigned char>& data, unsigned char key) {
for (auto& byte : data) {
byte ^= key;
}
}
// Function to read a binary file into a vector
vector<unsigned char> readFile(const string& filename) {
ifstream file(filename, ios::binary | ios::ate);
if (!file) {
cerr << "Error opening file: " << filename << endl;
exit(1);
}
streamsize size = file.tellg();
file.seekg(0, ios::beg);
vector<unsigned char> buffer(size);
if (!file.read(reinterpret_cast<char*>(buffer.data()), size)) {
cerr << "Error reading file: " << filename << endl;
exit(1);
}
return buffer;
}
// Function to write a vector to a binary file
void writeFile(const string& filename, const vector<unsigned char>& data) {
ofstream file(filename, ios::binary);
if (!file) {
cerr << "Error opening file: " << filename << endl;
exit(1);
}
file.write(reinterpret_cast<const char*>(data.data()), data.size());
}
int main() {
string inputFilename, outputFilename;
unsigned char key;
cout << "Enter the input image file name: ";
cin >> inputFilename;
cout << "Enter the output image file name: ";
cin >> outputFilename;
cout << "Enter the encryption/decryption key (0-255): ";
cin >> key;
if (key < 0 || key > 255) {
cerr << "Key must be between 0 and 255." << endl;
return 1;
}
// Read the input file into a vector
vector<unsigned char> data = readFile(inputFilename);
// Encrypt or decrypt the data
xorCipher(data, key);
// Write the processed data to the output file
writeFile(outputFilename, data);
cout << "Processing complete. Check the output file: " << outputFilename << endl;
return 0;
}