#include #include #include

using namespace std;

string encrypt(string text) { string encryptedText; for (int i = 0; i < text.length(); i++) { encryptedText += text[i] ^ 0xFF; // XOR operation with 0xFF } return encryptedText; }

string decrypt(string encryptedText) { string decryptedText; for (int i = 0; i < encryptedText.length(); i++) { decryptedText += encryptedText[i] ^ 0xFF; // XOR operation with 0xFF } return decryptedText; }

int main() { string filePath = 'C:\absolute\path\to\file.txt';

// Read file contents
ifstream inputFile(filePath, ios::binary);
string fileContents((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());
inputFile.close();

// Encrypt file contents
string encryptedContents = encrypt(fileContents);

// Save encrypted contents back to file
ofstream outputFile(filePath, ios::binary);
outputFile << encryptedContents;
outputFile.close();

// Decrypt and display file contents
string decryptedContents = decrypt(encryptedContents);
cout << decryptedContents << endl;

return 0;

}

In this code, the 'encrypt' function uses the exclusive OR (XOR) operation to encrypt the text, and the 'decrypt' function decrypts the encrypted text. The 'main' function first reads the file contents from the specified absolute path file, then uses the 'encrypt' function to encrypt the file contents, and saves the encrypted contents back to the file. Next, it uses the 'decrypt' function to decrypt the encrypted contents and prints the decrypted contents to the console.

Please note to replace the value of the 'filePath' variable with the actual absolute path of the file you want to operate on. Also, ensure that you have the permission to read and write to the file.


原文地址: https://www.cveoy.top/t/topic/qA7n 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录