168k views
0 votes
C++

Prog 1. File Encryption: File encryption is the science of writing the contents of a file in a secret code. Your encryption program should work like a filter reading the contents of one file, modifying each character in the input file into a code, and then writing the coded characters out to a second file. The second file will be a version of the first file but written in a secret code.

In Notepad or something similar, you will create a file with text in it. It should be several lines long. Your program should read the file character by character and make some change to the character before writing that character to a second new encrypted file. See program 13-8 to see how to read and write one character at a time.

You may come up with an encryption technique of your own or use something simple like adding 10 to the ASCII code of each character in the file before it is written to the second file.

Prog. 2 – File Decryption: Write a program that decrypts the file produced by the program in Prog. 1. The decryption program should read the contents of the coded file, restore the information to its original state and write it to another (third) file. Example: If you added 10 to each character in the first program, subtract 10 from each character in this program. (The output file of program 2 should be the same as the input file of program 1)

Error Checking: Your program should check the fail bit after each open, read, and write operations. Display an error message if the operation failed. Test your program to make sure that each of these tests works correctly. (Example, use invalid file name to get a file open error).

Sample of what the two codes will be based on

(They should be nearly identical except one encrypts and one decrypts).

File name will be named: example

// This file demonstrates the getline function with a // user-specified delimiter. #include #include #include using namespace std; int main() // Variable needed to read file string input; // Open the file. fstream dataFile("addresses.txt", ios::in); if (!dataFile) cout << "Error opening file."; return 0; // Read lines terminated by '$' sign and output getline(dataFile, input, '$'); while (!dataFile.fail()) cout << input << endl; getline(dataFile, input, '$'); // Close the file dataFile.close(); return 0;

// This program demonstrates the use of the get member // functions of the istream class #include #include #include using namespace std; int main() // Variables needed to read file one character at a time string fileName; fstream file; char ch; // character read from the file // Get file name and open file cout << "Enter a file name: "; cin >> fileName; file.open(fileName, ios::in); if (!file) cout << fileName << " could not be opened.\\"; exit(1); // Read file one character at a time and echo to screen ch = file.get(); while (ch != EOF) cout << ch; ch = file.get(); // Close file file.close(); return 0;

User Jlconlin
by
8.3k points

1 Answer

3 votes

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.

User Bishnu
by
7.5k points