Question
Learning Outcomes Design a class hierarchy using inheritance. Write polymorphic methods that operate on different types of data. Use abstract classes and interfaces to specify
Learning Outcomes
Design a class hierarchy using inheritance.
Write polymorphic methods that operate on different types of data.
Use abstract classes and interfaces to specify methods for other classes to implement.
Implement classical cryptographic ciphers.
For this assignment you will be writing a program that encrypts and decrypts text messages using substitution ciphers. In a substitution cipher, every letter in the unencrypted plaintext is replaced by a unique letter in the encrypted ciphertext.
The simplest example of a substitution cipher is the Caesar cipher, which substitutes every letter with the letter three positions after it in the alphabet. This means A is replaced by D, B is replaced by E, C is replaced by F and so on. If there is no letter three positions after a given plaintext letter, the Caesar cipher goes back to the beginning of the alphabet. This means X is replaced by A, Y is replaced by B, and Z is replaced by C. As an example, the Caesar cipher would encrypt the word SECRET as VHFUHW. To decrypt a ciphertext message, simply replace each letter with the letter three positions before it, going back to the end of the alphabet in the case of A, B, and C. A shift cipher is a generalization of the Caesar cipher, where the number of positions shifted is represented by a key that can be any number between 0 and 25.
The shift cipher is very weak because it uses the same key to encrypt each letter of the plaintext. The Vigenre cipher improves on the shift cipher by cycling through several keys when encrypting plaintext. It does this by introducing a fixed length keyword where each unique letter represents a unique key. The letter A corresponds to key 0, B corresponds to key 1, C corresponds to key 2, and so on. For example, the plaintext THE TRUTH IS OUT THERE would be encrypted using the keyword MULDER as follows:
plaintext: THE TRUTH IS OUT THERE keyword: MUL DERMU LD ERM ULDER ciphertext: FBP WVLFB TV SLF NSHVV
In fact, the Caesar cipher is just a specialized case of the Vigenre cipher where the keyword is D.
In order to perform the operations required for these ciphers you will need to use the following Java String object operations:
Given string str, str.length() returns its length.
Given string str and index i, str.charAt(i) returns the character at that index. Like arrays, string indexes start at zero.
Given string str and character ch, str += ch adds the ch to the end of str.
You will need to create four classes and one interface for this assignment:
Cipher - An interface representing a generic cipher.
SubstitutionCipher - An abstract class representing a substitution cipher.
ShiftCipher - A class representing a shift cipher.
VigenereCipher - A class representing a Vigenre cipher.
YournameProgram2 (with your last name) - A class containing a main function that provides an interface for the user to encrypt plaintexts and decrypt ciphertexts using a shift cipher or a Vigenre cipher.
The Cipher interface should contain the headings for two public methods.
encrypt - takes a plaintext string and returns the corresponding ciphertext string.
decrypt - takes a ciphertext string and returns the corresponding plaintext string.
Remember that these functions are not defined here, but rather in any class that implements this interface.
The SubstitutionCipher class implements the Cipher interface and adds some additional functionality of its own. It is an abstract class, because it contains headings for the following public methods:
encryptChar - takes a plaintext character and returns an encrypted ciphertext character.
decryptChar - takes a ciphertext character and returns an encrypted plaintext character.
reset - takes no arguments, resets any state maintained by the cipher (more on this later), and returns nothing.
In addition, SubstitutionCipher provides three useful public static methods:
alphabetic - takes a character and returns true if it is either an uppercase lettter (between A and Z) or a lowercase letter (between a and z) and false otherwise.
getKeyNum - takes a character and returns the corresponding key number using the following algorithm:
If the character is an uppercase letter it subtracts A from the character and returns the result.
If the character is a lowercase letter it subtracts a from the character and returns the result.
If the character is not alphabetic it returns -1.
shiftChar - takes a character and an integer key and returns the result of shifting the character by the number of positions specified by the key using the following algorithm:
If the character is an uppercase letter, it performs the shift using the expression:
- 'A' + key) % 26 + 'A')
If the character is a lowercase letter, if performs the shift using the expression
- 'a' + key) % 26 + 'a')
If the character is not alphabetic it returns the character unchanged.
Finally, SubstitutionCipher implements the two methods from the Cipher interface as follows:
encrypt takes a plaintext string and generates the corresponding ciphertext using the following algorithm:
Call reset to reset any cipher state.
Create a ciphertext string, initially empty.
For each character in the plaintext, call encryptChar to encrypt it, and add the result to the end of the ciphertext string.
Return the resulting ciphertext string.
decrypt takes a ciphertext string and generates the corresponding plaintext using the following algorithm:
Call reset to reset any cipher state.
Create a plaintext string, initially empty.
For each character in the ciphertext, call decryptChar to decrypt it, and add the result to the end of the plaintext string.
Return the resulting ciphertext string.
The ShiftCipher class is derived from the SubstitutionCipher class. It adds the following members:
A private integer member variable that stores the key.
A constructor that takes an integer key value as an argument and initializes the key member variable.
It also implements the abstract methods from SubstitutionCipher:
reset - does nothing as the shift cipher has no state.
encryptChar - uses the inherited shiftChar method to shift the plaintext character using the key member variable.
decryptChar - uses the inherited shiftChar method to shift the ciphertext character using a value of 26 - the key member variable. This has the effect of shifting the character backwards while making sure the math works properly.
The VigenereCipher class is also derived from the SubstitutionCipher class. It adds the following members:
A private string member variable that stores the key.
A private integer member variable that keeps track of the index of the character in the keyword used to encrypt or decrypt the next plaintext or ciphertext character.
A constructor that takes a string keyword value, initializes the keyword member variable to this value, and sets the keyword position member variable to zero.
It also implements the abstract methods from SubstitutionCipher:
reset - sets the keyword index member variable to zero.
encryptChar - encrypts the given plaintext character using the following algorithm:
Get the keyword character at the current index.
If the plaintext charachter is alphabetic, increment the keyword index.
If the resulting keyword index is equal to the length of the string, reset it to zero.
Use the inherited getKeyNum method to get the integer key that corresponds to the keyword character.
If getKeyNum returns a non-negative value, use the inherited shiftChar method to shift the plaintext character using the key value and return the result.
Otherwise return the original plaintext character.
decryptChar - decrypts the given ciphertext character using the following algorithm:
Get the keyword character at the current index.
If the cipherxtext charachter is alphabetic, increment the keyword index.
If the resulting keyword index is equal to the length of the string, reset it to zero.
Use the inherited getKeyNum method to get the integer key that corresponds to the keyword character.
If getKeyNum returns a non-negative value, use the inherited shiftChar method to shift the plaintext character using 26 minus the key value and return the result.
Otherwise return the original ciphertext character.
The main function in YournameProgram2.java should do the following:
Declare a variable of type Cipher along with any other variables you may need.
Display a welcome message.
Repeat the following until the user decides to exit the program:
Ask the user if they want to encrypt or decrypt a message.
Ask the user if they want to use a shift cipher or a Vigenre cipher.
If a shift cipher was chosen, ask the user to enter a key between 0 and 25, and initialize your Cipher variable using the ShiftCipher constructor.
If a Vigenre cipher was chosen, ask the user to enter a keyword, and initialize your Cipher variable using the Vigenere cipher constructor.
If the user chose to encrypt, prompt for the plaintext, generate the ciphertext, and display it to the user.
If the user chose to decrypt, prompt for the ciphertext, generate the plaintext, and display it to the user.
Ask the user if they want to continue with the program.
Display a message thanking the user for using the program.
Note that the plaintext and ciphertext can include spaces, so you will need to use the nextLine() method of the Scanner class to read an entire line. Since next() and nextInt() can leave a newline character on the keyboard buffer, an extra call to nextLine() is needed to clear the character before reading the next line:
Scanner keyboard = new Scanner(System.in); String word = keyboard.next(); // reads a single word keyboard.nextLine(); // removes the newline from the buffer String line = keyboard.nextLine(); // reads the next line
Example Input and Output
Welcome to Nicholas Coleman's cipher program! Do you want to [E]ncrypt or [D]ecrypt? E Do you want to use a [S]hift cipher or a [V]igenere cipher? S Please enter a number between 0 and 25 to use as a key: 3 Please enter the plaintext to be encrypted: Trust no one. The corresponding ciphertext is: Wuxvw qr rqh. Do you want to continue (Y/N)? Y Do you want to [E]ncrypt or [D]ecrypt? D Do you want to use a [S]hift cipher or a [V]igenere cipher? S Please enter a number between 0 and 25 to use as a key: 3 Please enter the ciphertext to be decrypted: Wuxvw qr rqh. The corresponding plaintext is: Trust no one. Do you want to continue (Y/N)? Y Do you want to [E]ncrypt or [D]ecrypt? E Do you want to use a [S]hift cipher or a [V]igenere cipher? V Please enter a keyword: Mulder Please enter the plaintext to be encrypted: The truth is out there. The corresponding ciphertext is: Fbp wvlfb tv slf nshvv. Do you want to continue (Y/N)? Y Do you want to [E]ncrypt or [D]ecrypt? D Do you want to use a [S]hift cipher or a [V]igenere cipher? V Please enter a keyword: Mulder Please enter the ciphertext to be decrypted: Fbp wvlfb tv slf nshvv. The corresponding plaintext is: The truth is out there. Do you want to continue (Y/N)? N Thank you for using Nicholas Coleman's cipher program.
Notes and Comments
Upload your Java source file to the dropbox named Programming Assignment 2. The name of the source file for the main class must be your last name followed by Program2 with the extension .java.
Make sure to include comments with your name, a description of what the program does, the course (CSCI 2010), and the assignment name (Programming Assignment 2) at the beginning of all of your source files. Also make sure to add comments at the beginning of each of the methods you write describing what they do.
Make sure you only hand in the source files for your assignment, not the class files, or any other files that NetBeans creates.
Your programs must compile without errors in order to be graded. Once your program compiles make sure to test it using multiple test cases to see if it meets the requirements of the assignment.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started