Exercise 1: The Caesar Cipher (50%)




Document: Software Engineering 1: Lab Exercises

next Exercise 2: Crytanalysis (20%)
up Session 9: Week 18/19: Encryption
gif Session 9: Week 18/19: Encryption

Exercise 1: The Caesar Cipher (50%)

"Encryption" is the process of disguising a message or text so that its meaning will not be apparent to anyone who may intercept it, accidentally or otherwise.

In this exercise you will work with a very simple encryption procedure. This is called a substitution cipher, in which each letter of the original message (the "plaintext") is replaced by a different letter to generate the coded message (the "ciphertext").

To simplify matters further, we will restrict our plaintext to use only the upper case letters A to Z, together with the space and newline characters; furthermore, space and newline characters will be left unchanged by the encryption.

In each case, the replacement letter will be a fixed number of letters later in the alphabet compared to the original letter (and "wrapping around" back to A again after Z, as necessary). The number of letters to count forward will be called the encryption key.

Thus, with a key of 3, we would replace A by D, B by E and so on, with, finally, W being replaced by Z, X by A, Y by B and Z by C.

This simple kind of cipher is sometimes called a "Caesar Cipher" after Julius Caesar, who is said to have used it for secure battlefield messages.

To implement a Caesar cipher with a C program, note that, when accessing individual characters in C, what you are "really" dealing with are numeric codes for the characters. As such, you can do any normal arithmetic (addition, subtraction etc.) and comparison (greater-than, less-than etc.) operations on them.

The relationship between the characters, as displayed on your screen or in your editor, and the numeric codes accessed within your C program is strictly arbitrary. It is not specified by the C language standard. It is thus open to anyone developing a C compiler to adopt any character numbering scheme they like; you cannot reliably assume that the numbering scheme in effect for one C compiler will be the same as for another. Strictly, you need to check the documentation for your particular compiler to find out what the numbering scheme is.

However: there is one particular numbering scheme which is the most widely used, and to which most C compilers conform (including DJGPP): this is the so-called ASCII scheme.gif In ASCII, the upper case letters A to Z have consecutive numeric codes from 65 to 90. Thus, given a particular key value (a number from 1 to 25) simple addition will encypher a letter as required - except that if the sum is greater than 90 you must "wrap around" to the start of the alphabet again by subtracting 26.

You are required to develop a program which will input a series of lines of characters, and encode them with a Caesar cipher; as each line of plaintext is input, the corresponding line of ciphertext should be output. It is up to you to devise a mechanism whereby the user can indicate that all lines have been input, and to implement the program in such a way that it will then terminate.

The key for the encoding should be "hard coded" into your program (as opposed to be being read in at run time). Thus, changing the key will involve modifying the source program and recompiling.

You may find it handy to be able to run your program on an input file (and producing an output file) rather than working only with the keyboard and screen. Use command line redirection to achieve this.




Document: Software Engineering 1: Lab Exercises

next Exercise 2: Crytanalysis (20%)
up Session 9: Week 18/19: Encryption
gif Session 9: Week 18/19: Encryption



McMullin@eeng.dcu.ie
Tue Apr 30 14:15:37 GMT 1996