Question

Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher...

Language: C

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
42,3 frumious
48,3 vorpal
22,1 outgrabe
34,3 that
23,5 Beware
7,2 gyre
Placing these pairs into a cipher text, we get the following:
35,0,5,1,42,3,48,3,22,1,34,3,23,5,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

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

ANSWER :


// code to intercept any of the .cry files but without knowledge of the books used

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

#define N_CAPITAL_LETTERS 26
#define LTRTOIDX(x) (x-'A')

int main(int argc, char *argv[])
{
char plain[_MAX_PATH], codfile[_MAX_PATH];
char cipher[_MAX_PATH], positions[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];

FILE fp_plain, fp_cod, fp_cipher, fp_pos;
long pos[N_CAPITAL_LETTERS];
int ch_plain, ch_cod;

// Argument processing
if (argc != 3) goto error1;
_splitpath(argv[1], drive, dir, fname, ext );
if (strlen(ext) == 0) strcpy(ext, "cod");
_makepath(codfile, drive, dir, fname, ext);
_makepath(positions, drive, dir, fname, "pos");
_splitpath(argv[2], drive, dir, fname, ext );
if (strlen(ext) == 0) strcpy(ext, "txt");
_makepath(plain, drive, dir, fname, ext);
_makepath(cipher, drive, dir, fname, "cry");

// File opening
if ((fp_plain = fopen(plain, "r")) == NULL) goto error3;
if ((fp_cod = fopen(codfile, "r")) == NULL) goto error3;
if ((fp_cipher = fopen(cipher, "w")) == NULL) goto error3;
fp_pos = fopen(positions, "rb+");

// Position array intialization
if (fp_pos == NULL) memset(pos, 0, sizeof(pos));
else {
fread(pos, sizeof(long), N_CAPITAL_LETTERS, fp_pos);
fclose(fp_pos);
}
// Main loop
do {
ch_plain = toupper(getc(fp_plain));
if (isalpha(ch_plain))
{
fseek(fp_cod, pos[LTRTOIDX(ch_plain)], SEEK_SET);
do {
ch_cod = getc(fp_cod);
} while (!(ch_cod == ch_plain || ch_cod == EOF));
if (ch_cod == ch_plain) {
pos[LTRTOIDX(ch_plain)] = ftell(fp_cod);
fprintf (fp_cipher, "%1ld ", pos[LTRTOIDX(ch_plain)]);
} else goto error2;
}
} while (ch_plain != EOF);
// Termination
fp_pos = fopen(positions, "wb");
fwrite (pos, sizeof(long), N_CAPITAL_LETTERS, fp_pos);
fclose(fp_plain); fclose(fp_cod); fclose(fp_cipher); fclose(fp_pos);
return 0;
// Error handling
e1: printf ("USAGE: bkcode codfile message\n"); return 1;
e2: printf ("Run out of letters %c!\n", ch_plain);
fclose(fp_plain); fclose(fp_cod);
fclose(fp_cipher); remove(cipher); return 1;
e3: printf ("Can not open files\n"); return 1;
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

THANKS

Add a comment
Know the answer?
Add Answer to:
Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher...
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
  • Language: C Write an encoder and a decoder for a modified "book cipher." A book cipher uses a doc...

    Language: C 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...

  • Text book used: C Primer Plus 6th Edition, Stephen Prata Create count_ch() that will count all...

    Text book used: C Primer Plus 6th Edition, Stephen Prata Create count_ch() that will count all alphanumeric characters, all whitespace characters and all punctuation entered until the user simulates end of file from the keyboard. Also provide a total of ALL characters regardless of type. At the end, you should display 4 totals. (Also, review pages 252-254) If you copy and paste jabberwocky.txt, press enter, and then input the EOF simulation, you should have the following results... AlphaNumeric: 743, Punctuation:...

  • Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program...

    Help write down below program with C++ language!!! Please... The Cipher Program Requirements An interactive program is required that allows a user to encode text using any of three possible ciphers. The three ciphers you are to offer are: Caesar, Playfair and Columnar Transposition. • The program needs to loop, repeating to ask the user if they wish to play with Caesar, Playfair or Columnar Transposition until the user wishes to stop the program. •For encoding, the program needs to...

  • Implement the RC4 stream cipher in C++. User should be able to enter any key that...

    Implement the RC4 stream cipher in C++. User should be able to enter any key that is 5 bytes to 32 bytes long. Be sure to discard the first 3072 bytes of the pseudo random numbers. THE KEY OR THE INPUT TEXT MUST NOT BE HARD CODED IN THE PROGRAM. program should be tested by a plain text You should write TWO PROGRAMS: encryption and decryption. The encryption program should input the plaintext file and output a cipher text in...

  • USING C LANGUAGE Write a program to compute a Caesar Cipher . You must read in...

    USING C LANGUAGE Write a program to compute a Caesar Cipher . You must read in a file of text and convert some of the letters to another letter using the following array: char key[27] = “efghijklmnopqrstuvwxyzabcd”; This means that if you encounter the letter ‘a’, then you will replace it with the letter ‘e’,etc. If you encounter any other characters in the input file (example: ‘A’ or space, etc, you will write them as is. You will write each...

  • Implement the RC4 stream cipher in C++. User should be able to enter any key that is 5 bytes to 3...

    Implement the RC4 stream cipher in C++. User should be able to enter any key that is 5 bytes to 32 bytes long. Be sure to discard the first 3072 bytes of the pseudo random numbers. THE KEY OR THE INPUT TEXT MUST NOT BE CODED IN THE PROGRAM. Test your program with the following plain text: In cryptography, RC4 (Rivest Cipher 4 also known as ARC4 or ARCFOUR meaning Alleged RC4) is a stream cipher. While remarkable for its...

  • Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is...

    Using C++ Part C: Implement the modified Caesar cipher Objective: The goal of part C is to create a program to encode files and strings using the caesar cipher encoding method. Information about the caesar method can be found at http://www.braingle.com/brainteasers/codes/caesar.php.   Note: the standard caesar cipher uses an offset of 3. We are going to use a user supplied string to calculate an offset. See below for details on how to calculate this offset from this string. First open caesar.cpp...

  • I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text an...

    I need Help to Write a function in C that will Decrypt at least one word with a substitution cipher given cipher text and key My Current Code is: void SubDecrypt(char *message, char *encryptKey) { int iteration; int iteration_Num_Two = 0; int letter; printf("Enter Encryption Key: \n");                                                           //Display the message to enter encryption key scanf("%s", encryptKey);                                                                   //Input the Encryption key for (iteration = 0; message[iteration] != '0'; iteration++)                               //loop will continue till message reaches to end { letter = message[iteration];                                                      ...

  • This Question is from the book(Problem Solutions with C++ 10th Walter Savitch(ISBN-10 1292222824) Programming project 8...

    This Question is from the book(Problem Solutions with C++ 10th Walter Savitch(ISBN-10 1292222824) Programming project 8 chapter 6. m this book 8 This program numbers the lines found in a text file. Write a reads text from a file and outputs each line to the screen and to another file preceded by a line number. Print the line number at the start of the line and right-adjusted in a field of three spaces. Follow the line number with a colon,...

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