Question

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 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

// 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

{

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 uses a doc...
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...

    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...

  • 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...

  • In this assignment, you will write a program in C++ which uses files and nested loops...

    In this assignment, you will write a program in C++ which uses files and nested loops to create a file from the quiz grades entered by the user, then reads the grades from the file and calculates each student’s average grade and the average quiz grade for the class. Each student takes 6 quizzes (unknown number of students). Use a nested loop to write each student’s quiz grades to a file. Then read the data from the file in order...

  • 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...

  • Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher,...

    Develop a Java application that uses a type of encrypted alphabet, called a random monoalphabetic cipher, to encrypt and decrypt a message. Your encryption key and message are to be read in from two different files and then the encrypted message will be output to third file. Then the encrypted message is read in and decrypted to a fourth file. A monoalphabetic cipher starts with an encryption word, removes the redundant letters from the word, and assigns what’s left to...

  • Write a Java program which uses the LWJGL library to draw a window of 640x480 (with...

    Write a Java program which uses the LWJGL library to draw a window of 640x480 (with a black background). The coordinate system should be centered in this window. Your program will read a file titled coordinates.txt and draw the corresponding filled polygon in this window using the scanline polygon fill algorithm. Each specified polygon should be filled in the color specified in the text file and then undergo the transformations specified in the input file before being drawn on the...

  • Python program This assignment requires you to write a single large program. I have broken it...

    Python program This assignment requires you to write a single large program. I have broken it into two parts below as a suggestion for how to approach writing the code. Please turn in one program file. Sentiment Analysis is a Big Data problem which seeks to determine the general attitude of a writer given some text they have written. For instance, we would like to have a program that could look at the text "The film was a breath of...

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