Help me with the following C++ Assignment please, i will thumb up for a correct
ID: 3913814 • Letter: H
Question
Help me with the following C++ Assignment please, i will thumb up for a correct and complete solution.
(Please provide your own solution, don't copy and paste from other chegg questions or from other sources)
Note: the code should meet the all requirement stated. Thank you
Part 1: Text Files Write a program "File Encryption and Decryption" 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 the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code. Your program should be able to decrypt the encrypted file. Your program should output the decrypted contents on a separate file and also on the console. Although there are complex encryption techniques (which you will study eventually if you are a CS major), you should come up with simple algorithm of your own. For example, yogi could read the first file one character at a time, and add 10 to the ASCII code of each character before it is written to the second file. Please write separate functions for each part of the algorithmExplanation / Answer
//encryption algorithm
I am reading the file line by line and adding each line to a string ,,then each character of the string buf is added 5 to get the encrypted char ,,then each character is written to output file as entered by user
//decryption
User specifies the decrypt file name
program reads each char from file and subtract 5 from each character to get original character
/* This program is menu driven ,, user can choose between three options */
-------------------------------------------------------------------------------------------
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
//function for encrypt
void encrypt(string buf,ofstream &out);
string decrypt(string out);
int main()
{
//input file handle to read the file for encryption
ifstream in;
//output file for writing encrypted text
ofstream out;
string input, output,buf,str;
int choice;
do
{
cout << "Enter 1. To encrypt a text from a file 2. Dycrypt text from a file 3. Quit ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the input file name : ";
cin >> input;
//open input and output files
in.open(input);
//check if input file is open
if (!in)
{
cout << "Not able to open the input file" << endl;
return -1;
}
cout << "Enter the output file name: ";
cin >> output;
out.open(output);
//check if file is open fro writing
if (!out)
{
cout << "Not able to open the outputfile" << endl;
return -1;
}
//read from the file
while (!in.eof())
{
getline(in, str);
buf += str;
//add new line char
buf += ' ';
}
//call encrypt function
buf[buf.length() - 1] = EOF;
encrypt(buf, out);
break;
case 2:
//call decrypt function
cout << "Enter the output file name: ";
cin >> output;
str = decrypt(output);
cout << "Decrypted message is : " << str << endl;
break;
case 3:
cout << "Exiting.." << endl;
break;
default:
cout << "Invalid option,tryy again..." << endl;
}
} while (choice != 3);
}
/*definition of encrypt function
input: text file content as string
function: this function adds 5 to each character in the string and write the text to output file referenced by out
return: none
*/
void encrypt(string buf,ofstream &out)
{
char ch;
for (int i = 0; i < buf.length(); i++)
{
ch = buf[i] + 5;
out << ch;
}
out.close();
}
/*defintion of decrypt function
input:output file name to read from
function: this function is reverse of encrypt ie it subtract 5 from each character to get the original character
return: string containing decrypted string
*/
string decrypt(string out)
{
//open the ouput file for reading
ifstream in;
string buf;
in.open(out);
if (!in)
{
cout << "Not able to open the file for reading " << endl;
return "";
}
char ch;
while (!in.eof())
{
in >> ch;
buf += (ch - 5);
}
in.close();
return buf;
}
/*output
Enter 1. To encrypt a text from a file 2. Dycrypt text from a file 3. Quit
1
Enter the input file name : encrypt.txt
Enter the output file name: decrypt.txt
Enter 1. To encrypt a text from a file 2. Dycrypt text from a file 3. Quit
2
Enter the output file name: decrypt.txt
Decrypted message is :
definition of encrypt function
input: text file content as string
function: this function adds 5 to each character in the string and write the text to output file referenced by out
return: none
Enter 1. To encrypt a text from a file 2. Dycrypt text from a file 3. Quit
3
Exiting..
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.