Enigma Machine |
A simple way to generate a random cypher is the following
- Create a
char[] cypher
array wherecypher[0] = 'a'
,cypher[1] = 'b'
and so on. Remember that ifi
is an integer then(char)('a' + i)
will be 'b' when i==1, 'c' when i==2, and so on. - 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 handyTip: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:
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.
Post a Comment