(C++ only) Write a file encryption program. First the user will be prompted for
ID: 3833646 • Letter: #
Question
(C++ only) Write a file encryption program. First the user will be prompted for a key string and the name of an input file and an output file. Then the user will be asked if they wish to encrypt or decrypt the input file, writing the results to the output file. If the input file is to be encrypted, the key string will be used to encode the input file, writing its encrypted contents to the output file.
-The key string may consist only of alphabetic characters, otherwise it's an error and the program will terminate.
-The key string is to be treated in a case-insensitive manner; if the user enters all lowercase, all uppercase or even mixed case, it makes no difference to the final program results.
-This would use the encrypted file foo.enc and the same key string to create the decrypted file foo.dec, so that it's a copy of the original file foo.txt.
Basically, the alphabet is remapped. To take a simple example, imagine the characters of the alphabet in reverse order so at the first position we find the letter Z, at the second position is the letter Y, at the third position is the letter X, and so on, up to the 26th position containing the letter A. Let's call this the "code table." Then, whenever an input character is read, what's written to the output file is the corresponding letter from the code table. So if the letter A were read (which is the first letter of the alphabet), then what would be written to the output file is the letter Z (since that occupies the first position of the code table). Similarly, if the letter B were read, then the letter Y would be written, the letter C would produce the letter X, and so on.
But if our code table were simply the reverse of the alphabet it would be trivial for a someone to decrypt your files. So to make it not so easy, we'll introduce a key string to make the code table more dynamic. First of all, the code table will consist only of uppercase letters. The key string is copied to the code table, but any duplicate characters are removed so that only unique characters are in the code table
HI! First I'd like to say I'm sorry if all of this is confusing. This part is part of a bigger project which we are required to write all the functions for this program however I only need help writing the Encrypt function and the Decrypt function. I included the header file and I can include main too. But if somebody can help me get on track I'd greatly appreciate it! Thanks.
tifndef CENCRYPT HEADER #define CENCRYPT HEADER includeExplanation / Answer
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string.h>
#include<stdlib.h>
#include<map>
using namespace std;
int main()
{
char fname[20];
string ch;
fstream fps, fpt, fdt;
char alp[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char enc[26] = {'Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A'};
map<char, string> valueDecoderMap;
// store the values in key,value form
for ( int i = 0; i < (sizeof(alp)); i++ )
{
valueDecoderMap [ alp [ i ] ] = enc [ i ];
}
string keyString = "";
cout << "Enter the key string : ";
cin >> keyString;
int flag = 0;
int i = 0;
while (keyString[i])
{
if (isalpha(keyString[i])) flag = 1;
else
{
flag = 0;
break;
}
i++;
}
if(flag == 0)
{
cout << "The key string may consist only of alphabetic characters " << endl;
return 0;
}
cout<<"Enter file name (with extension like foo.txt) to encrypt : ";
cin >> fname;
fps.open(fname);
if(!fps)
{
cout<<"Error in opening file..!!";
cout<<" Press any key to exit...";
getch();
exit(1);
}
fpt.open("encrypt.txt");
if(!fpt)
{
cout<<"Error in creating encrypt.txt file..!!";
fps.close();
cout<<" Press any key to exit...";
getch();
exit(2);
}
while(fps.eof()==0)
{
fps>>ch;
map<char, string>::iterator mapIterator;
for(int i = 0 ; i < ch.length(); i++ )
{
ch[i]= toupper(ch[i]);
mapIterator = valueDecoderMap.find ( ch[i] );
if ( mapIterator != valueDecoderMap.end () )
{
fpt << mapIterator->second;
}
else
{
cout << "No encoding present for " << ch << endl;
}
}
}
cout<<"File "<<fname<<" encrypted successfully..!!";
fdt.open("decrypt.txt");
if(!fdt)
{
cout<<"Error in creating decrypt.txt file..!!";
fdt.close();
cout<<" Press any key to exit...";
getch();
exit(2);
}
while(fpt.eof()==0)
{
fpt>>ch;
map<char, string>::iterator mapIterator;
for(int i = 0 ; i < ch.length(); i++ )
{
ch[i]= toupper(ch[i]);
mapIterator = valueDecoderMap.find ( ch[i] );
if ( mapIterator != valueDecoderMap.end () )
{
fdt << mapIterator->first;
}
else
{
cout << "No encoding present for " << ch << endl;
}
}
}
cout<<"File "<<fname<<" decrypted successfully..!!";
cout<<" Press any key to exit...";
fps.close();
fpt.close();
fdt.close();
getch();
return 0;
}
I dint get the concept of keyString. What is to be done with that? Else I tried to code the same manner you want it.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.