Write a C program that provides an interface for the use of the Affine Cipher. R
ID: 3635834 • Letter: W
Question
Write a C program that provides an interface for the use of the Affine Cipher. Recall that the Affine Cipher uses the following mathematical formulas to encrypt and decrypt messages:E(x) = Ax + B (mod N) <= ENCRYPTION FUNCTION
D(x) = A-1(x – B) (mod N) <= DECRYPTION FUNCTION
Note: A and N must be coprime. This means they share no prime factors.
A-1 is the inverse of A (mod N). This means A * A-1 = 1 (mod N)
Recall that A and B are mathematical constants used to determine the “key” used by the Affine Cipher. The two parties exchanging information would agree upon a key upfront so they know how to encrypt and decrypt the messages mathematically. N is the size of the alphabet we are using. In cryptography, we typically remove all spaces, punctuation and case information from the letters. This means we must transform input files so that they do not contains these invalid characters. In essence, all message will become a series of lowercase letters, which gives us 26 unique symbols we need to deal with. This gives us an alphabet size of N = 26.
Example:
Plain Text : This is an English sentence. It contains invalid characters!
Clean PT : thisisanenglishsentenceitcontainsinvalidcharacters
Your program should provide a user-friendly interface for encrypting and decryption messages with this cipher. The messages will be in text files in the same directory as the program. The program should provide interface options that allow the user to select the input and output files and choose the key used by the Affine Cipher to encrypt and decrypt data. Finally, the program should have a menu option that makes a system call to display the contents of the current directory.
Your program should provide the user with a menu containing the following options:
•&?ßsp;Select Input File
•&?ßsp;Select Output File
•&?ßsp;Select Affine Cipher Key
•&?ßsp;Encrypt Input File
•&?ßsp;Decrypt Input File
•&?ßsp;Get Directory Listing
When designing your program, you will want to keep the following things in mind:
•&?ßsp;The data read from the input file must be transformed (see above example) in case it contains spaces, punctuation, uppercase letters, or other invalid characters.
•&?ßsp;The english sentence “The quick brown fox jumped over the lazy dogs.” contains all of the letters in the english alphabet. I suggest you use it when testing your encryption and decryption functions to make sure they are doing the proper operations on all of the english letters.
•&?ßsp;Not all values are valid selections for A and B. Since the user is selecting the key used by the cipher, you should be checking to make sure the entered values are valid.
•&?ßsp;There is a difference between A and A-1. When a user enters a value for A, I suggest you immediately calculate the value of A-1 so your program will have it ready to use for decrypting messages.
•&?ßsp;The DOS command for displaying the contents of the current directory is “dir /p”. DOS commands can be issued by your program with the system() function found in stdlib.h (i.e. system(“ dir /p”); would generate the current directory listing).
•&?ßsp;Check to make sure the filenames provided by the user are valid before the program reads/writes data to/from them. The following code demonstrates the technique you should use to perform a check on the file handle.
FILE* fileHandle = NULL;
fileHandle = fopen(“someFile.txt”, “r”);
if(fileHandle == NULL) {
printf(“ The file could not be opened for reading. ”);
return 1;
}
Aside from these restrictions, you have full creative license. Please organize your code in an intelligent manner and provide comments and functions where appropriate.
When preparing your sample output, please encrypt a text file that contains the sentence:
The quick brown fox jumped over the lazy dogs.
Encrypt the message with the Affine Cipher key (19, 4).
Explanation / Answer
#include #include #include #include void AffineCipher(char *,char *); void encipher(); void decipher(); void main() { int choice; //loop takes choice from user and calles appropriate function while(1) { printf(" 1. Encrypt Text "); printf("2. Decrypt Text "); printf("3. Exit "); printf("Enter Your Choice : "); scanf("%d",&choice); fflush(stdin); if(choice == 3) exit(0); else if(choice == 1) encipher(); else if(choice == 2) decipher(); else printf("Please Enter Valid Option."); } } void encipher() { unsigned int i,j; char input[257],key[33]; printf("Enter Text to be Encrypted [Max. 256 characters/ only alphabets]: "); gets(input); printf("Enter Encryption Key [Max. 32 Characters/ only aphabets]: "); gets(key); for(i=0,j=0;i=strlen(key)) { j=0; } //actual logic -> character from input + character from key % 26 is encrypted charater printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65))%26)); } } void decipher() { unsigned int i,j; char input[257],key[33]; int value; printf("Enter Text to be Decrypted [Max. 256 characters/ only alphabets]: "); gets(input); printf("Enter Decryption Key [Max. 32 Characters/ only aphabets]: "); gets(key); for(i=0,j=0;i=strlen(key)) { j=0; } //similar to encipher only difference is you need to subtract value = (toupper(input[i])-64)-(toupper(key[j])-64); //if value is negative. We have to rotate it backwards (like backwards from z,y,x) //so add it to 26 (it's a negative value to adding will actually cause subtraction) to get original character. if( value < 0) { value = 26 + value; } printf("%c",65 + (value % 26)); } }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.