Question

In: Computer Science

**** PLEASE DO NOT COPY AND PASTE FROM ANOTHER SOURCE BECAUSE THE ANSWER IS INCOMPLETE********* Introduction:...

**** PLEASE DO NOT COPY AND PASTE FROM ANOTHER SOURCE BECAUSE THE ANSWER IS INCOMPLETE*********

Introduction: IN C PROGRAMMING

For this assignment you will write an encoder and a decoder for a modified "book cipher." A book cipher uses a document or book as the cipher key, and the cipher itself uses numbers that reference the words within the text. For example, one of the Beale ciphers used an edition of The Declaration of Independence as the cipher key. The cipher you will write will use a pair of numbers corresponding to each letter in the text. The first number denotes the position of a word in the key text (starting at 0), and the second number denotes the position of the letter in the word (also starting at 0). For instance, given the following key text (the numbers correspond to the index of the first word in the line)

[0] 'Twas brillig, and the slithy toves Did gyre and gimble in the wabe;

[13] All mimsy were the borogoves, And the mome raths outgrabe.

[23] "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!

[36] Beware the Jubjub bird, and shun The frumious Bandersnatch!"

[45] He took his vorpal sword in hand: Long time the manxome foe he sought—

The word "computer" can be encoded with the following pairs of numbers:

35,0 catch!

5,1 toves

43,3 frumious

48,3 vorpal

22,1 outgrabe.

34,3 that

23,6 "Beware

7,2 gyre

Placing these pairs into a cipher text, we get the following:

35,0,5,1,43,3,48,3,22,1,34,3,23,6,7,2

If you are encoding a phrase, rather than just a single word, spaces in the original english phrase will also appear in the ciphered text. So, the phrase "all done" (using the above Jabberwocky poem) might appear as: 0,3,1,4,13,1 6,0,46,2,44,2,3,2

Only spaces in the key text should be considered delimiters. All other punctuation in the key text are to be considered part of a key word. Thus the first word in the Jabberwocky poem, 'Twas, will have the following characters and positions for key word 0:

Position 0: '

Position 1: T

Position 2: w

Position 3: a

Position 4: s

Developing the Program:

You should approach this assignment in several parts. The first part will be to write a menu driven program that prompts the user for the following actions:

  1. Read in the name of a text file to use as a cipher key
  2. Create a cipher using the input text file (and save the result to a file)
  3. Decode an existing cipher (prompt user for a file to read containing the cipher text)
  4. Exit the program

For each choice, create a stub function that will be completed in the remaining steps.

After testing your menu, continue to fill in the stub functions with the following specifications:

Choice #1

For this menu choice, you will prompt the user for the name of a cipher text file (such as the Declaration of Independence). You will read this text file line by line, and place each word, in order, in an array of char strings. As you copy each word into the array, you will keep a running count of the number of words in the text file and convert the letters of each word to lower case. You may assume that you will use no more than the first 5,000 words in the text, and no more than the first 15 characters in a word. However, there is no guarantee that the text will have 5000 words or less and any word in the text is 15 characters or less.

Choice #2

If no file has been chosen for Choice #1 (i.e. the user goes directly to Choice #2 without first choosing Choice #1), you will first prompt the user to enter a cipher text and read it into memory (presumably by calling the function that would be called by Choice #1). You will prompt the user to enter a secret message (in plain text - such as "Computer Science is the best major at GMU!") that is terminated by pressing the "Enter" key. You can assume that the length of this message will be less than 1500 characters (including the carriage return and NULL terminator). You will then parse this message, character by character, converting them to lower case as you go, and find corresponding characters in the words found in the key text word array. You can do this by going through each word in the word array, and then each character in each word, until you find a match with the current message character. There are more efficient ways to perform this operation, and you are encouraged to implement them instead of the given method. Once a character match is found, you will write the index of the word and the index of the character to a character string that you will later write out to a text file. Spaces are to be placed into the text as found in the message and will be used to delimit the separate words in the secret message. Once the message has been encoded, prompt the user for the name of a file to save the encoded message to, and save it to that file.

Choice #3

You will prompt the user for the name of a file containing an encoded text (i.e. a file containing number pairs). Your program will read the file and decode the the text using the indexes pairs given for each character in the word and the text file chosen for Choice #1. If no file has been chosen for Choice #1 (i.e. the users goes directly to Choice #3 without first choosing Choice #1), you will prompt the user to enter a cipher text and read it into memory (presumably by calling the function that would be called by Choice #1). Spaces found in the file are to be treated as spaces in the decoded text. You can assume that the number of characters in the encoded text file is 5000 or less, including any carriage returns or NULL terminator characters. Once the text is decoded, print the message to standard output.

