Question: I'm not sure where is the wrong part. The Caesar's cipher is a cryptographic method for encrypting text such that it becomes unreadable to a






I'm not sure where is the wrong part.
The Caesar's cipher is a cryptographic method for encrypting text such that it becomes unreadable to a party without 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=(x+k) mod 26, Decryption: x=(c-k) mod 26. To illustrate the use of the Ceasar's cipher, consider the encryption of "fry" with key k = 3. Letters f, r, y correspond to the 5th, 16th, and 24th letter of the alphabet (letter 'a' being the oth). f:(5+3) mod 26 = 8-ir:(16+3) mod26 = 19+ y:(24+3) mod 26 = 1 + Similarly, the decryption of i,ub follows the reverse process i:(8-3) mod26 = 5+f u:(19-3) mod26 = 16+ b:(1-3) mod26 = 24 NOTE: The modulo operation for negative numbers is different from the % arithmetic operator in C (look for the code provided at the end). Write a C program that decrypts a file named "encrypted.txt" and places the decryption output to a file called "decrypted.txt". A file encrypted with k = 3 is provided with the assignment. You can use it to test your decryption function. You will know when you have succeeded because the text becomes readable. In your program Ask the user to enter the decryption key.. Repeat the request until the right key is entered. Display a message when file decryption is over. Ask the user to enter a key to re-encrypt the file. Re-encrypt file "decrypted.txt" and store it at "encrypted.txt". Display a message when file encryption is over. Only alphabet letters (uppercase/lowercase) must be encrypted. The remaining characters (question marks, periods, etc. must remain intact) Your code must be modular. Use the following function prototypes for encryption and decryption: char decryptFun (int shift, char letter); // This function receives the shift value (key) and an encrypted letter and returns the decrypted letter. char encryptFun (int shift, char letter); // This function receives the shift value (key) and the plaintext letter and returns the encrypted letter. You are also given the following function that correctly implements the modulo operation for both positive and negative numbers int mod (int x, int y){ while (x 2 char encryptFun(int shift,char ptr_letter); 3 char decryptFun(int shift, char ptr_letter); 4 int main(){ 5 int key, i; 6 do{ 7 printf("Enter the decrpytion key: "); 8 scanf("%d",&key); if(key!=3){ 10 printf("Wrong Key "); 11 } 12 }while(key!-3); 13 char encrypted[1000]; 14 char decrypted[1000]; 15 char buffer[1000]; 16 FILE *dec=fopen("decrypted.txt", "a"); 17 FILE *enc=fopen("encrypted.txt", "a+"); 18 19 while (fgets(buffer, sizeof(buffer), enc) != NULL) { 20 for(i=0; buffer[i]!='\0';i++){ 21 decrypted[i]=decryptFun(key, buffer[i]); 22 } 23 decrypted[i]='\0'; 24 1/printf("%s ", decrypted); 25 fputs(decrypted, dec); 26 27 } 28 printf("Do you want to re-encrpyt the file(y)? "); 29 char choice; 30 scanf("%c%c",&choice,&choice); 31 if(choice=='y'){ 32 fseek(dec, 0, SEEK_SET); 33 fseek(enc, 0, SEEK_SET); 34 34 35 printf("Enter an encryption key: "); 36 scanf("%d",&key); 37 while (fgets(buffer, sizeof buffer, dec)!= NULL) { 38 for(i=0;buffer[i]!='\0';i++){ 39 encrypted[i]=encryptFunckey, buffer[i]); 40 } 41 encrypted[i]='\0'; 42 //printf("%s", encrypted); 43 fputs(encrypted, enc); 44 } 45 printf("File encrypted "); 46 } 47 printf("Good Bye!"); 48 49 } 50 int mod(int x, int y){ 51 while(x=0){ 52 X+=y; 53 } 54 return x%y; 55 } 56 char encryptFun(int shift,char ptr_letter){ 57 if(ptr_letter 'Z'&& ptr letter>'A'){ 58 return 'A'+mod(ptr_letter-65+shift, 26); 59 } 60 if(ptr_letter'A'){ 68 return 'A'+mod(ptr_letter-65-shift, 26); 69 70 if(ptr_letter'a'){ 71 return 'a'tmod(ptr_letter-97-shift, 26); 72 } 73 return ptr_letter; 74 1 1: Compare output ^ 0/1 Input 5 3 Your output Enter the decrpytion key: Wrong Key Enter the decrpytion key: Do you want to re-encrpyt the file (y)? Good By Your output does not contain Wrong key 2: Compare output ^ 0/1 Input 3 Your output Enter the decrpytion key: Do you want to re-encrpyt the file (y)? Good By Your output does not contain File decrypted! 3: Unit test A 1/1 4: Unit test ^ 0/1 Test feedback decryptFun (5, a) incorrectly returned a 5: Unit test ^ 1/1 6: Unit test A 0/1 Test feedback encryptFun (3, a) incorrectly returned a 7: Unit test ^ 1/1 The Caesar's cipher is a cryptographic method for encrypting text such that it becomes unreadable to a party without 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=(x+k) mod 26, Decryption: x=(c-k) mod 26. To illustrate the use of the Ceasar's cipher, consider the encryption of "fry" with key k = 3. Letters f, r, y correspond to the 5th, 16th, and 24th letter of the alphabet (letter 'a' being the oth). f:(5+3) mod 26 = 8-ir:(16+3) mod26 = 19+ y:(24+3) mod 26 = 1 + Similarly, the decryption of i,ub follows the reverse process i:(8-3) mod26 = 5+f u:(19-3) mod26 = 16+ b:(1-3) mod26 = 24 NOTE: The modulo operation for negative numbers is different from the % arithmetic operator in C (look for the code provided at the end). Write a C program that decrypts a file named "encrypted.txt" and places the decryption output to a file called "decrypted.txt". A file encrypted with k = 3 is provided with the assignment. You can use it to test your decryption function. You will know when you have succeeded because the text becomes readable. In your program Ask the user to enter the decryption key.. Repeat the request until the right key is entered. Display a message when file decryption is over. Ask the user to enter a key to re-encrypt the file. Re-encrypt file "decrypted.txt" and store it at "encrypted.txt". Display a message when file encryption is over. Only alphabet letters (uppercase/lowercase) must be encrypted. The remaining characters (question marks, periods, etc. must remain intact) Your code must be modular. Use the following function prototypes for encryption and decryption: char decryptFun (int shift, char letter); // This function receives the shift value (key) and an encrypted letter and returns the decrypted letter. char encryptFun (int shift, char letter); // This function receives the shift value (key) and the plaintext letter and returns the encrypted letter. You are also given the following function that correctly implements the modulo operation for both positive and negative numbers int mod (int x, int y){ while (x 2 char encryptFun(int shift,char ptr_letter); 3 char decryptFun(int shift, char ptr_letter); 4 int main(){ 5 int key, i; 6 do{ 7 printf("Enter the decrpytion key: "); 8 scanf("%d",&key); if(key!=3){ 10 printf("Wrong Key "); 11 } 12 }while(key!-3); 13 char encrypted[1000]; 14 char decrypted[1000]; 15 char buffer[1000]; 16 FILE *dec=fopen("decrypted.txt", "a"); 17 FILE *enc=fopen("encrypted.txt", "a+"); 18 19 while (fgets(buffer, sizeof(buffer), enc) != NULL) { 20 for(i=0; buffer[i]!='\0';i++){ 21 decrypted[i]=decryptFun(key, buffer[i]); 22 } 23 decrypted[i]='\0'; 24 1/printf("%s ", decrypted); 25 fputs(decrypted, dec); 26 27 } 28 printf("Do you want to re-encrpyt the file(y)? "); 29 char choice; 30 scanf("%c%c",&choice,&choice); 31 if(choice=='y'){ 32 fseek(dec, 0, SEEK_SET); 33 fseek(enc, 0, SEEK_SET); 34 34 35 printf("Enter an encryption key: "); 36 scanf("%d",&key); 37 while (fgets(buffer, sizeof buffer, dec)!= NULL) { 38 for(i=0;buffer[i]!='\0';i++){ 39 encrypted[i]=encryptFunckey, buffer[i]); 40 } 41 encrypted[i]='\0'; 42 //printf("%s", encrypted); 43 fputs(encrypted, enc); 44 } 45 printf("File encrypted "); 46 } 47 printf("Good Bye!"); 48 49 } 50 int mod(int x, int y){ 51 while(x=0){ 52 X+=y; 53 } 54 return x%y; 55 } 56 char encryptFun(int shift,char ptr_letter){ 57 if(ptr_letter 'Z'&& ptr letter>'A'){ 58 return 'A'+mod(ptr_letter-65+shift, 26); 59 } 60 if(ptr_letter'A'){ 68 return 'A'+mod(ptr_letter-65-shift, 26); 69 70 if(ptr_letter'a'){ 71 return 'a'tmod(ptr_letter-97-shift, 26); 72 } 73 return ptr_letter; 74 1 1: Compare output ^ 0/1 Input 5 3 Your output Enter the decrpytion key: Wrong Key Enter the decrpytion key: Do you want to re-encrpyt the file (y)? Good By Your output does not contain Wrong key 2: Compare output ^ 0/1 Input 3 Your output Enter the decrpytion key: Do you want to re-encrpyt the file (y)? Good By Your output does not contain File decrypted! 3: Unit test A 1/1 4: Unit test ^ 0/1 Test feedback decryptFun (5, a) incorrectly returned a 5: Unit test ^ 1/1 6: Unit test A 0/1 Test feedback encryptFun (3, a) incorrectly returned a 7: Unit test ^ 1/1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
