Overview
For as long as people have been able to write they have tried to find ways to send messages secretly by hiding the message in some way. One way of encrypting a message is to hide it inside some other text. It works as follows: you and the intended message recipient both agree on a (large) piece of source text, such as a passage from a book, which you don’t share with anyone else. You then find a character position for each letter in your (much shorter) message within the larger piece of text, and send the list of positions. Here’s an example:1
2
3Source text (secret): The quick brown fox jumps over the lazy dog
Message to encrypt: bread
Encrypted message: 10 11 2 36 40
Of course, we could have taken alternative positions for ‘e’ (also found at positions 28 and 33) and ‘r’ (also at 29), but in this cipher we’ll always use the first occurrence of a letter.
If you look closely at the secret text above you’ll notice that it contains every letter in the English alphabet, which means we can encrypt any message we want (note we’ll be restricting ourselves to messages that do not contain spaces in this request). While this is likely to be true of any sufficiently long piece of text, what should we do if the message contains a letter not found in the secret text? In the absence of any valid substitute, let’s use 1 to indicate that it wasn’t found. Here’s an example of that:1
2
3Source text (secret): The quick brown fox jumps over
Message to encrypt: beak
Encrypted message: 10 2 1 8
ecovering the original message is simply a matter of looking up the character at each indicated position in the shared secret text, replacing any lost symbols with some agreed upon alternative, like ‘_’. A worked example:1
2
3
4Source text (secret): Shall I compare thee to a summer's day? Thou art more lovely and more temperate.
Received message: 0 1 2 1
14 0 11 14 2 13 14
Decrypted message: sha_espeare
Your request
Your request will consist of a single class, with all code in the main() method. Call your class SentenceCipher (that is, the source code will be in a file called SentenceCipher.java). If you choose to use an IDE other than DrJava, ensure that your program is not defined in a package (there should be no package statement at the top of the file).
When your request is complete, a user running your program will be prompted to enter the shared ‘source’ text, then asked if they want to encrypt or decrypt a message. The source text may contain any mixture of characters, including punctuation and spaces. Before continuing, the source text must be converted to all lower case.
After receiving the source text from the user, they will be asked to choose one of two options:
- Encrypt a message
- Decrypt a message
Note that both options finish by displaying the amount of information loss.
The three development and testing stages defined below will step you through creating a functioning cipher tool. You do not need to follow these suggested development stages, but if you do then a testing tool will be available that can run your partiallycompleted program with lots of different inputs and indicate when it works as expected and when it does not.
General guidance
Use the Scanner class (import it from the java.util package) to read the user’s input.
Your program will need to declare appropriate variables for holding the source text, user’s processing choice, and (for encryption) the word to be encrypted. Several other temporary variables will be needed.
Use constants to hold sensible default values and any special values that will be used repeatedly.
Example Sessions
In these example sessions the user’s input is highlighted. All other text is produced by the program.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29Sentence Cipher
Enter source text: The quick brown fox jumps over the lazy dog
Select a cipher option:
1. Encrypt
2. Decrypt
Choice: 1
Enter word to encrypt: bread
Result: 10 11 2 36 40 ‐2
Information loss (%): 0.0
Sentence Cipher
Enter source text: The quick brown fox jumps
Select a cipher option:
1. Encrypt
2. Decrypt
Choice: 1
Enter word to encrypt: tea
Result: 0 2 ‐1 ‐2
Information loss (%): 33.33333333333333
Sentence Cipher
Enter source text: The quick brown fox jumps
Select a cipher option:
1. Encrypt
2. Decrypt
Choice: 2
Enter positions (‐2 to end): 0 2 ‐1 ‐2
Result: te_
Information loss (%): 33.33333333333333
which is identical to if the user pressed Enter between each position (no change to your program will be necessary)
Development Stages
The three request stages take you through developing: a program that can encrypt or decrypt a single character; a program that can encrypt and decrypt an entire word; and finally a program that can process an entire word and report on characters that could not be encrypted or decrypted and invalid positions given during decryption. Only the final stage directly contributes to your mark for this request.
Over the coming weeks you are encouraged to complete and test each of the following stages in turn. You are of course free to complete each stage more quickly or even to jump straight to the last one (if you believe you have a perfect solution).
Evaluation and testing prior to submission
Use the supplied testing tool to run automated tests on your program. The later tests also look for the number of invalid characters and information loss and will indicate if either is not found (when expected) or appears incorrect.