Skip Site NavigationCarleton College: Home
 
 

Overview

An important class of algorithms that some computer scientist concern themselves with are encryption and decryption techniques. How can text be encoded so that no one can read it apart from the desired recipient? This is critical for trade secrets and government messages, but even more critical for such common needs as secure web-based purchases, being able to send your password over the web without others listening in, and so on.

An old simplistic trick is the rotation cipher. Pick a key from 1 and 25; then for each character in your message, shift each letter forward by the key, wrapping around the end of the alphabet. For example, if your original message is "hi how are you", and your key is 2, your encrypted message is "jk jqy ctg aqw". Supposedly, Julius Caesar used this technique to communicate with his generals.

Your assignment

Create a directory called rotation. You will actually write two programs. The first one, called Encoder.java, should read a message in English contained in a file called source.txt. Your program should assume that your input text is entirely in lower case, with no punctuation. Encoder.java should pick a key from 1 to 25 at random and encode your message, printing it out to the screen.

Your second program, called Decoder.java, should read an encrypted message from a file called encrypted.txt. Your program should assume that your encrypted text is entirely in lower case, with no punctuation. Decoder.java should try all keys from 0 to 25 (including 0 to see the original), and print out each message to the screen. The user can then look at the 26 messages to see which one turns out to be in English.

Tips

  • When you run Encoder, it might be convenient to have your program dump the output right to encrypted.txt so you can try to decode it. If you write Encoder to dump directly to the screen, you can dump to a file by running your program as follows:

    java Encoder > encrypted.txt
    
  • When testing out Encoder, you might find it a lot easier to pick your key directly right inside your program (key=2 or whatever), and only change it to choosing randomly after everything is working. That way you can more easily test your message by hand, and also verify that "wrapping around the alphabet" is working as it should.

  • Since we are assuming that all the input is lowercase text (and spaces), you should use a traditional Scanner approach: use the next method to grab one word at a time.

Have fun, and good luck!

Available from: Monday, April 9 2007, 09:55 AM
Due date: Wednesday, April 11 2007, 11:55 PM