Here are the C++ programs for file encryption and decryption based on the provided code samples:
Program 1 - File Encryption:
```cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Open the input file for reading
ifstream inputFile("input.txt");
if (!inputFile) {
cout << "Error opening input file." << endl;
return 1;
}
// Open the output file for writing
ofstream outputFile("encrypted.txt");
if (!outputFile) {
cout << "Error opening output file." << endl;
return 1;
}
char ch;
while (inputFile.get(ch)) {
// Encrypt the character by adding 10 to its ASCII code
ch += 10;
outputFile << ch;
}
// Close the files
inputFile.close();
outputFile.close();
cout << "File encrypted successfully." << endl;
return 0;
}
```
Program 2 - File Decryption:
```cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Open the input file for reading
ifstream inputFile("encrypted.txt");
if (!inputFile) {
cout << "Error opening input file." << endl;
return 1;
}
// Open the output file for writing
ofstream outputFile("decrypted.txt");
if (!outputFile) {
cout << "Error opening output file." << endl;
return 1;
}
char ch;
while (inputFile.get(ch)) {
// Decrypt the character by subtracting 10 from its ASCII code
ch -= 10;
outputFile << ch;
}
// Close the files
inputFile.close();
outputFile.close();
cout << "File decrypted successfully." << endl;
return 0;
}
```
To use these programs, make sure to replace the file names ("input.txt", "encrypted.txt", "decrypted.txt") with the appropriate file names you want to use.