CodeBreaker

advertisement
APCS: TOP SECRET Project
Mission Deadline: October 27th: 0800 hours
Emailed to kknudtzon@cathedral.org
Welcome to the world of secrets and intrigue… the world where the craziest ideas just may
be the solution you’re looking for… the world that few people understand, and fewer are
allowed to talk about… In this project we’re going to enter the ultra-secret world of
espionage and cryptography, the world of the NSA.
This project will let you start taking advantage of your programming skills to accomplish reallife tasks and problems. Maybe this project won’t get you an immediate job offer from the NSA,
but the concepts we’re addressing here will be the same that those folks use every day, just a
more complicated version of what we’re going to do. And what we’re going to do in this project
is create a program (we’ll expand on it throughout the year) that will allow you to send messages
in code.
We, of course, will be using non-classified codes to send our messages. I can’t share with you
anything that deals with real-life cryptography or codes that the US government is currently
using, but this project will introduce you to simple encryption and decryption – enough at least to
fool your parents1. So let’s get started!!!
The Project:
Your main task in this program is to write a series of methods that will encode and decode
words. Later we will be extending this program to encode and decode lines of text and even
entire text files. In this project we will use three different algorithms or codes.
Part 0 – Helper Methods (Feel free to use additional helpers as you need them)
1. boolean isVowel(String s) or boolean isVowel(char c) – returns true
if letter or char is a vowel
2. boolean isConsonant (String s) or boolean isConsonant(char c)
– takes in one letter (as a String or as a char) and returns true if it is a consonant. (You
could do both if you want). Note: you may assume that a person only enters letters in
their words, so if you want, this method could just be the “opposite” of the isVowel
method.
1
These codes not guaranteed to fool parents, teachers, or adults of any kind, especially anyone
who may work at the NSA. The individual assigning this project will not be held responsible if a
message you send in these codes is broken and you get into trouble.
Part I – Substitution Code
1. Encryption: In this code, you are going to replace all vowels (not y) with a symbol. So in
the word you accept as a parameter, you should replace all vowels according to the
following table:
English Letter
Code Symbol
A or a
*
E or e
$
I or i
%
O or o
_ (underscore)
U or u
!
2. Decryption: For this code, you need to simply perform the reverse replacement of the
various symbols.
Part II – The old “Switcheroo” Code
1. Encryption: In this code, you will be swapping letters in the word provided as a
parameter. The result of the encryption will be that the first and second-to-last letters will
be swapped, and the second and last letters will be swapped.
a. For words of length 4 or more, the algorithm is as described above.
b. For words of length 2 or 3, you will only do one swap of the first and last letters.
c. For words of length 1, you will not perform any action.
2. Decryption: You must work this code in reverse and perform the “inverse switcheroo” in
order to read the original message.
Part III – Pig Latin
1. Encryption: For this method you will take in a word (as a parameter) and encrypt it into
Pig Latin. You will want to use your helper methods. For this assignment (and for ease of
decryption) we are going to slightly change the Pig Latin rules:
a. For words that begin with a consonant, move the consonant to the end of the word
followed by ay. For example: cat would become atcay; dog would become ogday
b. For words that begin with a vowel, just add xay to the end of the word. For
example is would become isxay and arm would become armxay
2. Decryption: For this method you will take in a word and decrypt it, i.e. take a pig latin
word and turn it back to English. You must use the String’s suffix method in your decrypt
method.
a. If words end in xay, we will assume the word did not start with an x, but that it
was a word starting with a vowel; so we simply remove the xay and the word
should be intact.
b. Otherwise we need to remove the ay, get the last letter, and put it back to the front
of the word.
Part IV – Numerical Replacement
1. Encryption: For this method you will use a helper class (Binary) that I will give you
(by posting it to the website). You will split your word into individual letters (either to
Strings using substring(int begin, int end) or to chars using
charAt(int index). Then you will use the Binary Class’s
convertLetterToBinary method that will take in a String (single letter) or a char
and return the binary code for it (as a String). This method should turn the word into a
string of binary codes, with spaces between each code.
a. For example, “cat” will become “1100011 1100001 1110100”
2. Decryption: For this method you will use the Binary helper class’s
convertBinaryToWord method to reconstruct the word from the series of binary
codes.
Part V – Substitution Code
1. Encryption: (Odd Character Coding): For this method you are going to replace every
other letter by its “pair” letter. A pair letter is the letter that is 13 letters away from the
letter (like a-n, b-o, c-p, etc).
a. For example, “string” becomes “fteiag”
2. Decryption: For this method you will simply do the same algorithm again to return the
add characters back to normal.
Extra Credit:
1. Expand your code to work with a line of text, not just a word. To do this, you will need
another Scanner object to bring up a line of text (as a string) into individual strings.
Download