Question

C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...

C Program

In this assignment you'll write a program that encrypts the alphabetic letters in a file using the Vigenère cipher. Your program will take two command line parameters containing the names of the file storing the encryption key and the file to be encrypted. The program must generate output to the console (terminal) screen as specified below.

Command Line Parameters

Your program must compile and run from the command line.

The program executable must be named “vigenere” (all lower case, no spaces or file

extension).

Input the required file names as command line parameters. Your program may NOT

prompt the user to enter the file names. The first parameter must be the name of the encryption key file, as described below. The second parameter must be the name of the file to be encrypted, as also described below. The sample run command near the end of this document contains an example of how the parameters will be entered.

Your program should open the two files, echo the processed input to the screen, make the necessary calculations, and then output the ciphertext to the console (terminal) screen in the format described below.

Note: If the plaintext file to be encrypted doesn't have the proper number of alphabetic characters, pad the last block as necessary with the letter 'X'.

Encryption Key File Format

The encryption key is plain text that may contain upper and lower case letters, numbers, and other text. The input must be stripped of all non-alphabetic characters. Please note that the input text must be converted to contiguous lower case letters to simplify the encryption process.

Format of the File to be Encrypted

The file to be encrypted can be any valid text file with no more than 512 letters in it. (Thus, it's safe to store all characters in the file in a character array of size 512, including any padding characters.) Please note that the input text file will also generally have punctuation, numbers, special characters, and whitespace in it, which should be ignored. You should also ignore whether a letter is uppercase or lowercase in the input file. Thus, you should treat ‘A’ and ‘a’ the same in your program. Therefore, convert the upper case letters to lower case letters.

Output Format

The program must output the following to the console (terminal) screen:

Echo the input key file

Echo the input plaintext file

Ciphertext output produced from running the cipher against the input plaintext file.

The ciphertext output portion should consist of only lowercase letters in rows of exactly 80 letters per row, except for the last row, which may possibly have fewer. These characters should correspond to the ciphertext produced by encrypting all the letters in the input file. Please note that only the alphabetic letters in the input plaintext file will be encrypted. All other characters should be ignored.

Program Notes and Hints

Your program must read in an input plaintext file that may contain uppercase letters, lowercase letters and non-letter characters. Your program must distinguish between these three groups so that only the letters get encrypted. All non-letter characters in the file are simply skipped and not counted as part of the plaintext. Please note that although both upper case and lower case letters will be encrypted, your program should not treat them differently, that is, the program should process an upper case input letter the same as the corresponding lower case letter, i.e., it should treat an ‘A’ the same as an ‘a’.

One possible breakdown to solve this problem is as follows:

1) Write a section of code or function that reads only the upper and lower case letters in the input file into an char array of size 512, storing only the appropriate lowercase letters in the character array.

2) Write a section of code or function that takes as input the array from section 1 and the encryption key and produces an array of ciphertext storing only lowercase letters.

3) Write a section of code or function that takes as input the array storing the ciphertext and outputs it to the screen in the format specified. Additional functions or code will be needed to echo the input key and plaintext files.

Sample Key File

"Computer programming is an art, because it applies accumulated knowledge to the world, because it requires skill and ingenuity, and especially because it produces objects of beauty. A programmer who subconsciously views himself as an artist will enjoy what he does and will do it better." - Donald Knuth

Sample Input File

678901234567890123456789012345678901234567890 “Cowards die many times before their deaths; The valiant never taste of death but once. 678901234567890123456789012345678901234567890 Of all the wonders that I yet have heard,

It seems to me most strange that men should fear; Seeing that death, a necessary end, 678901234567890123456789012345678901234567890 Will come when it will come.” 678901234567890123456789012345678901234567890

William Shakespeare, Julius Caesar

Corresponding Output File

