Program 5, A Cryptic Message

Short Description:

Write a program which decodes a series of secret messages.

This assignment is worth 50 points.

Goals

When you finish this homework, you should:

Formal Description

The ministry of communications at the Eastern Union of People (EUP) needs to be able to communicate with their information gatherers across the world. The EUP does not fund the ministry of communications very well, so the ministry is unable to spend money on expensive spy gadgets, supercomputers, algorithm development, or even an extensive programming department. In fact, the head of the ministry has learned of your programming experience and has turned to you to assist in decrypting the incoming messages from the agents in the field.

The agents collect information and then encode important facts in messages for the ministry using the DDCA (Dan's Dumb Cryptography Algorithm). The agents post these messages to the ministry's FaceTweeter account. FaceTweeter is a social media site which allows users to post formatted messages of any length.

During the day, head of external communications captures messages posted to FaceTweeter by the agents and stores these messages in files on the ministry's computer. Each file contains a single message. In addition the head of external communications creates a master file for each day called master.lst. The master file consists of a number of lines containing a single word. This word is the base name of a message file, or message name.

Your job is to decode the messages at the end of the day by reading the contents of master.lst. For each message name in the master file decode the corresponding message file. This file is stored in a file with the name constructed from the message name and the extension .crypt. The decoded code should be stored in the results in a file where the file name constructed from the message name with the extension .decoded.

For example, if the file master.lst contains the following message names:

message1
message2
the head of external communications will have created the input files
message1.crypt
message2.crypt
Your program should read these files, decode the message and create the output files
message1.decoded
message2.decoded
which contain the code or an error message.

Please note, master.lst, along with any files it names will be stored in the directory in which your program is run. master.lst will exist and will contain 0 or more message names. The named files may or may not exist. If the input file exists, calculate the code and store this in the output file. If the input file does not exist, create the output file, which contain a single line: Error: xxxx.crypt does not exist. where xxxx is the base name of the file master.lst.

Please be careful, the contents of master.lst are not fixed in size of value and will change every day.

DDCA Method of Decryption

The purpose of DDCA is to encrypt hidden messages, or code, inside of plain text message or text. A text consists of a series of lines, separated by the new line character. A line is further broken into words, which are a series of non-white space characters separated by a single space. The first word of a line has no leading white space and the last word on a line is terminated by the new new line character.

Code is encrypted in DDCA by creating lines in a text. The first word of each line of the text is at position 0 and is called the DDCA Key Word (DKW). The length of the DKW provides an offset into the line where the next word in the code, or the code word (CW), is stored. This offset is modified by the case of the first letter in the DKW. If the letter is upper case, the offset is from the left of the line, a lower case letter indicates an offset from the right of the line.

Offsets from the left correspond to the position, while offsets from the right correspond to the length of the line minus the offset. Words in a line are always numbered starting with 0 from the left and 1 from the right.

The entire code is formed by concatenating the CWs in the order they appear in the text. Spaces should be added between each CW.

For example, in the line "She always says hello to all of her friends," The DKW is "She". "She" is of length three, and starts with an upper case letter, so the encoded word is at the third position starting from the left, counting from 0. In this example the word "hello" is at the given position , so the CW is "hello".

A second example, the line "and her friends, all across the world say hi!". The DKW is "and". Again the length is three, but the word begins with a lower case letter, so the position is from the right. In this case the CW is "world".

Position:  0     1     2    3    4  5   6  7     9
          She always says hello to all of her friends,
Position:  9   8     7      6     5    4    3    2  1	
	  and her friends, all across the world say hi!
    

If a text contains the following:

She always says hello to all of her friends,
and her friends, all across the world say hi!
The code is "hello world"

Example Input

The following files used as input: Should produce the following output files:

Discussion

You may assume that there is exactly one space between words in a line of any message.

If a line of the message is not properly formed (ie the first word has more letters than the length of the line) emit the word ERROR in place of any code word for that line.

Your program must be written using functions.

The length of functions should be limited to 30 lines. Points will be deducted for extremely long functions.

You should make appropriate use of constants and variables.

Your identifiers should be constructed properly, with meaningful names.

You should document your code with comments.

Your program will operate in batch. There will be no interactive input or output.

Your output should follow the output specification exactly.

There is no redo on this program.

Required Files

A single source code file.

Submission

Submit the assignment to the D2L folder Program 5 by the due date.