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 0Pair Programming Required (or You Will Receive a 0)
To Begin: Watch this Video about Alan Turing - the "Grandfather of Computer Science" Assignment 16: The Enigma Machine (10pts)
HELLO WORLD!
726976767932877982766833
H E L L O W O R L D !
72 69 76 76 79 32 87 79 82 76 68 33
char letter = 'A';
System.out.println(letter);;
int unicode = letter; //save char as an int
System.out.println(unicode); //prints 65 - the Unicode value
//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???
char uniDigit1 = (char) (firstDigit + 48); char uniDigit2 = (char) (secondDigit + 48);
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
method name: decrypt:
*** 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
Goodbye.
|