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

Hi, im having trouble with this programming question Program is done in C Contex

ID: 3809156 • Letter: H

Question

Hi, im having trouble with this programming question

Program is done in C

Context:

The Monoalphabetic Cipher

• Shuffle (jumble) the letters of the alphabet arbitrarily

• Therefore each plaintext letter maps to a different random ciphertext letter or symbol

• Hence the cipher key is 26 letters long

Example Mapping

Plain: abcdefghijklmnopqrstuvwxyz

Cipher: DKVQFIBJWPESCXHTMYAUOLRGZN

Plaintext: ifwewishtoreplaceletters

Ciphertext: WIRFRWAJUHYFTSDVFSFUUFYA

Requirements:

• Implement the Mono-Alphabet substitution cipher in C for both:

– Encryption

– Decryption

• Ensure to take your plaintext ( or ciphertext) information from a text file of arbitrary length and output the transformation to a text file.

• Additionally, allow the option (‘e’ or ‘d’), plaintext file, and ciphertext file to be passed to your program as command line arguments

• Allow the user to enter an encryption (or decryption) key of their choice.

Usage:

• Encryption Usage:

– MyCipher e plaintext.dat ciphertext.dat

• Decryption Usage:

– MyCipher d ciphertext.dat plaintext.dat

Explanation / Answer

#include<stdio.h>
#include<string.h>

void monoAlphaEncrypt(FILE* input, FILE* output, char key[]);
void monoAlphaDecrypt(FILE* input, FILE* output, char key[]);

int main(int argc, char* argv[])
{
char option = ' ';
FILE* input = NULL;
FILE* output = NULL;
char key[26] = {''};


if(argc != 4){
printf("Usage Error : Program expects three inputs 'e/d' 'cipher/input file' 'cipher/input file' ");
return -1;
}

/*Encrypt or decrypt ? */
option = argv[1][0];
input = fopen(argv[2],"r");

if(input == NULL){
fprintf(stderr,"Unable to open input file ");
return -1;
}

output = fopen(argv[3],"w");
if(output == NULL){
fprintf(stderr,"Unable to open output file ");
return -1;
}


if((option == 'e') || (option == 'E'))
{
printf("Please enter the encryption key : ");
scanf("%s",key);

if(strlen(key) != 26){
printf("Error: Encryption key should be of 26 characters ");
return -1;
}
monoAlphaEncrypt(input,output, key);

}
else if( (option == 'd') || (option == 'D'))
{
printf("Please enter the encryption key : ");
scanf("%s",key);

if(strlen(key) != 26){
printf("Error: Encryption key should be of 26 characters ");
return -1;
}

monoAlphaDecrypt(input,output,key);

}
else{
fprintf(stderr,"Usage error: first option must be 'e' or 'd' ");
return -1;
}


fclose(input);
fclose(output);

return 0;
}


void monoAlphaEncrypt(FILE* input, FILE* output, char key[])
{
int pos = 0;
char ch = ' ', encrypt_ch = ' ';

ch = fgetc(input);

while(ch != EOF)
{
if(ch >=65 && ch <=90){
pos = ch - 65;
encrypt_ch = key[pos];
fputc(encrypt_ch, output);
}
else if( ch >=97 && ch <=122){
pos = ch - 97;
encrypt_ch = key[pos];
fputc(encrypt_ch, output);
}

ch = fgetc(input);
}

}

void monoAlphaDecrypt(FILE* input, FILE* output, char key[])
{

int pos = 0;
char ch = ' ', decrypt_ch = ' ';

ch = fgetc(input);

while(ch != EOF)
{
if(ch >=65 && ch <=90){

for(pos = 0; pos < 26;pos++){
if(key[pos] == ch)
break;
}
decrypt_ch = 'A' + pos;

fputc(decrypt_ch, output);
}
else if( ch >=97 && ch <=122){

for(pos=0; pos < 26; pos++){
if(key[pos] == ch)
break;
}
decrypt_ch = 'A' + pos;

fputc(decrypt_ch, output);
}

ch = fgetc(input);
}

}

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