Assignment 16
Due Friday, June 12 at 11:59pm on Canvas

Important: To receive credit, you must only use concepts and methods taught in class to complete these assignments - or You Will Receive a 0

Pair Programming Required (or You Will Receive a 0)
  • Both partners fill in, sign, date, and submit the pair programming contract
  • Upload the document(s) along with your assignment code to Canvas.
  • Please make sure both your names are on your file (hint: use a comment).
  • If you need help finding a partner, please contact me as soon as possible.

To Begin: Watch this Video about Alan Turing - the "Grandfather of Computer Science"

Assignment 16: The Enigma Machine (10pts)

  • In this program, we will develop an algorithm for encrypting and decrypting text data. In other words, we are going to write messages in a secret code!
  • We learned a little about encryption and decryption from the above video on the life of Alan Turing.
  • Encryption is the process of encoding messages or information in such a way that only authorized parties can read it.
  • Decryption is the process of decoding encrypted data.
  • The text before encryption is called plaintext.
  • The term for encrypted data is ciphertext.
  • As an example, we may encode the plaintext message:
  • after which the ciphertext text will be:
  • Notice that the ciphertext looks nothing like the original. However, looking more closely, we can see the encryption is really the Unicode codes for the characters. We look up the characters in a Unicode table and see the numerical codes match the characters.
H  E  L  L  O     W  O  R  L  D  !
72 69 76 76 79 32 87 79 82 76 68 33
  • Remember that computers can only understand numbers. Therefore, Unicode is the numerical representation of a character such as 'a', 'A' or '!'.
  • For our program, we will use a simple encryption based on Unicode. The encryption technique is straightforward since Java saves characters as numbers. The data type determines how they appear when displayed with a print statement:
char letter = 'A';
int unicode = letter; //save char as an int
System.out.println(unicode); //prints 65 - the Unicode value
  • To encode, we first separate the two digits of the Unicode code as integers.
//We want to separate 65 into two digits: 6 and 5
int firstDigit = 65 / 10; //Stores the integer 6 as firstDigit.
int secondDigit = 65 % 10; //Stores the integer 5 as secondDigit. Why does this work???
  • Then we convert the integer values to Unicode numerical values by adding 48 and saving as character values.
char uniDigit1 = (char) (firstDigit + 48);
char uniDigit2 = (char) (secondDigit + 48);
  •  Finally, we concatenate the two characters to the end of our encryption string.
String encryption += uniDigit1;
encryption += uniDigit2;

The encryption algorithm is:

For each character in a String:
1. Extract each character from a String as a char.
2. Convert the character to its Unicode value.
3. Unicode divided by 10 to get the first encoded character as an int.
4. Unicode modulus 10 to get the second encoded character as an int.
5. Convert each of the two integers to its Unicode character code.
6. Concatenate both of the characters to the end of a String.
The final String (after concatenation) contains the encrypted message.

Working through an example:

char letter = 'A' from the input String
'A' -> 65
65 / 10 -> 6
65 % 10 -> 5
6 + 48 -> 54 -> '6'
5 + 48 -> 53 -> '5'
encryptionString = encryptionString + uniDigit1 + uniDigit2

To Decrypt our Secret Coded Message, We Need to Reverse the Above Steps! (Why?)

The decryption algorithm is:

For each pair of encrypted characters:
1. Convert the first and second characters of the pair to its Unicode value.
2. Convert the first and second characters to an integer value.
2. Decrypted Unicode code = firstCharacter * 10 + secondCharacter.
3. Convert the Unicode to a character.
4. Append the character to a String.
The final String (after concatenation) contains the decrypted message.

Working through an example:

char1 = '6' from the input string
char2 = '5' from the input string
'6' - 48 -> 6
'5' - 48 -> 5
letter = 6 * 10 + 5;
OutputString = OutputString + letter

Notice that with our two-digit conversion technique, Unicode characters with codes higher than 99 ('c') will no longer appear as numbers. For example:

H  e  l  l  o     W  o  r  l  d  !
72 :1 :8 :8 ;1 32 87 ;1 ;4 :8 :0 33
  • Write a program that encrypts and decrypts text into its Unicode equivalent.

    For example, the letter "A" would encrypt into "65" and "123" encrypts into "495051". All encrypted letters and digits become two character values that are stored as Strings. Decryption returns the Unicode encoded String to its original letters and digits. When letters go beyond lowercase 'c', the ciphertext is no longer purely numbers but includes leading characters such as ':'.

  • Open up Eclipse and create a new Java file called
  • Within your program, define the two functions, one for encryption and one for decryption as follows:
method name: encrypt:
  • Converts a String of uppercase letters and digits into their Unicode equivalent. Lowercase letters may have non-digit characters.
  • Parameter(s): One String parameter for the plaintext to encrypt
  • Prints a ciphertext String of Unicode encoded letters and digits with two characters of output for each character of input.
  • Returns nothing

method name: decrypt:
  • Converts a Unicode encrypted String back into letters and digits
  • Parameter(s): One String parameter for the ciphertext to decrypt.
  • Prints a plaintext String of letters and digits equivalent to the original un-encrypted String.
  • Returns nothing
  • Both methods should have a full Javadoc comment (-2 for missing or incorrect Javadoc comments)
  • Your program must work identically to the sample output below:
*** Unicode Encryption ***

Enter "e" to encrypt, "d" to decrypt or "x" to exit (e/d/x): z
Unrecognized command: z
Enter "e" to encrypt, "d" to decrypt or "x" to exit (e/d/x): e
Enter the String to encrypt: abcABC123
ciphertext: 979899656667495051

Enter "e" to encrypt, "d" to decrypt or "x" to exit (e/d/x): d
Enter the String to decrypt: 979899656667495051
plaintext: abcABC123

Enter "e" to encrypt, "d" to decrypt or "x" to exit (e/d/x): e
Enter the String to encrypt: Hello World!
ciphertext: 72:1:8:8;13287;1;4:8:033

Enter "e" to encrypt, "d" to decrypt or "x" to exit (e/d/x): d
Enter the String to decrypt: 72:1:8:8;13287;1;4:8:033
plaintext: Hello World!

Enter "e" to encrypt, "d" to decrypt or "x" to exit (e/d/x): x