Program 1: For this program you will be implementing a simple rotation encryption to be used on a text file. Your program will need to read in text from a text file and encrypt or decrypt it (have the user select which) using the encryption key entered by the user, then write the results out to another file. The user will enter the names of the source and destination files, the encryption key, and will select whether they are encrypting or decrypting the file. You can use any text file of choice.
The program should have these 4 functions:
C CODE WITH ALL FOUR FUNCTIONS :-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
encrypt();
decrypt();
read();
write();
}
void read()
{
char c[1000];
FILE *fp;
if((fp=fopen("filename.txt", "r"))==NULL)
{
printf("Error in opening
file..!!");
getch();
exit(1);
}
fscanf(fp, "%s", c);
printf("Data in file = %s",c);
fclose(fp);
getch();
}
void write()
{
FILE *fp;
char s[100], fname[20];
printf("Enter a file name with extension (like
file.txt) to create a file : ");
gets(fname);
fp=fopen(fname, "w");
if(fp==NULL)
{
printf("Error in opening
file..!!");
getch();
exit(1);
}
printf("\nEnter few lines of text :\n");
while(strlen(gets(s))>0)
{
fputs(s, fp);
fputs("\n",fp);
}
fclose(fp);
getch();
}
void encrypt()
{
char fname[20], ch, choice;
FILE *fps, *fpt;
printf("Enter file name (with extension like file.txt)
to encrypt : ");
gets(fname);
fps=fopen(fname, "r");
if(fps==NULL)
{
printf("Error in opening
file..!!");
printf("\nPress any key to
exit...");
getch();
exit(1);
}
fpt=fopen("temp.txt", "w");
if(fpt==NULL)
{
printf("Error in creating temp.txt
file..!!");
fclose(fps);
printf("\nPress any key to
exit...");
getch();
exit(2);
}
while(1)
{
ch=fgetc(fps);
if(ch==EOF)
{
break;
}
else
{
ch=ch+100;
fputc(ch,
fpt);
}
}
fclose(fps);
fclose(fpt);
fps=fopen(fname, "w");
if(fps==NULL)
{
printf("Error in opening source
file..!!");
printf("\nPress any key to
exit...");
getch();
exit(3);
}
fpt=fopen("temp.txt", "r");
if(fpt==NULL)
{
printf("Error in opening temp.txt
file...!!");
fclose(fps);
printf("\nPress any key to
exit...");
getch();
exit(4);
}
while(1)
{
ch=fgetc(fpt);
if(ch==EOF)
{
break;
}
else
{
fputc(ch,
fps);
}
}
printf("File %s encrypted successfully..!!",
fname);
printf("\nPress any key to exit...");
fclose(fps);
fclose(fpt);
getch();
}
void decrypt()
{
char ch, choice, fname[20];
FILE *fps, *fpt;
printf("Enter file name (with extension like file.txt)
which you have encrypted earlier to decrypt : ");
gets(fname);
fps=fopen(fname, "w");
if(fps==NULL)
{
printf("Error in opening source
file..!!");
printf("\nPress any key to
exit...");
getch();
exit(7);
}
fpt=fopen("temp.txt", "r");
if(fpt==NULL)
{
printf("Error in opening temp.txt
file..!!");
fclose(fps);
printf("\nPress any key to
exit...");
getch();
exit(9);
}
while(1)
{
ch=fgetc(fpt);
if(ch==EOF)
{
break;
}
else
{
ch=ch-100;
fputc(ch,
fps);
}
}
printf("File %s decrypted
successfully..!!",fname);
printf("\nPress any key to exit...");
fclose(fps);
fclose(fpt);
getch();
}
***GENTLE REMINDER:-PLEASE GIVE GOOD RATINGS.***
Get Answers For Free
Most questions answered within 1 hours.