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
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
521 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;}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