eciplwwuxvageyfuuryjwfbrvmiikrxwebasiwpdedicpnzygekxdcgskqhhgxapnasjqvzibpntbwaw guihmbyelaimesaihprgvqcblcezvxgbiowsedrsepgyllimbvbvbqydrgnetlwsnoktbtrdtrhnrnqo ijohfqyofkvdnkcgwhfzvmpoptxusxjwalyirfaztgmdainashqsinzgdswsrkatfiialfqybqqboalk xiahkrqe

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int main(int argc,char* argv[])
{
FILE *kptr,*fptr;
char key[513],pt[513];
int ch;
/* open the key file for reading */
kptr = fopen(argv[1], "r");
if (kptr == NULL)
{
printf("Cannot open key file \n");
exit(0);
}
int i=1,j,k=0;
ch = fgetc(kptr);
printf("Key file:\n");
while (ch != EOF)
{
printf ("%c", ch);
if(i>512)
break; // if file size is more than 512 characters
i++;
if(ch>='a'&&ch<='z')
key[k++]=toupper(ch);
if(ch>='A'&&ch<='Z')
key[k++]=ch;
ch = fgetc(kptr);

}
key[k]='\0';
printf("\n");
fclose(kptr);
//open input file for reading

fptr = fopen(argv[2], "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
k=0,i=1;
ch = fgetc(fptr);
printf("Input (plain text) file:\n");
while (ch != EOF)
{
printf ("%c", ch);
if(i>512)
break; // if file size is >512
i++;

if(ch>='a'&&ch<='z')
pt[k++]=toupper(ch);
if(ch>='A'&&ch<='Z')
pt[k++]=ch;
ch = fgetc(fptr);
}
printf("\n");
pt[k]='\0';

fclose(fptr);
int msglen=strlen(pt),keylen=strlen(key);
char newkey[msglen],ct[msglen];
//printf("%d %d\n",msglen,keylen );
  
// Appending x at last
for(i=msglen;i<keylen;i++)
pt[i]='X';
pt[i]='\0';
msglen=strlen(pt);

//encryption
for(i=0;i<msglen;++i)
ct[i]=( (pt[i]+key[i])%26)+'A';
ct[i]='\0';
printf("Cipher text:\n");
for(i=0;i<strlen(ct);i++)
{
if(i%80==0)
printf("\n");
printf("%c",tolower(ct[i]));
}
printf("\n");
/*
// Decryption
char npt[512];
printf("\n\n");
printf("\nDecrypted text: \n");
for(i = 0; i < msglen; ++i)
npt[i] = (((ct[i] - key[i]) + 26) % 26) + 'A';  
npt[i]='\0';
for(i=0;i<strlen(npt);i++)
{
//if(i%80==0)
// printf("\n");
printf("%c",tolower(npt[i]));
}
printf("\n");
*/
}

/HomeworkLib/c/vignere.c-Sublime Text (UNREGISTERED) t E, E) (1 :07, 64%) 40, 19:38 * l.c xVtemplate.cpp Х gnere.ckey.txt median.jat↓ E, 宝 圆 (1:07, 64%) 40, 19:39 * /HomeworkLib/c/vignere.c-Sublime Text (UNREGISTERED) data.txt nput.tx median.java x l.c x Vtempla/HomeworkLib/c/vignere.c-Sublime Text (UNREGISTERED) t↓ E, 宝 圆 (1:07, 64%) 40, 19:39 * l.c xVtemplate.cpp Χ nput.tx median.java x dt Terminal File Edit View Search Terminal Help nanohar@nanohar-Latitude-3540: /HomeworkLib/c$ cat key.txt Conputer progranning is a

Add a comment
Know the answer?
Add Answer to:
C Program In this assignment you'll write a program that encrypts the alphabetic letters in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt...

    Kindly follow the instructions provided carefully. C programming   Project 6, Program Design One way to encrypt a message is to use a date’s 6 digits to shift the letters. For example, if a date is picked as December 18, 1946, then the 6 digits are 121846. Assume the dates are in the 20th century. To encrypt a message, you will shift each letter of the message by the number of spaces indicated by the corresponding digit. For example, to encrypt...

  • WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...

    WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include: a function called encode that takes two parameters: key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order; plaintext, a string of unspecified length that represents the message to be encoded. encode will return a string representing the ciphertext. a...

  • C program: Write a program that assigns and counts the number of each alphabetic character in...

    C program: Write a program that assigns and counts the number of each alphabetic character in the Declaration of Independence and sorts the counts from the most used to the least used character. Consider upper and lower case letters the same. The frequency of each character should be accumulated in an array. USE POINTERS to increment counts for each letter, sort your array, and display the results. Output should consist of two columns: (1) each letter 'a'-'z' and (2) the...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • 1) Echo the input: First, you should make sure you can write a program and have...

    1) Echo the input: First, you should make sure you can write a program and have it compile and run, take input and give output. So to start you should just echo the input. This means you should prompt the user for the plaintext, read it in and then print it back out, with a message such as "this is the plaintext you entered:". [4 points, for writing a working program, echoing the input and submitting the program on the...

  • Write a C program that reads characters from a text file, and recognizes the identifiers in...

    Write a C program that reads characters from a text file, and recognizes the identifiers in the text file. It ignores the rest of the characters. An identifier is a sequence of letters, digits, and underscore characters, where the first character is always a letter or an underscore character. Lower and upper case characters can be part of the identifier. The recognized identifier are copied into the output text. b. Change the C program in exercise 2, so that all...

  • Write a Python program which implements the following two classical cryptosystem which we covered n class:...

    Write a Python program which implements the following two classical cryptosystem which we covered n class: a) Affine Cipher b) Vigenere Cipher Your program should consist of at least five functions: a) Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. b) Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string...

  • Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two...

    Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two arguments, the first one a string and the second one an integer key. Both should return a string. Vigenère part of the homework: Write vig_encrypt() and vig_decrypt() functions. Each takes two strings as inputs, with the first being the plaintext/ciphertext, and the second being the key. Both should be calling functions you wrote earlier to help make the work easier. The key will be...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT