Write a program that reads and encrypts a text file. Encryption is the science o
ID: 3786565 • Letter: W
Question
Write a program that reads and encrypts a text file. Encryption is the science of hiding the contents of the file from users that are not allowed to read its contents. Your program will first read die data from a text file called data.txt and save the information in an array of strings. The first line of the text file has an integer value that indicates the number of lines in the text file. This number will allow you to create a dynamic array of the correct number of strings, where each element in the array of strings will hold a single line of text After the data is read and saved in the array, the next step is to encrypt the data. Modify the contents of the array by encrypting its contents one element at a time. A very simple way of encrypting data is going through a string one character at a time, and adding 7 to the ASCII code of the character. After you are done with the text encryption, write the contents of the array in reverse order to a text file called encrypted.txt For example, let us assume that the content of is the following: 2 CMPSC 122: Intermediate Programming This is an example of encrypting text. The integer value 2 in the first line indicates that the file is composed of two lines of strings. You will then create a dynamic array of two strings where the first element of the array will hold the following string "CMPSC 122: Intermediate Programming" while the second element will hold "This is an example of encrypting text." The next step is to encrypt the contents of the array and save it in a file called encrypted.txt. The contents of the file should now look like: [opz'p z'hu'1 htws1'vm'1uj y w{pun'{1 {5 JTWZJ'899A'Pu{lyt1kph{1'Wyvnyhttpun Where the first encrypted line refers to "This is an example of encrypting text." In addition to the main function, your program should have the following functions: A function that reads the input from the text file (data.txt) and saves it in an array A function that encrypts the contents of the array of strings A function that saves the encrypted data to the output file (enaypted.txt)Explanation / Answer
//C++ Program - Encrypt File the data
#include< iostream.h >//headerfiles
#include<conio.h >
#include<fstream.h >
#include<stdio.h >
#include<stdlib.h >
void main( )
{
clrscr() ;
char fname[20], cha, choice ;
fstream fps1, fpt1;
cout<<"Enter file name () to encrypt ";
gets(fname) ;
fps.open(fname) ;
if(!fps1)
{
cout<<"Error in opening file";
cout<<"Press any key to exit...";
getch() ;
exit(1) ;
}
fpt1.open("temp.txt")
if(!fpt1)
{
cout<<"Error in creating temp.txt file.";
fps1.close();
cout<<" Press any key to exit...";
getch( );
exit( 2 );
}
while( 1fps.eof()==0 )
{
fps1>>ch1;
ch1=ch1+100;
fpt1<<ch1;
}
fps1.close();
fpt1.close();
fps.open(fname) ;
if(!fps1)
{
cout<<"Error in opening source file";
cout<<"Press any key to exit.";
getch ();
exit (3);
}
fpt.open("temp.txt");
if(!fpt1)
{
cout<<"Error in opening temp.txt file.";
fps.close ();
cout<<"Press any key to exit...";
getch() ;
exit(4) ;
}
while(fpt. eof()==0)
{
fpt>>ch1;
fps<<ch1;
}
cout<<"file " << fname <<" encrypted successfully ";
cout<<"Press any key to quit " ;
fps1.close();
fpt1.close();
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.