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

PLEASE ANSWER IN C PROGRAMMING 1 Affine Cipher (70 points) The Affine cipher is

ID: 3754294 • Letter: P

Question

PLEASE ANSWER IN C PROGRAMMING

1 Affine Cipher (70 points)

The Affine cipher is a cryptographic method for encrypting text such that it becomes unreadable to a party without access to the cryptographic key. 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+) mod26,126and126 Decryption:x=1·(c) mod26.

Here, 1 is a multiplicative inverse of in the group of integers modulo 26. To find the multiplicative inverse of one needs to find x such that

·x=1 mod26. (1) A simple way of finding the inverse of is to consider all numbers from 1 to 25 and see which one satisfies equation

(1).

To illustrate the use of the Affine cipher, consider the encryption of “defend the east wall of the castle” with keyk = (,) = (5,7). The first letter “d” is mapped to number 3 (alphabet letters are numbered from 0 to 25). Inserting 3 to the encryption functions yields

c=53+7 mod26=22 mod26=22

which corresponds to letter “w”. Applying the same process for ever letter, the plaintext sentence is translated to “wbgbuw yqb bhty nhkk zg yqb rhtykb” . Now to decode, we first need to find the inverse of 5 modulo 26. Scanning every number from 1 to 25, we observe that

5·21 mod26=1,
so the inverse of 5 is 21. Using 21 for decrypting the first letter “w” (or 22) becomes

21·(227) mod26=3,

which reverses back to letter “d”.
NOTE: The modulo operation for negative numbers is different from the % arithmetic operator in C.

Write a C program that decrypts a file named “encrypted.txt” and places the decryption output to a file called “de- crypted.txt”. A file encrypted with k = (5, 7) 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

1

• 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 encryptFun(int a, int b, char letter); // This function receives as input the key components (,) and the plaintext letter and returns the encrypted letter.

char decryptFun(int a, int b, char letter); // This function receives as input the key components (,) and an encrypted letter and returns the decrypted letter.

int inverse(int x); // This function as input receives an int x and returns the multiplicative inverse of x modulo 26.

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 < 0){

x+ = y;}

return x%y;}

Explanation / Answer

#include <stdio.h>

#include <ctype.h>

/*

Your code must be modular. Use the following function prototypes for encryption and decryption:

*/

/*

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 < 0)

{

x += y;

}

return x%y;

}

// This function as input receives an int x and returns the multiplicative inverse of x modulo 26.

int inverse(int a)

{

// To find the multiplicative inverse of , one needs to find x such that ·x=1 mod26.

// (1) A simple way of finding the inverse of is to consider all numbers from 1 to 25 and see which one satisfies equation

  

for( int x = 1; x<=25; x++ )

{

// check for the equation

if( (x*a)%26 == 1 )

return x;

}

  

// return dummy value

return 1;

}

// This function receives as input the key components (,) and the plaintext letter and returns the encrypted letter.

char encryptFun(int a, int b, char letter)

{

// find the index for this letter

int letterNumber;

  

// if this is upper case

if( isupper(letter) )

letterNumber = letter - 'A';

else if( islower(letter) )

letterNumber = letter - 'a';

else

return letter;

  

// if control reaches here, then this is a letter

  

// get the new letter number

int encryptedLetterNumber = mod( a*letterNumber + b, 26);

  

// return the new letter based on upper or lower case

  

if( isupper(letter) )

return (encryptedLetterNumber + 'A');

else

return (encryptedLetterNumber + 'a');

}

// This function receives as input the key components (,) and an encrypted letter and returns the decrypted letter.

char decryptFun(int a, int b, char letter)

{

// find the index for this letter

int letterNumber;

  

// if this is upper case

if( isupper(letter) )

letterNumber = letter - 'A';

else if( islower(letter) )

letterNumber = letter - 'a';

else // case where this is not a letter

return letter;

  

// if control reaches here, then this is a letter

  

// find the modulo inverse of alpha

int moduloInverseAlpha = inverse( a );

  

// get the new letter number

int decryptedLetterNumber = mod( moduloInverseAlpha*(letterNumber - b), 26);

  

// return the new letter based on upper or lower case

  

if( isupper(letter) )

return (decryptedLetterNumber + 'A');

else

return (decryptedLetterNumber + 'a');

}

// read key from the user

void readKeyFromUser(int * a,int * b)

{

// print message

printf("Input key (a,b) ");

printf("Enter a : ");

scanf("%d",a);

printf("Enter b : ");

scanf("%d",b);

}

// method to encrypt the input file and store to output file

// return 0 for fail

// return 1 for successful encryption

int encryptFile(char * inputFile, char * outputFile, int a, int b)

{

// open the input fil

FILE * inputFp = fopen(inputFile,"r");

  

// if this is null, return 0

if( !inputFp )

return 0;

  

// open output file

FILE * outputFp = fopen(outputFile,"w");

  

// if this is null, return 0

if( !outputFp )

return 0;

  

// read as long as character

char c;

while( (c = fgetc(inputFp)) != EOF )

{

// encrypt and paste to output file

fprintf(outputFp, "%c", encryptFun(a, b, c));

}

  

// close input file

fclose(inputFp);

// close output file

fclose(outputFp);

  

// return 1 for success

return 1;

}

// method to decrypt the input file and store to output file

// return 0 for fail

// return 1 for successful encryption

int decryptFile(char * inputFile, char * outputFile, int a, int b)

{

// open the input fil

FILE * inputFp = fopen(inputFile,"r");

  

// if this is null, return 0

if( !inputFp )

return 0;

  

// open output file

FILE * outputFp = fopen(outputFile,"w");

  

// if this is null, return 0

if( !outputFp )

return 0;

  

// read as long as character

char c;

while( (c = fgetc(inputFp)) != EOF )

{

// encrypt and paste to output file

fprintf(outputFp, "%c", decryptFun(a, b, c));

}

  

// close input file

fclose(inputFp);

// close output file

fclose(outputFp);

  

// return 1 for success

return 1;

}

int main()

{

// to store the key

int a,b;

  

// read the key

readKeyFromUser(&a, &b);

  

// encrypt

encryptFile("input.txt", "encrypted.txt”", a, b);

  

// read the key

readKeyFromUser(&a, &b);

  

// decrypt

decryptFile("encrypted.txt", "de-crypted.txt”", a, b);

  

/*

   Add code to read a,b from user, encrypt or decrypt file.

   I have added methods that do all the work. You just need to encrypt or decrypt as needed

   */

  

// return 0;

return 0;

}

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