Choice #4

Exit the program.

Additional Specifications:

  • In order to introduce some "randomness" in the specific character encoding, you will generate a random number i from 0..9 inclusive (use the last four digits of your G Number as the seed), and use the ith instance of that character found in the text. (If fewer than i instances of the character is found in the text, loop back and continue the search from the beginning of the document.)
    • Example: Suppose the letter to encode is a 'c'. Using the sentences just above, we find that there are the following 'c' characters:
      • In order to introduCe some "randomness" in the speCifiC CharaCter enCoding, you will generate a random number i from 0..9 inClusive (use the last four digits of your G Number as the seed), and use the ith instanCe of that CharaCter found in the text.
      • If the random number returns 6, then you will use the 'c' from the word "inclusive." (Start counting from 0). If the random number returns 2, you would the second c found in the word "specific."
  • If a given character in the secret message is not found in any word of the text, replace that character with the '#' character in the encoded text (a single '#' character replaces a word/position pair).
  • Files and filenames will follow the standard CS262 conventions (username, lab section, etc. at top of file, and as part of filename).
  • Each menu choice (except #4) should call a separate function to perform the operation.
  • You can assume that the message to encrypt or decrypt will be less than 1500 characters.
  • Your program will be compiled using a Makefile

Predefined C functions that may be useful for this project:

strtok()

strlen()

tolower()

atoi()

Options for extra credit:

  1. Use dynamic memory for the arrays (up to 5 points additional credit)
  2. Finding the original Beale treasure and sharing it with your professor (up to 1000 points additional credit)

Solutions

Expert Solution

#include <stdio.h>

#include <string.h>

int main()

{

char **key Words;

char *fileName;

int numWords = 0;

keyWords = malloc(5000 * sizeof(char*));

for (int i = 0; i<5000; i++)

{

key Words[i] = (char *)malloc(15);

}

fileName = (char*)malloc(25);

int choice;

//menu

while (1)

{

printf('1') Read in the name of a text file to use as a cipher key\n");

printf('2') Create a cipher using the input text file (and save the result to a file)\n");

printf('3') Decode an existing cipher (prompt user for a file to read containing the cipher text)\n");

printf('4') Exit the program.\n");

printf("Enter your choice: ");

scanf("%d", &choice);

if (choice == 1)

{

numWords = readFile(keyWords, fileName);

}

else if (choice == 2)

{

encode(keyWords, fileName, numWords);

}

else

{

Exit();

}

printf("\n");

}

return 0;

}

// reads key words into array of character strings

int readFile(char** words, char *fileName)

{

FILE *read;

printf("Enter the name of a cipher text file:");

scanf("%s", fileName);

read = fopen(fileName, "r");

if (read == NULL)

{

puts("Error: Couldn't open file");

fileName = NULL;

return;

}

char line[1000];

int word = 0;

/* read a line by line*/

{

while (fgets(line, sizeof line, read) != NULL)

{

int i = 0;

int j = 0;

while (line[i] != '\0')

{

if (line[i] != '')

{

if (line[i] >= 65 && line[i] <= 90)

{

words[word][j] = line[i]; +32;

}

else

{

words[word][j] = line[i];

}

j++;

}

else

{

words[word][j] = '\n';

j = 0;

word++;

}

i++;

}

words[word][i] = '\n':

word++;

}

return word;

}

// converts a plain text into cipher text

void encode(char** words, char *fileName, int nwords)

{

char line[50];

char result[100];

if (strcmp(fileName, ") == 0)

{

nwords = readFile(words, fileName);

}

getchar();

printf("Enter a secret message(and press enter): ");

gets(line);

int i = 0, j = 0;

int w = 0, k = 0;

while (line[i] != '\0')

{

if (line[i] >= 65 && line[i] <= 90)

{

line[i] = lineļi] + 32;

}

W = 0;

int found = 0;

while (w<nwords)

j = 0;

while (words[w][j] != '\0')

{

if (lineļi] == words[w][i])

{

printf("%c -> %d,%d \n", line[i), w, j);

found = 1;

break;

}

j++;

}

if (found == 1)

break;

W++;

}

i++;

}

result[k] = '\n';

}

void Exit()

{

exit(0);

}


Related Solutions

In context of Australia ; Please answer in detail and do not copy paste for any...
In context of Australia ; Please answer in detail and do not copy paste for any other source 1)A client has receipts for $50.00 from donations provided to door to door charity collectors. Can the client claim a tax deduction for this amount? Why? 40–50 words 2) A client wants to claim $300 for work-related expenses and says they might not have spent that amount but because it does not need to be substantiated they will still make the claim....
48. Answer the following using your own words, do not copy from another source. A. Define...
48. Answer the following using your own words, do not copy from another source. A. Define the term enterotoxin and give an example of an organism (Genus and species name) that causes food poisoning and produces an enterotoxin. B. Define the term cytotoxin and give an example of an organism (Genus and species name) that causes food poisoning and produces a cytotoxin. 50. Choose ONE of the following questions to answer. You can write your answer on a separate sheet...
Video Response 8.1 - "Search and Seizure" Instructions please do not copy and paste from internet-answer...
Video Response 8.1 - "Search and Seizure" Instructions please do not copy and paste from internet-answer based on the video-thanky ou Write a 200- to 400-word essay about the most important thing you learned while watching "Search and Seizure." video: you can find it easily by typing: Search and Seizure: Crash Course Government and Politics #27 or link if needed: https://www.youtube.com/watch?v=_4O1OlGyTuU&list=PL8dPuuaLjXtOfse2ncvffeelTrqvhrz8H&index=27
Please DO NOT copy-paste from other sources. answer will be checked for plagiarism. Thank you!!! Question:...
Please DO NOT copy-paste from other sources. answer will be checked for plagiarism. Thank you!!! Question: Using a Web browser, search for “incident response template.” Look through the first five results and choose one for further investigation. Take a look at it and determine if you think it would be useful to an organization creating a CSIRT. Why or why not?
Please DO NOT copy-paste from other sources. answer will be checked for plagiarism. Thank you!!! Question:...
Please DO NOT copy-paste from other sources. answer will be checked for plagiarism. Thank you!!! Question: Using a Web browser, identify at least five sources you would want to use when training a CSIRT. Using a Web browser, visit mitre org. What information is provided there, and how would it be useful?
Please DO NOT copy-paste from other sources. answer will be checked for plagiarism. Thank you!!! Question:   ...
Please DO NOT copy-paste from other sources. answer will be checked for plagiarism. Thank you!!! Question:    Using a Web browser, visit securityfocuscom. What is Bugtraq, and how would it be useful? What additional information is provided under the Vulnerabilities tab? Using a Web browser, visit certorg. What information is provided there, and how would it be useful? What additional information is provided at cert org/csirts/?
There are two answer already posted up on this website please do not copy and paste...
There are two answer already posted up on this website please do not copy and paste from one of those I can not use those, Please help I have posted this before but all I get is a copy paste, Please no less then 3 paragraphs the more the better THank you for your help and I will rate well. Select a familiar or local Common Resource or Public Good (see text for examples to consider). Assess the existence of...
IN YOUR OWN WORDS-PEASE DO NOT COPY AND PASTE ) PLEASE READ-->DO NOT ANSWER QUESTIONS__> Please...
IN YOUR OWN WORDS-PEASE DO NOT COPY AND PASTE ) PLEASE READ-->DO NOT ANSWER QUESTIONS__> Please determine in each questions wether it is biased and which is unbiased for both A & B. How their examples do or do not create a bias. 1) A) Do you think obesity is the cause of cardiovascular diseases (CVD)? B) Why people don’t lose weight to decrease risk factors for cardiovascular diseases (CVD)? 2 A) Do you think High Blood Pressure is caused...
Answer the following questions at least in 200 words each. Please do NOT copy and paste...
Answer the following questions at least in 200 words each. Please do NOT copy and paste from another source (Looking for answers not plagiarism). Please include any sources which were used. 1- According to Socrates, what is the value of justice? Why should we go to the trouble of being just? Is this discussion of justice still relevant today? How it affects economics? 2- Socrates believed that the "ideal city" should be comprised of three classes. What are these classes,...
Please provide detailed answer for each category. Do not copy paste. The statement of Cash Flows...
Please provide detailed answer for each category. Do not copy paste. The statement of Cash Flows has three categories, Operating, Investing, and Financing. Write the importance of each category to a potential investor and why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT