Objective
Encrypt and decrypt player chat messages in JavaCraft to ensure secure communication.
Introduction
In this lab, you will add cryptography to the JavaCraft chat system. You will implement a cipher in Java, integrate it into the chat logic, and test it by sending encrypted messages between players.
All chat messages in JavaCraft must follow the format:
@[name]: [text]
Your task is to ensure that this format is preserved while also securing messages so they cannot be easily intercepted or read by unintended recipients.
Instructions
1. Log into the Raspberry Pi
- Connect your computer to the same Wi-Fi network as the Raspberry Pi.
- Open a terminal and log in with SSH:
ssh piNNNN@piNNNN
Replace NNNN with your group’s assigned number.
3. Enter the password you set in Week 1.
2. Develop a Cipher in Java
-
Choose a cipher algorithm (see Appendix for options).
-
Modify your JavaCraft code so that it can:
- Encrypt a given message
- Decrypt an encrypted message
-
Test your cipher with simple strings before integrating it into JavaCraft.
3. Integrate Cipher into JavaCraft Chat
-
Open the JavaCraft source file
-
Modify the code so that:
- All outgoing messages are encrypted before being sent.
- All incoming messages are decrypted before being displayed.
- Messages that fail to decrypt are shown as-is with a warning.
-
Ensure the
@[name]: [text]format is validated after decryption using your DFA/NFA.
4. Sending Encrypted Messages
When sending an encrypted message, use the following format:
cipherNumber,key(optional),encryptedMessage
Examples:
- Caesar cipher (shift = 3):
1,3,KHOOR
- Vigenère cipher (key = KEY):
5,KEY,RIJVS
- Atbash cipher (no key):
2,SVOOL
5. Test Your Encrypted Communication
-
Recompile and run the modified chat system:
-
Connect at least two players on the same network.
-
Exchange at least ten valid encrypted messages.
- Each message must decrypt correctly back into the format
@[name]: [text].
- Each message must decrypt correctly back into the format
-
Send at least two invalid messages (wrong format or bad decryption) and confirm they are rejected.
6. Validate Security
- Confirm that intercepted messages appear as encrypted text.
- Verify that only players with the correct cipher and key can decrypt them.
- Ensure your DFA/NFA validator checks the message format after decryption.
7. Shut Down the Raspberry Pi
When finished, shut down safely:
sudo shutdown -h now
Wait until the device powers down before unplugging it.
Lab Submission
- Show the TA your working chat with integrated encryption.
- Submit:
- Modified JavaCraft code
- A log of at least 10 valid encrypted messages and 2 invalid messages
Summary
By completing this lab, you have:
- Implemented a cipher in Java
- Integrated encryption and decryption into the JavaCraft chat
- Preserved the
@[name]: [text]format through DFA/NFA validation - Tested secure communication between multiple players
Appendix: Cipher Help
Below is a list of ciphers categorized by difficulty, along with examples and references for further reading.
Easy Ciphers
-
Caesar Cipher (Cipher #1)
-
Difficulty: Easy
-
Explanation: Shifts each letter in the plaintext by a fixed number of positions down the alphabet.
-
Example:
- Plaintext:
HELLO - Shift:
3 - Ciphertext:
KHOOR
- Plaintext:
-
Reference: Caesar Cipher
-
-
Atbash Cipher (Cipher #2)
-
Difficulty: Easy
-
Explanation: Substitutes each letter with its opposite in the alphabet (e.g.,
A↔Z). -
Example:
- Plaintext:
HELLO - Ciphertext:
SVOOL
- Plaintext:
-
Reference: Atbash Cipher
-
-
ROT13 Cipher (Cipher #3)
-
Difficulty: Easy
-
Explanation: A Caesar cipher with a fixed shift of 13; applying it twice returns the original text.
-
Example:
- Plaintext:
HELLO - Ciphertext:
URYYB - Double ROT13:
URYYB→HELLO
- Plaintext:
-
Reference: ROT13 Cipher - CyberChef
-
-
Rail Fence Cipher (Cipher #4)
-
Difficulty: Easy
-
Explanation: Arranges text in a zigzag pattern on multiple "rails" and reads it line by line.
-
Example:
- Plaintext:
HELLO WORLD - Rails:
3 - Ciphertext:
HOLELWRDLO
- Plaintext:
-
Reference: Rail Fence Cipher - Crypto Corner
-
Medium Ciphers
-
Vigenère Cipher (Cipher #5)
-
Difficulty: Medium
-
Explanation: Uses a keyword to determine the shift for each letter.
-
Example:
- Plaintext:
HELLO - Keyword:
KEY - Ciphertext:
RIJVS
- Plaintext:
-
Reference: Vigenère Cipher
-
-
Playfair Cipher (Cipher #6)
-
Difficulty: Medium
-
Explanation: Encrypts pairs of letters using a 5x5 grid derived from a keyword.
-
Example:
- Plaintext:
HELLO - Keyword:
MONARCHY - Ciphertext:
CFSUPM
- Plaintext:
-
Reference: Playfair Cipher
-
-
Columnar Transposition Cipher (Cipher #7)
-
Difficulty: Medium
-
Explanation: Writes the plaintext in rows and reads it off column by column based on a keyword.
-
Example:
- Plaintext:
HELLOWORLD - Keyword:
KEY - Ciphertext:
EORHLODLWL
- Plaintext:
-
Reference: Columnar Transposition
-
-
Autokey Cipher (Cipher #8)
-
Difficulty: Medium
-
Explanation: A variant of the Vigenère cipher where the key is the keyword followed by the plaintext itself.
-
Example:
- Plaintext:
HELLO - Keyword:
KEY - Ciphertext:
RIJSS
- Plaintext:
-
Reference: Autokey Cipher
-
Difficult Ciphers
-
Beaufort Cipher (Cipher #9)
-
Difficulty: Difficult
-
Explanation: Similar to the Vigenère cipher but uses a different encryption algorithm involving subtraction.
-
Example:
- Plaintext:
HELLO - Keyword:
KEY - Ciphertext:
DANZQ
- Plaintext:
-
Reference: Beaufort Cipher
-
-
Bifid Cipher (Cipher #10)
-
Difficulty: Difficult
-
Explanation: Combines substitution and transposition using coordinates in a 5x5 grid.
-
Example:
- Plaintext:
HELLO - Keyword:
KEYWORD - Ciphertext:
fhycz
- Plaintext:
-
Reference: Bifid Cipher
-
-
Affine Cipher (Cipher #11)
-
Difficulty: Difficult
-
Explanation: Applies a mathematical function to each letter's numeric equivalent.
-
Example:
- Plaintext:
HELLO - Function:
5x + 8 mod 26 - Ciphertext:
RCLLA
- Plaintext:
-
Reference: Affine Cipher
-
Detailed Explanations
Caesar Cipher (Cipher #1)
-
Difficulty: Easy
-
Explanation: Each letter in the plaintext is shifted by a fixed number of positions down the alphabet.
-
Example:
-
Plaintext:
HELLO -
Shift:
3 -
Encryption Steps:
- Convert each letter to its numerical equivalent (A=0, B=1, ..., Z=25).
- Apply the shift:
(Letter Index + Shift) mod 26. - Convert the result back to a letter.
-
Calculation:
H(7) →(7 + 3) mod 26= 10 →KE(4) →(4 + 3) mod 26= 7 →HL(11) →(11 + 3) mod 26= 14 →OL(11) →(11 + 3) mod 26= 14 →OO(14) →(14 + 3) mod 26= 17 →R
-
Ciphertext:
KHOOR
-
-
Reference: Caesar Cipher - Khan Academy
Atbash Cipher (Cipher #2)
-
Difficulty: Easy
-
Explanation: Each letter is replaced by its counterpart from the opposite end of the alphabet.
-
Corrected Example:
-
Plaintext:
HELLO -
Encryption Steps:
- Create a mapping where
A ↔ Z,B ↔ Y, ...,M ↔ N. - Substitute each letter in the plaintext with its counterpart.
- Create a mapping where
-
Calculation:
H→SE→VL→OL→OO→L
-
Ciphertext:
SVOOL
-
- Reference: Atbash Cipher
ROT13 Cipher (Cipher #3)
-
Difficulty: Easy
-
Explanation: A special case of the Caesar cipher with a shift of 13.
-
Corrected Example:
-
Plaintext:
HELLO -
Encryption Steps:
- Shift each letter by 13 positions.
- Apply modulo 26 to wrap around the alphabet.
-
Calculation:
H(7) →(7 + 13) mod 26= 20 →UE(4) →(4 + 13) mod 26= 17 →RL(11) →(11 + 13) mod 26= 24 →YL(11) →(11 + 13) mod 26= 24 →YO(14) →(14 + 13) mod 26= 1 →B
-
Ciphertext:
URYYB
-
-
Reference: ROT13 Cipher
Rail Fence Cipher (Cipher #4)
-
Difficulty: Easy
-
Explanation: Arranges the plaintext in a zigzag pattern across multiple rails and reads it off row by row.
-
Corrected Example:
-
Plaintext:
HELLOWORLD -
Rails:
3 -
Encryption Steps:
- Write the message in a zigzag pattern across 3 lines.
- Read off each line to get the ciphertext.
-
Pattern:
H O L E L W R D L O -
Ciphertext:
HOLELWRDLO
-
-
Reference: Rail Fence Cipher - GeeksforGeeks