What's Your Secret?Due Tuesday, November 25, 11:59 PMThere has always been a need for private communications. Governments want to send sensitive messages to their diplomatic staff in foreign countries. Spies need to send secure messages to their headquarters. Credit card transactions need to be secure. Before computers existed, there was still a need to encipher messages so that they could not be read except by the intended receipient. There is some evidence that Julius Ceasar used a simple alphabetic substitution cipher to protect some of his messages - we'll talk about this in class. In the history of cryptography, there has been a constant battle between the cryptographers who invent new cryptographic techniques and the cryptananalysts who try to break them. Simple cryptographic techniques have to be replaced by more complex ones as the simple techniques lose their effectiveness. I would like you to write a program to encrypt messages using the Playfair cipher. This cipher was used by the British government from the mid 1800s until World War I. Although it has some significant weaknesses, it was a very good cipher for its time. Messages could be encrypted and decrypted using only pen and paper, the rules for encrypting and decrypting messages were easy to learn, and encrypted messages were very difficult to decrypt for someone who did not know the keyword. ObjectiveTo learn to use two dimensional arrays and the String and/or StringBuffer classes. Some TerminologyCleartext - the original message that you want to send. Ciphertext - the encrypted version of the message. Keyword - A secret word known only to the sender and receiver. The keyword is used to generate the key table. Key Table - A table that is used by the encryption algorithm to convert characters in the cleartext to characters in the ciphertext. It is also used by the decryption algorithm to convert the ciphertext back to the plaintext. Encrypt or encipher - to convert the cleartext to ciphertext Decrypt or decipher - to convert the ciphertext to cleartext. The Playfair CipherThe Ceasar cipher noted above is a type of monoalphabetic cipher - each character in the cleartext is replaced by a single character in the ciphertext. For example, every 'A' is replaced with a 'D', every 'B' with and 'E', and so on. The Playfair cipher is more complex. It bases its substitution on pairs of characters (or digraphs) from the cleartext, using a key table. This means that a particular character in the cleartext can be replaced by different characters in the ciphertext, depending upon what character it is next to.It also means that each letter in the ciphertext replaces more than one letter in the cleartext. For example, one instance of the letter C might be a replacement for A, while another instance of C replaced H. This makes encrypted messages much harder to decrypt if you don't know the keyword. The encryption algorithm is probably best shown with an example. We'll use the following keytable: B I O C H E M S T R Y F L Q W A G N U X D K P V Z This key table contains every letter of the alphabet except 'J'. Since the table has only 25 elements, the letter I is used to represent both I and J. The encryption algorithm has the following process
The decryption algorithm reverses these steps, using the same Key Table. Generating the Key TableThis algorithm requires the sender and receiver to use the same key table. Since it is not a good idea to write down the key table (what if it gets captured?), how can someone remember what it is? This is where the keyword comes in. The keyword is some word that the sender and receiver can both remember. It is used to generate the key table. Take the keyword, and remove all duplicate letters from it. For example, if the original keyword is 'everyday', then the resulting keyword is EVRYDA. Starting at the upper left corner of the keytable and progressing by rows, put one character of this keyword into the key table, like this E V R Y D A * * * * * * * * * * * * * * * * * * * Now, use the remaining letters of the alphabet (except J) to fill in the rest of the table, by columns: E V R Y D A G L P U B H M Q W C I N S X F K O T Z The keyword used to generate the table shown earlier was 'biochemistry'. Important:You need to replace any J's in the keyword with I's before removing duplicate characters. The ProgramYour program must do the following:
Methods and Arrays You must write methods in this program. Remember to think about top-level design when deciding what to put in each method. A method should do one task or subtask. Also, remember that a method can call other methods. Your program must use a two dimensional array for the key table. Make sure that you understand the Coding Standard rules for methods and arrays. String and StringBufferYou will probably find the String and StringBuffer classes useful for this assignment. Character arrays may also be needed. String and StringBuffer documentation is available from Sun Microsystems. Select String and StringBuffer from the list of classes in the lower left frame. Some methods from the String class that might be useful include indexOf(), charAt(), replace(), and toUpperCase(). Some methods from the StringBuffer class that might be useful include append(), charAt(), delete(), insert(), and toString(). Grading CriteriaYou can earn up to 25 points on this assignment. The points are allocated as follows
ApproachRemember, a good approach to follow when developing software is to do a little bit at a time. Pick a task, get it working, and then save your work. It is important to save your working versions so that when you add something to it that doesn't work, you can easily get back to a known good point. For this program I strongly suggest writing your psuedocode before trying to write any Java. I suggest the following course of action:
You can verify your program using my sample programs. Use SampleEncrypt to make sure that your ciphertext is the same as mine. Use SampleDecrypt to check that your ciphertext can be decrypted. TestingThere are a number of things to think about when testing this program:
Sample ProgramYou can run my version of the program by entering the command 'java SampleEncrypt'. Here is example run Please enter your keyword: pinecone P I N E C O F L S W A G M T X B H Q U Y D K R V Z Please enter your clear text: Saddam is in Tikrit The ciphertext is: OTZAPBGNFEEMFIKNXA Submitting Your ProgramWhen you are ready to turn your assignment in, use the following command submit cmps012a-bh.f03 hw6 Encrypt.java You can submit your program more than once. Later submissions replace earlier ones. Only the last one is saved, and is the one that will be graded. If you want to see what you have submitted, use the command peek cmps012a-bh.f03 hw6 Please follow these rules:
Extra Credit(10 points) Write a second program that decrypts your ciphertext and produces the original cleartext. Of course, you won't be able to put back the original whitespace and punctuation, but you should be able to generate the correct text. You also won't be able to tell which I's should be J's - we'll just have to live with that. The decryption algorithm reverses the steps in the encryption algorithm. For example, if both characters in the digraph are in the same column of the keytable, replace each character with the one above it. After completing the substitution, you will need to remove the X's that were added because the original cleartext digraph contained duplicate characters. It is OK to leave an extra X at the end of your cleartext. (Why? Well, what if your original message was "Use the ax"?) This program should be called Decrypt.java, and can be submitted using the command submit cmps012a-bh.f03 hw6 Decrypt.java Here is an example run from my sample program (java SampleDecrypt): Please enter your keyword: pinecone P I N E C O F L S W A G M T X B H Q U Y D K R V Z Please enter your cipher text: otzapbgnfeemfiknxa The cleartext is: SADDAMISINTIKRITX |