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 |
#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; } |
Explanation:
- XOR Cipher Function (
xorCipher
):- The
xorCipher
function performs encryption or decryption by applying an XOR operation with a single byte key. - XOR is a symmetric operation, meaning the same function can be used for both encrypting and decrypting data.
- The
- File Reading (
readFile
):- The
readFile
function opens a binary file (e.g., an image file) and reads its entire content into avector<unsigned char>
. - It uses
ios::ate
to determine the file size andseekg
to move back to the beginning before reading.
- The
- File Writing (
writeFile
):- The
writeFile
function writes the contents of avector<unsigned char>
back to a binary file. - It opens the file in binary mode and writes the data to it.
- The
- Main Function (
main
):- Prompts the user for input and output filenames, as well as the encryption/decryption key.
- Reads the binary data from the input image file, processes it with the XOR cipher, and writes the processed data to the output file.
- Ensures the key is within the valid range (0-255).
Possible Enhancements:
- Advanced Encryption: Use more sophisticated encryption algorithms like AES for stronger security.
- Error Handling: Improve error handling with detailed messages and exceptions.
- Image Format Validation: Validate the image format to ensure compatibility with different image types.
- GUI Interface: Implement a graphical user interface (GUI) for easier interaction with the encryption/decryption process.