Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This code must use the given information. We have not gone over strings yet so p

ID: 3796124 • Letter: T

Question

This code must use the given information. We have not gone over strings yet so please do not use them. Helpful comment to explain what you are doing would be awsome. The program should read a given txt file and decipher it and create a new txt file. Then it should ask the user if they want to re-encrypt the file and use the original file name. The code should also use the mod function given.

P Caesar's Cipher (50 points) The Caesar's cipher is a cryptographic method for encrypting text such that it becomes unreadable to a party with- out access to the cryptographic key. It is named after Julius Caesar, who allegedly used it to protect messages of military significance. The encryption and decryption operations are simple shifts of the alphabet letters in a cyclic fashion. Mathematically, for a key k, the encryption and decryption are defined as Encryption c (r +k) mod 26 Decryption: I (c k) mod 26. To illustrate the use of the Ceasar cipher, consider the encryption of "fry" with key k 3. Letters f r, correspond

Explanation / Answer


#include<stdio.h>
#include <math.h>

void decrypt_fun(int shift, char *pt_letter); //method declarations
void encrypt_fun(int shit, char *pt_letter);
int mod(int x, int y);

static FILE *inputFile, *outputFile; // Files to be used
inputFile = fopen("encrypted.txt", "r");
outputFile = fopen("decrypted.txt", "w");

int main(void){

   int shift;
   char lettertoQuit = '?';

   printf("Enter the decryption key: ");
   scanf("%d", &shift); // Take the key
  
   while(shift != 3) {
       printf("Wrong key ");
   printf("Enter the decryption key: ");
   scanf("%d", &shift);
    }

   if (inputFile == NULL) {
   printf("Cannot open input file. ");
   }
   char ch;
   while( ( ch = fgetc(inputFile ) ) != EOF ){
       decrypt_fun(shift,&ch); //decryption
   }
   printf("File Decrypted! ");
   printf("Do you want to re-encrypt the file (y/n)?");
   char c;
   scan("%c",&c);
   if(c == 'y'){
       printf("Enter the encryption key: ");
       scanf("%d", &shift);
       inputFile = fopen("decrypted.txt", "r");
       outputFile = fopen("encrypted.txt", "w");

       while( ( ch = fgetc(inputFile ) ) != EOF ){
           encrypt_fun(shift*-1,&ch); //encryption
       }
       printf("File Encrypted! Good Bye!! ");
   }
  
return 0;
}

void decrypt_fun(int shift, char *pt_letter){

   int n = 0;
   char *ch = pt_letter;
   if (*ch != '' && ((*ch >= 'A' && *ch <= 'Z') || (*ch >= 'a' && *ch <= 'z'))) {
        mod(*ch,shift); // writes the shifted letter to the file.
   }

}

void encrypt_fun(int shift, char *pt_letter){

   int n = 0;
   char *ch = pt_letter;
   if (*ch != '' && ((*ch >= 'A' && *ch <= 'Z') || (*ch >= 'a' && *ch <= 'z'))) {
        mod(*ch,shift);
   }
}
int mod(int x, int y){
char newch = 'a';
   if (x >= 'a' && x <= 'z') {
       char newch = x + y;
       if (newch > 'z') {
           newch = 'a' + (newch - 'z' - 1);
       }
       if (newch < 'a') {
           newch = 'z' - ('a' - newch - 1);
       }
   fputc(newch, outputFile);
   }
   else if (x >= 'A' && x <= 'Z') {
       char newch = x + y;
       if (newch > 'Z') {
           newch = 'A' + (newch - 'Z' - 1);
       }
       if (newch < 'A') {
           newch = 'Z' - ('A' - newch - 1);
       }
   fputc(newch, outputFile);
   }
   else{
       fputc(newch, outputFile);
   }
}

Sorry! I was not able to compile code some reason, should work with small changes. I think the flow and the logic is correct as above.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote