For example, consider the substitution key
c d e f g h i j k l m n o p q r s t u v w x y z a b
To figure out how to substitute a letter with this key, line
up the alphabet with the key, as in:
a b c d e f g h i j k l m n o p q r s t u v w x y z // Regular plain text letters.
c d e f g h i j k l m n o p q r s t u v w x y z a b // Substitution code.
Thus, to find the code for 'f', look at 'f' in the top line. The code for
'f' then, is the letter directly below, which in this case is 'h'.
Here's another example.
a b c d e f g h i j k l m n o p q r s t u v w x y z // Regular plain text letters.
q w e r t y u i o p a s d f g h j k l z x c v b n m // Substitution code.
The code for 'f' in this case is 'y'.
In a Java program, we'll represent the substitution key
with a string as in:
String subKey = "qwertyuiopasdfghjklzxcvbnm";
char[] key = subKey.toCharArray ();
Note: it's easy to convert a string to a char array as shown above.
Your goal in this problem is to write encryption and decryption
methods for the above key. Start by creating a method
called encode
with the following signature:
static char encode (char c, char[] key)
{
// Returns the substitute character based on the given key.
}
Similarly, you'll want to write a method called
decode
that, when given the coded letter, returns the original.
Next, write methods called
encrypt and
decrypt that encrypt and decrypt
a whole char array, respectively. For example, encrypt
would look like this:
static char[] encrypt (char[] text, char[] key)
{
// Return an encrypted version of the text, given the key.
}
Now, test your encryption with "attackatdawn".
You should get "qzzqeaqzrqvf". To test decryption,
see what the following decrypts to:
"rorngxitqkqwgxzzitofzvigvqsatrofzgqwqkqfrgkrtktrqrgxwst".
Call your program
SubstitutionCipher.java.
In a rotation cipher, the substituion key is slightly
changed at each step. Consider the encryption of "attackatdawn"
with the substitution key "qwertyuiopasdfghjklzxcvbnm".
The first letter in the plain text is 'a'. To encode 'a',
we use
a b c d e f g h i j k l m n o p q r s t u v w x y z // Regular plain text letters.
q w e r t y u i o p a s d f g h j k l z x c v b n m // Substitution code.
Thus, 'a' is substituted by 'q'.
Now, as soon as this is done, we "rotate" the key before encoding
the next letter:
a b c d e f g h i j k l m n o p q r s t u v w x y z // Regular plain text letters.
w e r t y u i o p a s d f g h j k l z x c v b n m q // Rotated key.
So, now, to encode 't', the encoded letter is 'x'. Then,
the key is rotated again:
a b c d e f g h i j k l m n o p q r s t u v w x y z // Regular plain text letters.
e r t y u i o p a s d f g h j k l z x c v b n m q w // Rotated key.
Now, the next 't' (in "attackatdawn") gets translated to 'c'.
Thus, at each step a rotation is performed of the key, which
shifts each letter left by one spot in the array, moving
the first one to the last position.
Your goal here is to implement a rotation cipher in a program called RotationCipher.java. If you do this correctly, "attackatdawn" gets encrypted to "qxcruhuqspun". To test decryption, see what the following decrypts to: "ihnhtltvmbjedmqzipsivevatrqdapsniwzabmmkvookmzsnlbhrhljthpl".
Recommended, but not required: This week's theme: computer vision for the blind: