The fstream include file defines three classes called ifstream,
ofstream, and fstream. Within the main function of your program, you can use ifstream and ofstream to declare oprerators that will read files from disk and write files to disk respectively. In turn these operators will both have open(),close(), and eof() (end of file) functions.
#include <fstream>
#include <iostream>
int main()
{
ifstream inFile; //the name inFile determined by the programmer
ofstream outFile;
cout << "\nPlease enter the file name with file extension.
<< "(Enter path if not inpresent directory.): " << flush;
cin >> fileName; //type in file name, e.g. "diaryNov12.txt"
inFile.open(fileName); file is now opened and ready to be read
return
}
Using inFile.get() you an capture characters from file on disk such as diaryNov12.txt the same way cin.get() captures characters from the keyboard. As before, you need to
declare an array for your text and a char variable to place inside the .get() parentheses.
Use the eof() function to stop the loop that reads the disk file when it reaches the end of file (eof).
char textFile[1000], letter;
do{
//count++;
inFile.get(letter);
textFile[count] = letter;
count++;
}while(!inFile.eof());
The quickest way to view the contents of the file is to add the following line:
cout << textFile << endl;
Now you can encrypt the text contain in the array textFile[] by creating a key and
applying it to textFile[] using the ^ Exclusive ORbitwise operator.
char key[] = "Captain Nemo"; //key of 12 characters
int keyLength = 12;
int endFile = count; //final count from the inFile loop
count = 0;
int keyCount = 0;
do{
//apply the Exclusive OR operator to key[] and textFile[]
textFile[count] = key[keyCount] ^ textFile[count];
count++;
if(keyCount == 12)
keyCount = 0;
}while(count < endFile);
You can print the file to screen again to see it in an encrypted form.
You may be wondering "How do I unencrypt it?"Simply apply the Exclusive ORbitwise operator to textFile[] a second time using the same loop in the code above.
To write your encrypted (or unencrypted) text to file, use the ofstream function:
cout << "\nPlease enter the file name for the key."
<< flush;
//enter whatever name you want, e.g. "crypt.txt"
cin >> fileName;
outFile.open(fileName);
outFile << textFile;
Note:
At the top of the program, if you enter the input file name incorrectly you will crash the program. You can test to make sure the input file actually exists, and stop the program if it doesn't.
if(!inFile) //If inFile doesn't exist
{cout << "File does not exist. Program halted!";
return 0;}
You could also use this test in a loop to give the user another chance.
ASSIGNMENT #6
Lesson: Encrypting Disk Files
1. Encode a text using a loop and the exclusive OR operator. Have this program
print the encrypted text to screen.
2. Decode an enrypted file and print it to screen.
3. Write an encryption program in which the encryption key is inputted from the
keyboard. Use a loop to keep track of the key's length.