My program is encrypting a text file that says the following:
Two of the most important things in your LIFE right
now are your HEALTH and yor EDUCATION. Your EDUCATION
will provide you the better things in life providing
of course you are HEALTHY enough to ENJOY them. So
study hard, take care of your self, and always THINK.
The code encrypts the file, then when decrypting returns this:
Two of the most important things in your LIFE right
now are your HEALTH and yor EDUCATION. Your EDUCATION
will provide you the better things in life providing
of course you are HEALTHY enough to ENJOY them. So
study hard, take care of your self, and always THINK.11@=:
It seems like the EOF is being encrypted and then being turned into those random symbols during decryption.
Is there any way to surpass this problem?
Here's some code. Sorry for the length.
void Encryption()
{
cout << "\nWhat is the location of the file you would like to encrypt?";
string fileloc;
getline (cin, fileloc); //cin gets the file location as a string
cin >> fileloc;
infile.open (fileloc.c_str()); //opens the file to be encrypted
cout << "\nWhat is the password you would like to use to create the encryption?";
cout << "\nPlease enter a four letter word.";
string password;
getline (cin,password); //cin gets the meshing password as a string
cin >> password;
cout << "\n\nWhat is the name of the file to which you would like to print the encryption?";
string outfileloc;
getline (cin, outfileloc); //cin gets the output file as a string
cin >> outfileloc;
outfile.open (outfileloc.c_str()); //opens the output file
while (!infile.eof()) //while not the end of the file
{
for (int i=0 ;i<4 ; i++) //for loop cycles through the positions of the password array until end of file is reached
{
char name; //character from file to be encrypted
infile.get(name); //gets character from the file
name = name + password[i]; //adds the part of the password array
name = name - password[0]; //subtracts the first letter of the password (keeps characters printable)
outfile << name; //prints that character to the output file
}
}
infile.close(); //closes the original file
outfile.close(); //closes the file encryption
cout << "\n\n"; //aesthetic purposes
void Decryption()
{
cout << "\nWhat is the location of the file you would like to decrypt?";
string fileloc;
getline (cin, fileloc); //cin gets the location of the file to be decrypted as a string
cin >> fileloc;
infile2.open (fileloc.c_str()); //opens the file using infile2 (different input file)
cout << "\nWhat is the password used when the text was encrypted?";
cout << "\nPlease enter the four letter word.";
string password;
getline (cin,password); //gets password used for encrypting as a string
cin >> password;
cout << "\n\n";
while (!infile2.eof()) //while not the end of the file
{
for (int i=0 ;i<4 ; i++) //for loop cycles through the positions of the password array until end of file is reached
{
char name; //character from file to be decrypted
infile2.get(name); //gets character from file
name = name + password[0]; //re-adds the first letter of the password
name = name - password[i]; //subtracts the proper letter from the password array
cout << name; //prints decrypted character to screen
}
}
infile2.close(); //closes file that was being decrypted
void Encryption()
{
cout << "\nWhat is the location of the file you would like to encrypt?";
string fileloc;
getline (cin, fileloc); //cin gets the file location as a string
cin >> fileloc;
infile.open (fileloc.c_str()); //opens the file to be encrypted
cout << "\nWhat is the password you would like to use to create the encryption?";
cout << "\nPlease enter a four letter word.";
string password;
getline (cin,password); //cin gets the meshing password as a string
cin >> password;
cout << "\n\nWhat is the name of the file to which you would like to print the encryption?";
string outfileloc;
getline (cin, outfileloc); //cin gets the output file as a string
cin >> outfileloc;
outfile.open (outfileloc.c_str()); //opens the output file
while (!infile.eof()) //while not the end of the file
{
for (int i=0 ;i<4 ; i++) //for loop cycles through the positions of the password array until end of file is reached
{
char name; //character from file to be encrypted
infile.get(name); //gets character from the file
name = name + password[i]; //adds the part of the password array
name = name - password[0]; //subtracts the first letter of the password (keeps characters printable)
outfile << name; //prints that character to the output file
}
}
infile.close(); //closes the original file
outfile.close(); //closes the file encryption
cout << "\n\n"; //aesthetic purposes
}
void Decryption()
{
cout << "\nWhat is the location of the file you would like to decrypt?";
string fileloc;
getline (cin, fileloc); //cin gets the location of the file to be decrypted as a string
cin >> fileloc;
infile2.open (fileloc.c_str()); //opens the file using infile2 (different input file)
cout << "\nWhat is the password used when the text was encrypted?";
cout << "\nPlease enter the four letter word.";
string password;
getline (cin,password); //gets password used for encrypting as a string
cin >> password;
cout << "\n\n";
while (!infile2.eof()) //while not the end of the file
{
for (int i=0 ;i<4 ; i++) //for loop cycles through the positions of the password array until end of file is reached
{
char name; //character from file to be decrypted
infile2.get(name); //gets character from file
name = name + password[0]; //re-adds the first letter of the password
name = name - password[i]; //subtracts the proper letter from the password array
cout << name; //prints decrypted character to screen
}
}
infile2.close(); //closes file that was being decrypted
cout << "\n\n"; //aesthetic purposes
}
The problem is on lines 23 and 58. Despite the commentary, it does not stop at EOF, but stops at the first multiple of four bytes following EOF.
The fact that you have two loops (nested loops) is telling. Use only one loop.
Also, don't test against eof(). Test against good().
1 2 3 4 5 6 7 8 9 10 11
int i = 0; // index into the password for (en/de)cryption, always mod 4.
char c; // the current character being (en/de)crypted
while (infile.get( c ))
{
c += password[ i ];
c -= password[ 0 ];
outfile << c;
i++;
i %= 4;
}
Your cipher, BTW, only modifies the 2nd, 3rd, and 4th of every four characters.