Session 8: Week 15/16: <i>Encryption</i>




Document: Software Engineering 1: Course Notes

next Hints
up Term 2: Weeks 11-20
previous Session 7: Week 13/14: Lab Test #1

Session 8: Week 15/16: Encryption

"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 a 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, and 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 is called the 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 characters are read in with the getchar() function, what you receive (in int form) is the ASCII numeric code. 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 stream of characters, using getchar(), and encode them with a Caesar cipher; the ciphertext should be output with putchar(); the program should terminate when getchar() signals that the end of the input stream has been reached. The key for the encoding should be represented in your program with a symbolic constant.gif

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. There are various ways of doing this, but one relatively simple method is described in the hints section below.

Test your program carefully. Record in your report log book what tests you carry out, what results you expect, and what results actually occur.

Now think about the problem of cryptanalysis: that is, suppose you are "the enemy" and have intercepted a transmission encyphered with a Caesar cipher. How would you set about trying to decipher it? You may wish to challenge a colleague in the class to see who can decipher the other's ciphertext message most quickly. Record your conclusions in your report.

  Finally, if you have time, try to develop a program to implement the somewhat more sophisticated Vigenere cipher, invented in the 16th century by Blaise de Vigenere. This has the same basic idea as the Caesar cipher, but the key no longer stays the same throughout the message: instead it follows some periodic cycle. A Vigenere key is thus not a single number but a sequence of some fixed length - say 10, 3, 25, which would mean the first letter should be shifted by 10, the second by 3, the third by 25, and then back to 10 again etc.






Document: Software Engineering 1: Course Notes

next Hints
up Term 2: Weeks 11-20
previous Session 7: Week 13/14: Lab Test #1



McMullin@ugmail.eeng.dcu.ie
Wed Mar 15 10:20:49 GMT 1995