Working with loops, strings, and characters.
Encrypting messages is important for keeping messages secret and personal information secure. Intercepting and decrypting secret messages has played an important role in many wars by allowing one government to find out what their opponents were planning to do.
You are going to be writing a program to decrypt messages
encode by a substitution cypher. Substitution cyphers are
popular activities often found in newspapers along with
crossword and Suduko puzzles. For each letter of the
alphabet, you must substitute a different letter to decrypt
the message. The set of substitutions is know as the
decryption key. In the newspaper puzzles, the reader has to
figure out the decryption key, but for this assignment we
give you the decryption key. You just need to write a
program to perform the decryption.
A small example is shown below. The encoded message starts
with the letter 'N'. To decode that you find 'N' in the
alphabet at the top, and then the corresponding letter below
it is what it decodes to. In this case an 'H'. Letter by
letter you can decode the message: j -> e, g -> l, a
-> p. Any non-letters are not encrypted, so the
punctuation and spaces are simply copied. Try decoding the
rest of the message to make sure that you understand the
process. You are going to be writing a program to do this.
decryption key: PYFGCRLAOEUIDHTNSQJKXBMWVZ
message: Njga! Gjhfplpd oi afidfhw lq h gio ic xift.
||||||||||||||
decoded: Help! Learning __ _______ __ _ ___ __ ____.
To begin, write a program to loop through a string
character by character. If the character is a letter,
print a question mark, otherwise print the character. Use
the code below for the message string. This will be the
first string that you will decode. Use the String class
method .charAt(index) and the Character class method
.isLetter(char).
Copy and paste your code and results for Part 1 to your
solutions document.
Next modify your program to use this decryption key to decode uppercase letters. For each letter you will find and print the corresponding letter in the decryption key. You can just use simple character math. Recall that characters are stored inside the computers as numbers: A=65, B=66, C=67, etc. It is considered poor programming to use these numbers directly. Instead use character literal such as 'A' with the understanding that it corresponds to a number. If c is a character, the expression (c - 'A') will calculate an integer value of: 0 when c is 'A', 1 when c is 'B', 2 when c is 'C', etc. This value can then be used as an index to get the corresponding character at that position in the decryption key.
When it is working correctly you should be able to read the decrypted message.
Note: Use the String charAt(index) method. This
should only add 1 or 2 lines of code to part 1.
Copy and paste your code and results for Part 2 to your
solutions document.
Next modify your program to also decode lower case
letters. There are several ways to do this.
When printed, the case of a decoded character should match
the case of the original.
The Character class has methods: .isUpperCase(char),
.isLowerCase(char), .toUpperCase(char), and
.toLowerCase(char).
Below is a new message to decode. Cut and paste it
into your code.
"Bikf afidfhw nhq qkeejqqckggb mjefbaojm onj wjqqhdj.\n" +
"Whtj qkfj onho bik pix djo vion kaajfehqj hpm gixjfehqj gjoojfq lp bikf fjqkgo.\n";
Note: This only requires simple changes and adds 2 or 3 more lines.
Copy and paste your code and results for Part 3 to your solutions document.
Here is a message encoded with a different key. Your challenge is to use your program to help you figure out the decryption key. I suggest that you start with one letter words, then two letters, then three letters. There will be some guessing. Fill in your guesses one at a time in the decryption key, run your program and check that the results seem plausible. Also look for common words in the encrypted message and punctuation for additional clues.
String decryptKey = "__________________________";
String msg = "Klkp Seobpu tkm kp Gpulbmv riwdesgo mrbgpsbms, wksvgwksbrbkp,\n" +
"liubrbkp, roadskpklams, dvblimidvgo, kpy svgiogsbrkl zbiliubms.\n" +
"\n" +
"Yeobpu svg Mgripy Tioly Tko, Seobpu tiofgy xio svg Uiqgopwgps Riyg\n" +
"kpy Radvgo Mrviil (UR&RM) ks Zlgsrvlga Dkof, Zobskbp'm riygzogkfbpu\n" +
"rgpsog svks doiyergy Elsok bpsgllbugprg. Xio k sbwg vg lgy Ves 8,\n" +
"svg mgrsbip tvbrv tkm ogmdipmbzlg xio Ugowkp pkqkl roadskpklambm.\n" +
"Vgog vg ygqbmgy k pewzgo ix sgrvpbjegm xio mdggybpu svg zogkfbpu ix\n" +
"Ugowkp rbdvgom, bprleybpu bwdoiqgwgpsm si svg dog-tko Dilbmv ziwzg\n" +
"wgsviy, kp glgrsoiwgrvkpbrkl wkrvbpg svks riely xbpy mgssbpum xio\n" +
"svg Gpbuwk wkrvbpg. Seobpu dlkagy k dbqiskl oilg bp rokrfbpu\n" +
"bpsgorgdsgy riygy wgmmkugm svks gpkzlgy svg Kllbgm si ygxgks svg\n" +
"Pknbm bp wkpa roerbkl gpukugwgpsm, bprleybpu svg Zksslg ix svg\n" +
"Kslkpsbr, kpy bp mi yibpu vgldgy tbp svg tko. Bs vkm zggp gmsbwksgy\n" +
"svks svbm tiof mviosgpgy svg tko bp Geoidg za wiog svkp sti agkom\n" +
"kpy mkqgy iqgo xieosggp wbllbip lbqgm.\n";
Copy and paste your code and results to your solutions
document. What is the decrytion key?
Turn in your code and the results for each part of the
assignment.