Re-implement your solution for the problem below (with functions and arrays) usi
ID: 3532936 • Letter: R
Question
Re-implement your solution for the problem below (with functions and arrays) using command-line arguments as follows:
FileCrypt -encrypt "hello.txt" "hello.out"
FileCrypt -decrypt "hello.out" "hello.ans"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
char ceaserCipher(char c, int key)
{ /* function body */
return (c-'A'+key)&+'A';
}
/**************************************************************
* This function takes an encrypted character and a cipher key *
* to return a decrypted character. *
* @param c is a char (the character to be decrypted) *
* @param key is an integer (cipher key given in the handout) *
**************************************************************/
char ceaserDecipher(char c, int key)
{ /* function body */
c= (c-'A'-key)&+'A';
if(c<'A')
c+=26;
return c;
}
/**************************************************************
* This function displays the menu options as in the handout *
**************************************************************/
void displayMenu()
{ /* function body */
cout<<"Welcome to the Cryptographic Techniques Program ";
cout<<"Please enter your selection: ";
cout<<"1. Encrypt ";
cout<<"2. Decrypt ";
}
/**************************************************************
* This function encrypts the content of infile and saves the *
* encrypted text into outfile *
* @param infile string (file that has raw text) *
* @param outfile string (file that will have encrypted text) *
**************************************************************/
void encrypt(string infile, string outfile)
{ /* function body */
char c;
ifstream in;
ofstream out;
int key=5;
in.open(infile.c_str());
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
system("exit");
}
out.open(outfile.c_str());
in>>c;
while(in)
{if(isalpha(c))
c=ceaserCipher(c,key);
out<<c;
in>>c;
}
in.close();
out.close();
}
/**************************************************************
* This function decrypts the content of infile and saves the *
* decrypted text into outfile *
* @param infile string (file that has encrypted text) *
* @param outfile string (file that will have decrypted text) *
**************************************************************/
void decrypt(string infile, string outfile)
{ /* function body */
char c;
ifstream in;
ofstream out;
int key=5;
in.open(infile.c_str());
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
system("exit");
}
out.open(outfile.c_str());
in>>c;
while(in)
{if(isalpha(c))
c=ceaserDecipher(c,key);
out<<c;
in>>c;
}
in.close();
out.close();
}
/**************************************************************
* This function takes an character and a cipher key to return *
* an encrypted character. *
* @param c is a char (the character to be encrypted) *
* @param key is an integer (cipher key given in the handout) *
**************************************************************/
/**************************************************************
* This is the main function of the program. *
* @return a value to terminate the program successfully *
**************************************************************/
int main()
{int choice;
int again;
string ende[]={"encrypt","decrypt"};
string in,out;
do
{
displayMenu();
// 1. Get user input (e.g. encrypt/decrypt and filenames)
cin>>choice;
while(choice<1||choice>2)
{cout<<"Invalid entry ";
displayMenu();
cin>>choice;
}
cout<<"Enter name of your file to "<<ende[choice-1]<<": ";
cin>>in;
cout<<"Enter name of your output file: ";
cin>>out;
// 2. Call the appropriate function(s) to process encryption/decryption
if(choice==1)
encrypt(in,out );
else
decrypt(in,out );
// 3. Then, get user input to continue or exit the program
cout<<"Do this again(1=yes, any other number=no): ";
cin>>again;
}while(again==1);
return 0;
}
**Please use clean code
Explanation / Answer
//file with corrected encryption/decryption function
#include <iostream>
#include <fstream>
#include <string>
#include<cstring>
#include<cstdlib>
using namespace std;
char ceaserCipher(char c, int key)
{ /* function body */
if(isupper(c))
return ((c-'A'+key)&)+'A';
else
return ((c-'a'+key)&)+'a';
}
/**************************************************************
* This function takes an encrypted character and a cipher key *
* to return a decrypted character. *
* @param c is a char (the character to be decrypted) *
* @param key is an integer (cipher key given in the handout) *
**************************************************************/
char ceaserDecipher(char c, int key)
{ /* function body */
if(isupper(c)){
c= (c-'A'-key)+'A';
}else{
c=(c-'a'-key)+'a';
}
if(!isalpha(c)){
c+=26;
}
return c;
}
/**************************************************************
* This function encrypts the content of infile and saves the *
* encrypted text into outfile *
* @param infile string (file that has raw text) *
* @param outfile string (file that will have encrypted text) *
**************************************************************/
void encrypt(string infile, string outfile)
{ /* function body */
char c;
ifstream in;
ofstream out;
int key=5;
in.open(infile.c_str());
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
system("exit");
}
out.open(outfile.c_str());
in>>noskipws>>c;
while(in)
{
if(isalpha(c))
c=ceaserCipher(c,key);
out<<c;
in>>noskipws>>c;
}
in.close();
out.close();
}
/**************************************************************
* This function decrypts the content of infile and saves the *
* decrypted text into outfile *
* @param infile string (file that has encrypted text) *
* @param outfile string (file that will have decrypted text) *
**************************************************************/
void decrypt(string infile, string outfile)
{ /* function body */
char c;
ifstream in;
ofstream out;
int key=5;
in.open(infile.c_str());
if(in.fail())
{ cout<<"input file did not open please check it ";
system("pause");
system("exit");
}
out.open(outfile.c_str());
in>>noskipws>>c;
while(in)
{if(isalpha(c))
c=ceaserDecipher(c,key);
out<<c;
in>>noskipws>>c;
}
in.close();
out.close();
}
/**************************************************************
* This function takes an character and a cipher key to return *
* an encrypted character. *
* @param c is a char (the character to be encrypted) *
* @param key is an integer (cipher key given in the handout) *
**************************************************************/
/**************************************************************
* This is the main function of the program. *
* @return a value to terminate the program successfully *
**************************************************************/
int main(int argc,char *argv[])
{int choice;
int again;
string ende[]={"encrypt","decrypt"};
string conv;
string in,out;
if(argc==4){
if(strcmp(argv[1],"-encrypt")==0){
choice=1;
}else if(strcmp(argv[1],"-decrypt")==0){
choice=2;
}else {
cout<<"Wrong format!"<<endl;
cout<<"Please enter your choice in the following format:"<<endl;
cout<<"FileCrypt -encrypt "hello.txt" "hello.out""<<endl;
cout<<"FileCrypt -decrypt "hello.out" "hello.ans""<<endl;
exit(-1);
}
conv=argv[2];
in=conv.substr(0,conv.length());
conv=argv[3];
out=conv.substr(0,conv.length());
}else{
cout<<"Wrong format!"<<endl;
cout<<"Please enter your choice in the following format:"<<endl;
cout<<"FileCrypt -encrypt "hello.txt" "hello.out""<<endl;
cout<<"FileCrypt -decrypt "hello.out" "hello.ans""<<endl;
exit(-1);
}
// 2. Call the appropriate function(s) to process encryption/decryption
if(choice==1)
encrypt(in,out );
else
decrypt(in,out );
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.