Homework 1, Project Encryption
Short Description:
Write a program which will support a number of encryption algorithms.
This assignment is worth 100 points.
Goals
When you finish this homework, you should:
- Have reviewed basic C++ and refreshed your programming skills.
- Have reviewed and strengthened your design skills.
- Have reviewed and strengthened your skills with procedures and functions.
Formal Description
You have been hired by the Florin Ministry of Foreign Information to assist
in maintaining communications with their agents in the field. Each day,
the ministry prepares communications which they have received, or are
preparing to transmit, in a single batch run. The commands for the daily
run are inserted into the file Commands.dat, the format of which will be
discussed below. Messages, both to be sent and those received are each entered
into a separate file.
The Ministry supports a number of different encoding, and has, over the years,
developed a system for identifying the actions to take on a message. This
system is summarized in the following table:
Code Letter | Meaning | Additional Arguments |
A | Convert characters to numeric ascii value | |
a | Convert two digit pairs to character | |
I | Increase all digits by 1, no carry | |
i | Decrease all digits by 1, no carry | |
o | Reverse the order of the characters in each word | |
O | Reverse the order of characters in the entire message | |
C | Employ the caesar cipher | Shift |
S | Employ the self encoding caesar cipher | + for add, - for subtract |
P | Translate into pig-latin | |
p | Translate from pig-latin | |
> | Copy | |
+ | Merge | Second Source |
= | Compare | |
The file Commands.dat will be in the following format:
- Each action will be described in a single line.
- The format of the line is as follows
- The actions must be executed in order.
- Input file name, a single block of non-whitespace characters
- Command, a single letter from the table above.
- Output file name, a single block of non-whitespace characters
- Additional arguments, in the order described below.
- Additional arguments:
- The command C: a single integer, the shift factor
- The command S: a single character, + for add or - for subtract
- The command R: a single integer, the initial seed
- The command +: a file name, a single block of non-whitespace characters, the second source file,
- The command C: a single integer, the shift factor
- Any characters after the input arguments should be ignored.
Some examples:
FILE1 A FILE2 Read from FILE1, write to FILE2 converting all characters to ascii equivalent
FILE2 a FILE3 Read from FILE2, write to FILE3, converting all two digit numbers to ascii equivalent.
CODE1 C message 3 Read from CODE1, write to message, shift all letters by 3
FILE2 + FINAL_MESSAGE message Copy FILE2 and message to FINAL_MESSAGE
= FILE1 FILE2 Compare FILE1 to FILE2
Algorithm Discussions:
Ascii to Character and Back
Convert each letter to the ascii equivalent. Convert two character pairs, if possible, from ascii to the character equivalent.
The Ministry has trained all employees to work in hexadecimal (hex),
so all values will be stored in hex. The ministry also requires proper
formatting, so all values will be exactly two characters long. If an error
is encountered in a pair (ie character > 'f'),print out the pair
without change. Encode white space, but do not encode new line characters.
Upper case letters between A and F are valid.
Example (A):
Attack at dawn!
Becomes
41747461636b206174206461776e21
Example (a):
41747461636B206174206461776E21hk1
Becomes
Attack at dawn!hk1
Increment/Decrement Digits
Encode a message by incrementing or decrementing all digits in the message.
All valid hex digits should be incremented or decremented by one. Non-hex
digit characters should be output without change. There is no carry or borrow
in this encoding, so '9' becomes 'a', 'f' becomes '0' when incrementing, and
'0' becomes 'f' when decrementing.
Example (I)
41747461636B206174206461776E21hk1
Becomes
52858572747c317285317572887f32hk2
Example (i)
b eby in dbmp is 0un!
Becomes
a day in camp is fun!
Reverse word character order.
Reverse the letter order within each word.
A word is defined by the characters surrounded by white space character.
Example:
Attack at dawn!
Becomes:
kcattA ta !nwad
Reverse message character order.
Reverse all characters in a message.
Example
Attack at dawn!
Becomes:
!nwad ta kcattA
Caesar Cipher.
A caesar cipher is found by shifting all letters in a message by a fixed amount.
For a simple caesar cipher, all letters are shifted by a given key. Note valid keys are between -25 and 25. Larger keys should be adjusted into this range.
Upper case letters should be preserved in this encoding.
Example: (C, 3)
Attack at dawn!
Becomes
Dwwdfn dw gdzq!
Self Encoding Caesar Cipher.
This cipher algorithm uses the message as the key. For a given message, the
first letter is just output. The second letter is shifted by the distance of the first letter from a, the third letter is shifted by the distance of the second letter from a and so on. Non letters are not encrypted. Upper case letters
should be preserved in this encoding.
When encrypting a self encoding ceaser cipher, compute the key based on the
current letter. When decrypting a self encoding ceaser cipher, you should
compute the key based upon the shift of the current letter.
Example: (C, 3)
Attack at dawn!
Becomes
Atmtcm kt wdwj!
Translate into Pig Latin
Translate each word into Pig Latin. To translate a word into pig latin.
- Locate the first vowel in the word (a,e,i,o,u). If there is not a vowel in the word, do not translate it.
- The word is now in the form prefixVpostfix, where prefix are the characters which proceed the first vowel (V) and postfix are the letters which follow V.
- Output the word Vpostfix-prefixay.
Example:
Attack at dawn!
Becomes:
Attack-ay at-ay awn!-day
Translate into Pig Latin
Reverse the translation into pig latin. If a word is not in pig latin,
output the work unchanged.
Discussion
- You may assume that all input in Commands.dat is correct and error free.
- Your code SHOULD NOT crash or hang when an error in the message is encountered.
- atoi, isdigit and isblank are a few functions from the isalpha family of functions. You might want to investigate these, and other associated functions.
- Care should be taken when performing a caesar cipher to make sure you
don't encounter problems due to coercion.
- Whenever possible, eliminate repeated code by building subroutines. If you find you are repeating code, pull it out and put it into a subroutine.
- You should probably do a preliminary design on this set of programs.
- You should probably write some experimental code, and then revise your design.
- You should create a sub-directory and perform all work for this project within this directory. This will make the creation of the tar file easier.
- If you need help, you should ask.
- Please do not put this work off until the weekend before it is due.
Required Files
You should submit the following in a tar file:
- All source code required to build all programs.
- A makefile which will build all programs as well as clean the directory
- A README describing
- Author identification information
- The project
- Problems encountered
- Any portions of the code which are not working.
- Any unique features of the code.
- Any other information you wish to share.
You should not submit
- Object files or executable.
- Core files.
- Source code files which are not related to the project.
Submission
Please create a tar file containing the files described above. Email this file to danbennett360@gmail.com by class time of the due date.