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 file name: "; cin >> inputFilename; cout << "Enter the output 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, so it can be used for both encryption and decryption. - It iterates through each byte of the data and applies the XOR operation.
- The
- File Reading (
readFile
):- The
readFile
function opens a binary file and reads its content into avector<unsigned char>
. This vector represents the raw binary data of the file. - It uses
ios::ate
to move to the end of the file and determine its size before reading the data into the buffer.
- The
- File Writing (
writeFile
):- The
writeFile
function writes the contents of avector<unsigned char>
to a binary file. It takes the filename and the data to be written.
- The
- Main Function (
main
):- The main function prompts the user to enter the input and output filenames, as well as the encryption/decryption key.
- It reads the binary data from the input file, processes it using the XOR cipher, and writes the result to the output file.
- The key must be in the range of 0-255 as it is a single byte used for the XOR operation.
Possible Enhancements:
- Advanced Encryption: Implement more secure encryption algorithms such as AES for better security.
- Error Handling: Improve error handling with more detailed messages and recovery options.
- File Format Validation: Check and validate the format of the audio file to ensure compatibility.
- GUI Interface: Develop a graphical user interface (GUI) to make the encryption/decryption process more user-friendly.