he 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 = (a, b), the encryption and decryption are defined as:

Encryption: c = (a * x + b) mod 26 , 1 ≤ a <= 26 and 1 <= b <= 26
Decryption: x = a−1 * (c − b) mod 26.

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

a * x = 1 mod 26.

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 key k = (a, b) = (5, 7). (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 = 5 3 + 7 mod 26 = 22 mod 26 = 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 mod 26 = 1,

so the inverse of 5 is 21. Using 21 for decrypting the first letter "w" (or 22) becomes

21 * (22 − 7) mod 26 = 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

  • 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 (a, b) 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 (a, b) 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;
}

Sample Code Execution:

Enter the decryption key:2 7
Wrong key
Enter the decryption key:5 7
File decrypted!
Do you want to re-encrypt the file (y/n)? y
Enter an encryption key: 7 19
File encrypted!
Goodbye
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.