Monday, February 20, 2012

Lab 12: Random Cyphers

Enigma Machine
For this lab you will write a program that first generates a random cypher key and then uses that key to encrypt the users' messages. The cypher key is just a mapping from every letter in English (just the lower case letters) to another one.

A simple way to generate a random cypher is the following
  1. Create a char[] cypher array where cypher[0] = 'a', cypher[1] = 'b' and so on. Remember that if i is an integer then (char)('a' + i) will be 'b' when i==1, 'c' when i==2, and so on.
  2. Then, to randomize cypher simply pick 2 random indexes in the array and swap the values. Repeat this 100 times and the array should be properly randomized.

You will then use this cypher array to encrypt the user's message. Below is a sample interaction with the user:

Our cypher is:
abcdefghijklmnopqrstuvwxyz
jbeqkmrvayihonpfgdsxutczlw
Enter your message:
abc xyz
The encrypted message is:
jbe zlw
Enter your message:
Launch code Alpha, Tango, Tango, Bravo, Macho
The encrypted message is:
hjunev epqk jhfvj, xjnrp, xjnrp, bdjtp, ojevp
Enter your message:
how about a nice game of chess?
The encrypted message is:
vpc jbpux j naek rjok pm evkss?
Enter your message:

Tip: Remember to toLowerCase his input and ignore spaces and other characters. You will find that either charAt or toCharArray will come in handy

Tip:If you have char c; then cypher[(int)('a' - c)] will access the 0th position of cypher when c=='a'.

As always, turn it in on the dropbox.cse.sc.edu.

1 comment:

jmvidal said...

For this lab you can either create a Cypher Class or not, depending on what you think works better for you.

For the hackers among you, think about the pros and cons of each approach. When might one way be better than the other way? Think about how this code might get included into a bigger project, say, a file encryption system.