Take-home Assessment A3: Encryption Machine
Due July 13, 2021 11:59:59 pm PDT
This assessment will assess your mastery of the following objectives:
- Write a functionally correct Java program to produce specified console output.
- Use Scanner to accept and process user input.
- Write for loops, including nested loops, to repeat code and manage control flow.
- Write and call methods that accept parameters to perform generalized tasks.
- Write and call methods to represent distinct tasks within a program.
- Use class constants to represent and modify important values in a program.
- Follow prescribed conventions for spacing, indentation, naming methods, and header comments.
Background
Note: You do not need to read this section to complete the assignment, but it provides some helpful context that may make the assignment easier to understand.
The idea of encryption as a means to hide the meaning of messages has deep roots in history. The first known military use of encryption ciphers was in 100 BCE, when Julius Caesar used them to send secret messages to his generals out in the field. In the event that one of these messages got intercepted, his opponents could not read its contents easily. This process of encryption scrambles (in a deterministic fashion) the original message, known as plaintext, into an alternative form known as ciphertext. Today, encryption algorithms help to control data privacy and security all over the world. Algorithms such as RSA and AES are some of the most widely used ciphers that are vital to protect digital security. One of UW's professors, Neal Koblitz, is renowned for creating some of the most secure encryption algorithms today, elliptic and hyperelliptic curve cryptography.
For this assessment, you'll be implementing a Caesar cipher, a kind of substitution cipher. Substitution ciphers are what are known as symmetric-key algorithms. Symmetric-key algorithms are a special set of algorithms that use the same cryptographic key to both encrypt plaintext and decrypt ciphertext.
Key Vocabulary
- cipher: an algorithm for performing encryption and decryption.
- plaintext: the unencrypted input text input to a cipher algorithm.
- ciphertext: the unreadable output of a cipher algorithm
End-to-end encryption and decryption
The goal of encrypting conversation between two parties is to enforce privacy and prevent potential third parties from reading or modifying secret information. The set of ciphers you will work with on this assessment, substitution ciphers, rely on encrypting plaintext and decrypting ciphertext using a single cryptographic key. A cryptographic key is some piece of information (a string of letters from our alphabet) that is used to protect the integrity of a conversation. In principle, the key is only known by the two parties and can't be accessed by any third party.
To use a substitution cipher to encrypt their communication, both parties decide on a key word or phrase. The sender encrypts this key and includes it with the encrypted message so the other party can determine what shift was used and decrypt the message. Note that although your program will encrypt a key provided by the user, you won't need to use this key anywhere in your program. However, we will use this key to decrypt your encrypted secret messages.
Although you will only be implementing the encryption side of this algorithm, decrypting a message is a very similar process. Given an encrypted key, it's possible to determine what shift was used to encrypt it, and then the receiver can use that shift to reverse the original encryption process and determine the original plaintext message. Although you won't be implementing the decryption algorithm, we've implemented one for you that you can use to decrypt your secret message using the "Check" button when you submit it in Ed.
Program Behavior
- Expected Output
- User input is bold and underlined in the sample output in this document. Both samples use the default values for
SHIFT
andALPHABET
(3 and "abcdefghijklmnopqrstuvwxyz" respectively).
Welcome to the CSE142 Encryption Machine
The program lets you encrypt a message
with a key for your recipient to decrypt!
Encrypted messages use a shared keyword to decrypt.
Enter key: cseonefortytwo
"cseonefortytwo" has been encrypted to: fvhrqhiruwbwzr
How many words is your message? 4
Next word: computer
"computer" has been encrypted to: frpsxwhu
Next word: science
"science" has been encrypted to: vflhqfh
Next word: is
"is" has been encrypted to: lv
Next word: awesome
"awesome" has been encrypted to: dzhvrph
Message fully encrypted. Happy secret messaging!
Welcome to the CSE142 Encryption Machine
The program lets you encrypt a message
with a key for your recipient to decrypt!
Encrypted messages use a shared keyword to decrypt.
Enter key: cseonefortytwo
"cseonefortytwo" has been encrypted to: fvhrqhiruwbwzr
How many words is your message? 9
Next word: the
"the" has been encrypted to: wkh
Next word: quick
"quick" has been encrypted to: txlfn
Next word: brown
"brown" has been encrypted to: eurzq
Next word: fox
"fox" has been encrypted to: ira
Next word: jumps
"jumps" has been encrypted to: mxpsv
Next word: over
"over" has been encrypted to: ryhu
Next word: a
"a" has been encrypted to: d
Next word: lazy
"lazy" has been encrypted to: odcb
Next word: dog
"dog" has been encrypted to: grj
Message fully encrypted. Happy secret messaging!
In this program, you will build up an algorithm to explore the process of encrypting end to end communication between two groups of people. Unlike previous assessments, this program's behavior is dependent on input from a user (bold and underlined above). Your output should match our examples exactly when given the same input, but if the input changes, the output will also. Additional execution logs will be posted on the course website, and you can use the Mark button in Ed to check your output for various inputs.
The program begins with an introductory message that briefly explains the program, then prompts the user for a cryptographic key, encrypts it and prints out the ciphertext of that key. Next, the program prompts the user for the number of words in their message, reads in that many words, and prints out the encrypted ciphertext for each word. You may assume the user enters words from the lowercase english alphabet, though you should use a class constant so that your program can be easily modified to change this assumption (see below).
Caesar Ciphers
A Caesar cipher is a type of substitution cipher in which characters in the alphabet are substituted with other characters generated by shifting each character by a fixed number of positions to the right (3 by default). The default alphabet is the set of all lowercase english letters, but you should be able to modify the alphabet.
Using this substitution, here's how we would encrypt "play":
plaintext | p | l | a | y |
---|---|---|---|---|
+ shift | 3 | 3 | 3 | 3 |
= ciphertext | s | o | d | b |
More formally, the cipher substitutes each character in the alphabet for the character that is 3 positions ahead of it:
Notice that we want our cipher to "wrap-around" the alphabet as necessary. That is, shifting 'y' by 3 positions results in 'b'.
Building a Caesar Cipher
As outlined in the program behavior, characters in the alphabet are substituted with new characters generated by shifting each character a fixed number of positions to the right (3 by default). To perform this shift, you should not perform arithmetic with these characters. Although this is possible with Java, we require that you use a class constant to help search for the shifted characters. You should have a constant called ALPHABET
that maintains the alphabet for this program (described below). You should calculate a ciphertext character by finding the character that is SHIFT
characters ahead of the plaintext character in the ALPHABET
String.
Development Strategy
As usual, we recommend you approach the program in stages, rather than trying to complete everything at once. Specifically, we suggest implementing functionality in the following order:
- Single letter: Write a method to encrypt a fixed single letter with a fixed shift of 3.
- Parametrized letters: Modify your single letter encryption method to be able to encrypt any letter in your alphabet with a shift of three.
You'll need to have implemented your
ALPHABET
constant to do this. - Single word: Once your single letter encryption method completely works, write a method that encrypts a single word with a shift of 3 by calling your single letter method.
- User input: Prompt the user for a key and a message to encrypt and output the encrypted ciphertext.
- SHIFT constant: modify your program to use a constant to determine the amount to shift characters by.
Hints
- Remember that you can find the index of a specific character within a String with the
indexOf(char)
method - You can also find the character at a specific index within a String with the
charAt(int)
method - To allow for characters wrapping around the alphabet, it might be helpful to think of the alphabet as being circular. Use the mod (
%
) operator to wrap around! - If your program is generating
InputMismatchException
errors, you are likely reading the wrong type of values from yourScanner
(for example, usingnextInt
to read text).
Implementation Guidelines
Like all CSE 142 assessments, this assessment will be graded on all four dimensions defined in the syllabus. Be sure to adhere to the following guidelines:
Required methods/structure
For this assessment, your program is required to utilize the following structure:
- Your program must include a single method to encrypt a letter. This method should use parameters so that it can encrypt any letter
- Your program must also include a single method to encrypt a word. This must be achieved by calling the letter encryption method described in (1)
User Input
This program requires you to process user input, which you must do using a Scanner. All text input should be read using the next
method in the Scanner
class, not the nextLine
method. You may assume that both the key and word to be encrypted will be a String. You may also assume the user always enters valid input. Specifically, you may assume that:
- the user will always enter a value of the correct type.
- the user will only ever enter 1 word when asked for the the key and the next word to encrypt.
- the user will only enter letters in the defined
ALPHABET
constant (lowercase english letters, by default).
Class Constants
As described above, your program should work under the assumption that the user will use an alphabet of only lowercase english letters by default, but this value should be able to be easily changed. You must introduce a class constant for the alphabet, and use this constant throughout your program to determine what character to shift to. To ensure our testing and grading scripts work correctly, you must name this constant ALPHABET
. Please set the value of the constant to its default before submitting your work. By default, your ALPHABET
constant should be declared:
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
Your program should also be able to be easily changed to alter the shift. To achieve this, your program should include an integer class constant that represents the shift amount. (The default shift is 3.) The shift amount should be able to be altered by changing only the value of the constant. To ensure our testing and grading scripts work correctly, you must name this constant SHIFT
. We will only test your program with non-negative shifts.
See the "Files" section below for example logs with different values for the ALPHABET
and SHIFT
constants. The Mark button in Ed will test your program with different values for both constants.
Permitted Java Features
For this assessment, you are restricted to Java concepts covered in chapters 1, 2, 3.1, and 3.3 of the textbook, as well as specifically section 4.3. In particular, you MUST use for loops and parameters, and you may not use if or if-else statements, or returns. Although it is included in section 4.3, you should also not use character arithmetic.
Creative Aspect (secretmessage.txt)
Along with your program, you should write a "secret" message from you to your TA that should be encrypted with EncryptionMachine. The message can be anything you want, as long as it does not include hateful, offensive or otherwise inappropriate speech. Your TA will decrypt your message with your program and read it while grading. This part of the assessment will only contribute to the Behavior dimension grade and will not factor into grading on the other dimensions.
In order for your TA to decrypt your message, you should use the key cseonefortytwo
and include the ciphered key with your secret message. You should use the default ALPHABET
, but may use whatever value of SHIFT
you want. You should include your ciphered key on one line, and your ciphered message on another line.
- sample secretmessage.txt
ENCRYPTED KEY:
mcoyxopyebdidgy
ENCRYPTED MESSAGE:
ywkb sc wi pkfybsdo sxcdbemdyb
You should submit your secret message in the file secretmessage.txt
in this format in the Secret Message slide in the Ed lesson. You can submit it using the "Mark" button on that slide, and using the "Check" button on that slide will run our decryption program to decrypt your secret message.
Code Quality Guidelines
In addition to producing the desired behavior, your code should be well-written and meet all expectations described in the general rubric and the Code Quality Guide. For this assessment, pay particular attention to the following elements:
Capturing Structure
Your program must use the method structure described above, though you may include additional methods if you like. As always, your main method should be a concise summary of the program and you should not have any trivial methods. You should use static methods to accurately capture the structure of the output in your program. You should not produce any output in your main method.
Using Parameters
Your program should utilize parameters to define generalized methods that can be used to create various similar results. In particular, your program should include only a single Scanner which is passed as a parameter to all methods that need it. You should NOT declare your Scanner as a constant; you must pass it as a parameter. In addition, your methods should not accept any unnecessary parameters. For this assessment, a parameter is considered unnecessary if its value is unused, is always the same as the value as another parameter, or can be directly computed from the values of other parameters.
Code Aesthetics
Your code should be properly indented, make good use of blank lines and other whitespace, and include no lines longer than 100 characters. Your class, methods, variables, and constant should all have meaningful and descriptive names and follow the standard Java naming conventions. (e.g. ClassName
, methodOrVariableName
, CONSTANT_NAME
) See the Code Quality Guide for more information.
Commenting
Your code should include a header comment at the start of your program, following the same format described in assessment 1. Your code should also include a comment at the beginning of each method that describes that methods behavior. Method comments should also explicitly name and describe all parameters to that method. Comments should be written in your own words (i.e. not copied and pasted from this spec) and should not include implementation details (such as describing loops or expressions included in the code). See the Code Quality Guide for examples and more information.
Running and Submitting
You should write your program in a file named EncryptionMachine.java
and submit it in Ed. You can run your program by clicking the "Run" button in Ed. This will compile and execute your code and show you any errors, or the output of your program if it runs correctly. If you believe your output is correct, you can submit your work by clicking the "Mark" button in the Ed assessment. You will see the results of some automated tests. These tests are not fully indicative of the grade you will receive on this assessment, and your grade on this assessment is not final until you have received feedback from your TA.
You may submit your work as often as you like until the deadline; we will always grade your most recent submission before the deadline. Note the due date and time carefully -- work submitted after the due time will not be accepted.
Getting Help
If you find you are struggling with this assessment, make use of all the course resources that are available to you, such as:
- Reviewing relevant examples from lectures, section, and lab
- Reading the textbook
- Visiting support hours
- Posting a question on the message board
Collaboration Policy
Reflection
In addition to your code, you must submit answers to short reflection questions. These questions will help you think about what you learned, what you struggled with, and how you can improve next time. The questions are in the Reflection slide in the Ed assessment.
Files
- Caesar Cipher Slides
- expected_oneword
- expected_oneword_tricky
- expected_twowords
- expected_multiplewords
- expected_shift0
- expected_shift25
- expected_shift26
- expected_shift36
- expected_alphabet_ABCDEFGHIJKLMNOPQRSTUVWXYZ
- expected_alphabet_abcdefghijklmnopqrstuvwxyz0123456789
- expected_shift7_alphabet_0123456789