University of Calgary

caesar cypher

Simple Caesar Cypher
source: Katrin Becker, 1995
Write an encryption program that reads from a specified file and writes to a different specified file using a simple encryption scheme.
 
In its simplest form, a Caesar Cypher is an encryption scheme that uses a number N and replaces each character in the message with the character found N positions farther along in the alphabet. For example, if N = 1; then A becomes B, B becomes C, etc. If we are only using the alphabet, then Z becomes A. The version we will write will use the entire ASCII table (all 128 codes). To make it a little harder to 'crack', we will also use the character's position in the message to further modify N. For example, if N = 1, then the original message: "AAAAA" would be encoded as: "BCDEF".
 
NOTE: encrypted text will probably not be readable as text. In order to view the output in the file you must use the UNIX facility od (octal dump). ATTEMPTS TO DISPLAY THE OUTPUT FILE AT THE SCREEN MAY MESS UP YOUR SCREEN OR WORSE!!! ALSO: DO NOT ATTEMPT TO PRINT THE OUTPUT FILE (same reasons, plus it will annoy UCS and may result in many pages being printed which you may have to pay for).
 
Your program must be implemented as a Unix-style utility using C/C++ command line arguments. It should look something like this:
 
encrypt -e -n 3 source target
where -e indicates the message is to be ENcoded
-n 3 is the offset value for the Caesar Cypher
source is the name of the file containing the original message
target is the name of the file to receive the output
 
encrypt -d -n 3 source target
where -d indicates the message is to be DEcoded
-n 3 is the offset value for the Caesar Cypher (must be the same as was used to encode)
source is the name of the file containing the encoded message
target is the name of the file to receive the output
 
Requirements:
N must not be restricted (but must be adjusted internally to remain in the range 0-127; 0 means no encryption)
Both encode and decode must be accomplished by the same routines (decode simply subtracts the same values that encode adds).
 
Testing:
There are two sample executable versions in the course directory:
      /home/233/Encryption/encrypt
      /home/233/Encryption/encrypt2 { -e/-d -n are optional }
       
Test your program using at least the following files :
1. /home/233/Encryption/cheese.txt [see here]
2. /home/233/Encryption/lies.txt [see here]
3. /home/233/Encryption/needs.txt [see here]
4. your original source program (do not display this one; encode it, then decode to a new file and compare the original against the 2nd target file using diff)
 
BONUSES:
1. allow the user to choose the second number; the one that gets added to N for each subsequent character position in the message (default is 1)
2. if the user types 'encrypt' with no arguments, print a unix-style man page
3. allow the order of the arguments to vary (except the two file names which must have source name first)
4. allow optional arguments (if -d/-e omitted default to -e; if -n <N> omitted default to 13)



Updated: August 29, 2005 12:59 PM