Write a C program that: Now we will try a bit of grade-school cryptography. You
ID: 3774700 • Letter: W
Question
Write a C program that:
Now we will try a bit of grade-school cryptography. You will be given a file with a text message. Write a program that: Reads in the file, which will be called "message.daf. This will be emailed to you. Encrypts the message by performing a "5-position Caesar Cipher" on the letters of the message. The cipher works by taking each letter of the message and replacing it with the letter 5 positions over in the alphabet. For example, the letter A would be replaced with F, B would be replaced with G, C would be replaced with H, etc. At the other end of the alphabet, the letters rotate back to the beginning - Z would be replaced with E, Y, would be replaced with D, etc. For simplicity, you can assume that all of the letters will be upper-case. (However, you are more than welcome to extend the encryption to include lower-case letters and numbers.) (If you are interested, you can look at the Wikipedia entry for "Caesar Cipher" - it's a real thing used by the real Caesar.) Once the encryption is complete, the encrypted message is printed to a file called "ijxxflj.dat" Demonstrate your program to your lab instructor. Be sure to show the new file that contains the encrypted message. For fun, you can write the companion program that will translate the encrypted file back into readable form.Explanation / Answer
//Programme to Encrpt the given file data using 5 Position ceaser method
#include <stdio.h> //input output header file
int main() { //main function begin
FILE *infp,*outfp; //Input,Output file pointers declaration
char c;
infp = fopen("message.txt", "r"); //input file opening for read
outfp = fopen("rjxxfl.txt", "w"); //output file opening for writing encrypted data
//checks for input file is opened or not
if(infp == NULL)
{
perror("Error in opening Input file");
return(-1);
}
//checks for output file is opened or not
if(outfp == NULL)
{
perror("Error in opening Output file");
return(-1);
}
while((c = fgetc(infp)) != EOF) //read data from input file
{
if(c>=65&&c<=90) //check input char Captil letter
{
c=c+5; // apply 5 position ceaser method
if(c>=90) //if cross c value 90 then reverse the chars
{
c=64+c-90;
}
}
if(c>=97&&c<=122) //check input char small letter
{
c=c+5; // apply 5 position ceaser method
if(c>=122) //if cross c value 122 then reverse the chars
{
c=96+c-122;
}
}
printf("%c",c);
fputc(c, outfp); /* Write encrypted dat to file*/
}
fclose(infp);
fclose(outfp);
}
Sample input output files data for encryption :
Input : AZ
Output : FE
//Programme to Dycrpt the given file data using 5 Position ceaser method
#include <stdio.h> //input output header file
int main() { //main function begin
FILE *infp,*outfp; //Input,Output file pointers declaration
char c;
infp = fopen("message.txt", "r"); //input file opening for read
outfp = fopen("rjxxfl.txt", "w"); //output file opening for writing encrypted data
//checks for input file is opened or not
if(infp == NULL)
{
perror("Error in opening Input file");
return(-1);
}
//checks for output file is opened or not
if(outfp == NULL)
{
perror("Error in opening Output file");
return(-1);
}
while((c = fgetc(infp)) != EOF) //read data from input file
{
if(c>=65&&c<=90) //check input char Captil letter
{
c=c-5; // apply 5 position ceaser method
if(c<65) //if cross c value lessthan 65 then reverse the chars
{
c=90-(64-c);
}
}
if(c>=97&&c<=122) //check input char small letter
{
c=c-5; // apply 5 position ceaser method
if(c<97) //if cross c value lessthan 97 then reverse the chars
{
c=122-(96-c);
}
}
printf("%c",c);
fputc(c, outfp); /* Write endycrypted data to file*/
}
fclose(infp);
fclose(outfp);
}
Sample input output files data for dycryption :
Input : FE
Output : AZ
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.