Cryptography is the science of replacing informationsuch as text with a different set of text, such that the
original text can no longer be easily determined.
Numerous algorithms exist for this, and you’ll get to
implement a few in this assignment. In the process,
you’ll practice interfaces and inheritance along with
more class implementations in Java.
Write a Crypto application based on the following
class diagram that can encrypt or decrypt text using
substitution cypher either with a provided key or
generating a random key, or using the Rot13 cypher.
«Interface»
Cypher
+encrypt(unencrypted : String): String
+decrypt(encrypted : String): String
Substitution
Crypto
+main/args : String[]).
#encryptKey : char[26]
#decryptKey : char[26]
+Substitution(key: String)
+encrypt(unencrypted : String): String
+decrypt(encrypted : String): String
Rot13
+Rot130
ricegf@antares:-/dev/202201/P04/full_credit$ java Crypto rot13
(e)ncrypt, (d)ecrypt, or (q)uit? e
Enter text to encrypt
What’s the best thing about Switzerland?
Jung’f gur orfg guvat nobhg Fjvgmreynaq?
I don’t know, but the flag is a big plus.
Vqba’g xabj, ohg gur synt vf n ovt cyhf.
(e)ncrypt, (d)ecrypt, or (q)uit? di
Enter text to decrypt
Jung’f gur orfg guvat nobhg Fjvgmreynaq?
What’s the best thing about Switzerland?
V qba’g xabj, ohg gur synt vf n ovt cyhf.
I don’t know, but the flag is a big plus.
(e)ncrypt, (d)ecrypt, or (q)uit? q.
ricegf@antares:-/dev/202201/P04/full_credit$ I
Assignment Background
Cryptography is the science of replacing information such as text with a different set of text, such that the
original text can no longer be easily determined. Numerous algorithms exist for this, and you’ll get to
implement a few in this assignment. In the process, you’ll practice interfaces and inheritance along with more
class implementations in Java.
Full Credit
In your git-managed cse1325 directory cse1325/P04/full_credit, with reference to the UML diagram below,
create an interface (recommended) or abstract class (your choice) called Cypher for implementing
cryptographic algorithms. From Cypher, implement (if an interface) or extend (if an abstract class) a class
called Substitution that implements a simple substitution cypher. Finally, derive from Substitution a third class
called Rot13 that implements the ultra-simple but often useful Rot13 encryption algorithm.
«Interface
Cypher
+encryptiunencrypted : String): String
+ decrypt/encrypted : String): String
Crypto
+mainlargs : Strinelli
Substitution
#encryptKey: char[26]
#decryptKey: char[26]
+Substitutionikey: String)
+encryptiunencrypted : String): String
+decryptiencrypted : String): String
Rot13
+Rot130
Include a working build.xml file so that the ant command will build your code (this was provided to you in
lecture). Add, commit, and push all files to your private cse1325 GitHub repository.
Crypto
The Crypto class includes the user interface, which you may write as you please as long as you include these
options.
Allow the user on startup (via command line options or interactively) to either provide a specific substition key
or generate a substitution key randomly for the Substitution class, or use the Rot13 class.
1. Encrypt – Accept lines of plaintext and print the encrypted version of each line until a zero-length line is
entered
2. Decrypt – Accept lines of encrypted text and print the plaintext version of each line until a zero-length line
is entered.
Quit – Exit the program.
The suggested solution uses a command line interface.
ricegf@antares:~/dev/202201/P04/full_credit$ java Crypto -h
usage: java Crypto [key | rot13]
ricegf@antares:-/dev/202201/P04/full_credit$ I
ricegf@antares:-/dev/202201/P04/full_credit$ java Crypto zebrascdfghijklmnopqtuvwxy
(e)ncrypt, (d)ecrypt, or (q)uit? e
Enter text to encrypt
Hello, World!
Daiil, Vloir!
The quick brown fox jumps over the lazy dog
Qda ntfbh eolvk stw gtjmp luao da izyx ric
(e)ncrypt, (d)ecrypt, or (q)uit? d
Enter text to decrypt
Daiil, Vloir!
Hello World!
Qda ntfbh eolvk slw gtimp luao qda izyx rlc
The quick brown fox jumps over the lazy dog
(e)ncrypt, (d)ecrypt, or (q)uit? q
ricegf@antares:-/dev/202201/P04/full_credit$ I
ricegf@antares:~/dev/202201/P04/full_credit$ java Crypto
Key = rqsnxzywvhejbimalucdotfkpg
(e)ncrypt, (d)ecrypt, or (q)uit? e
Enter text to encrypt
Thanks for the key!
Dwriec zmu dwx exp!
(e)ncrypt, (d)ecrypt, or (q)uit? di
Enter text to decrypt
Dwriec zmu dwx exp!
Thanks for the key!
(e)ncrypt, (d)ecrypt, or (q)uit? q.
ricegf@antares:-/dev/202201/P04/full_credit$
Hints
Cypher
The Cypher interface (covered in Lecture 08) or abstract class (covered in Lecture 07) is very short and
consists of what is shown in the class diagram, since the methods have no implementations. In the suggested
solution, it is 3 lines of code.
Substitution
The Substitution class implements Cypher, and is by far the bulk of the effort this week.
The constructor accepts a key that consists of all 26 letters of the English alphabet, suitably shuffled. Data
validation rules should ensure that the key (1) has 26 characters and (2) includes each letter exactly once.
Store the key as a char array in field encryptKey, and its inverse in field decryptKey (see below).
Method encrypt accepts an unencrypted String and returns the encrypted String. Encryption substitutes the
plaintext letter of the alphabet with the letter with the same index in encryptKey.
You may read the Wikipedia “Substitution cipher” article for a more detailed explanation.
Plaintext ABCDEFGHIJKLMNOPQRSTUVWXYZ
encryptKey ZEBRASCDFGHIJKLMNOPQTUVWXY
To encrypt “HELLO WORLD”, find ‘H’ in Plaintext and write down the letter directly below it, ‘D’. Then find ‘E’
and write down the corresponding letter ‘A’. ‘L’ becomes ‘T’ (twice), ‘O’ becomes ‘L’, ‘W’ becomes ‘V’, ‘R’
becomes ‘O’, and ‘D’ becomes ‘R’ So “HELLO WORLD” becomes “DAIIL VLOIR”.
The letter lookup for char c in the encrypt method is simply encryptKey(c-‘A’).
To find the inverse of encryptKey, look at the above table. If you sort encryptKey along with Plaintext,
encryptKey becomes Plaintext and Plaintext becomes decryptKey.
decryptKey ECGHBIJKLMNOPQRSTDFUVWXYZA
Plaintext ABCDEFGHIJKLMNOPQRSTUVWXYZ
We can thus decrypt “DAIIL VLOIR” by finding each letter in the lower Plaintext and writing down the
corresponding letter in decryptKey. So ‘D’ becomes ‘H’, ‘A’ becomes ‘E’, T becomes ‘L’, and so on until
“HELLO WORLD” is revealed.
The letter lookup for char c in the decrypt method is simply decryptKey(c-‘A’).
You may want to practice the Don’t Repeat Yourself (DRY) principle by adding a protected substitute method
called by both the encrypt and decrypt methods, but this is NOT required and you won’t be penalized if you
don’t.
You must handle both upper and lower case letters. Non-alpha letters should be unchanged during encryption
and decryption.
Rot13
Rot13 stands for “Rotate 13” – it simply adds 13 to the ASCII value of each character in a string, subtracting 26
if the resulting character is greater than ‘z’.
So “The quick brown fox jumps over the lazy dog” becomes “Gur dhvpx oebja sbk whzcf bire gur ynml qbt”.
Newsgroup readers implemented Rot13 because of one unusual feature of Rot13: Encrypt again, and you get
the original string. So encrypting “Gur dhvpx oebja sbk whzcf bire gur ynml qbt” with Rot13 a second time
restores it to “The quick brown fox jumps over the lazy dog”. This made Rot13 particularly useful for hiding
spoilers, punch lines, and puzzle solutions.
A little thought should lead you to conclude that the following encryptKey in Substitution implements the Rot13
algorithm. The Rot13 constructor therefore has no key parameter; it delegates to its superclass Substitution
with the key below, and then relies on inheritance for the rest of its functionality.
The suggested solution for Rot13 is 3 lines of code.
Plaintext ABCDEFGHIJKLMNOPQRSTUVWXYZ
encryptKey NOPQRSTUVWXYZABCDEFGHIJKLM
Crypto
The Crypto class includes the user interface, which you may write as you please as long as you include these
options.
Allow the user on startup (via command line options or interactively) to either provide a specific substition key
or generate a substitution key randomly for the Substitution class, or use the Rot13 class.
1. Encrypt – Accept lines of plaintext and print the encrypted version of each line until a zero-length line is
entered.
1010100101010
Throwing a RuntimeException C01001
(
import java.util.Scanner;
public class Rot13 {
static final String key = “nopqrstuvwxyzabcdefghijklm”;
public String encode(String s) {
String result = id;
Oh, look, it’s the mathematical approach
for(charc s. tocharArray()) {
if (c ==”){result += c; continue;} NOT a valid solution to P04, which is all
to the Rot13 cypher! (Warning: This is
if(‘a’