Day 2: ๐ Introduction to CyberSecurity¶
Part 1:¶
Background¶
Weโve discovered that an attacker gained access to a Programmable Logic Controller (PLC) by exploiting its password. Originally, the PLC password was set to โcoolโ by the engineer. However, the attacker changed it and reprogrammed the PLC. As a result, the conveyor belt in the chocolate factory stopped working correctly. It could no longer sort chocolates with nuts from those without which a serious problem for customers with nut allergies. If not fixed, the company could be sued!
In this activity, youโll act as a cyber detective. Youโll examine the communication between the attacker and the PLC and uncover the new password the attacker set.
Activity Materials:¶
- Download the Zebra APP and install it.
- Download the lecture handout and open it.
- Download the Deer 1 APP and open it.
Part 2: Caesar Cipher in Python (For Beginners)¶
๐๏ธ What Is the Caesar Cipher?¶
The Caesar Cipher is one of the oldest and simplest ways to keep a message secret. It was used over 2,000 years ago by Julius Caesar, a Roman general, to send secret messages to his army.
๐ก Why Learn This?¶
- Itโs fun!
- It teaches how computers hide information.
- Youโll write your first Python program.
How it works:¶
- Pick a number (called a shift).
- Replace every letter in your message with the letter that comes that many steps later in the alphabet.
- If you go past Z, wrap around to the beginning.
๐ฆ Example with a shift of 3: - A โ D - B โ E - C โ F - … - Z โ C
So, the message HELLO becomes KHOOR.
๐งช Example¶
Original Message: HELLO WORLD
With a shift of 3: KHOOR ZRUOG
๐ง Python Basics: ord() and chr()¶
๐ค Background: How Letters Become Numbers¶
Computers donโt understand letters the way we do โ they understand numbers.
Each character you type โ like A, B, or even ! โ has a special number called a character code. The most common system is called ASCII (American Standard Code for Information Interchange).
Here are some examples:
| Character | ASCII Code |
|---|---|
| A | 65 |
| B | 66 |
| C | 67 |
| … | … |
| Z | 90 |
| a | 97 |
| b | 98 |
| c | 99 |
| … | … |
| z | 122 |
To convert letters into numbers and back in Python:
-
Use
ord(letter)โ gives you the number Example:ord('A')โ65 -
Use
chr(number)โ gives you the letter Example:chr(65)โ'A'
Thatโs why in Caesar Cipher, we can shift letters by turning them into numbers, changing the number, then turning them back into letters!
To shift letters, we need a way to turn them into numbers and back.
ord('A')gives the ASCII number for a character.- Example:
ord('A')โ65 chr(65)gives the character for a number.- Example:
chr(65)โ'A'
We use this trick to move letters forward or backward in the alphabet.
๐งฎ Example:¶
letter = 'B'
number = ord(letter) # 66
new_number = number + 3 # 69
new_letter = chr(new_number) # 'E'
print(new_letter)
E
๐ฉโ๐ป Step-by-Step Python Code¶
Don't worry if youโve never coded before โ follow along and copy this code into any Python editor or use an online tool like https://replit.com.
Step 1: Encrypt a Message¶
def encrypt_caesar(text, shift):
result = ""
for char in text:
shifted = ord(char) + shift
result += chr(shifted)
return result
Step 2: Try It Out¶
message = "Madison"
shift_amount = 3
encrypted = encrypt_caesar(message, shift_amount)
print("Encrypted Message:", encrypted)
Encrypted Message: Pdglvrq
๐ Decrypt the Message¶
To reverse the encryption (called decryption), just shift backwards:
def decrypt_caesar(text, shift):
return encrypt_caesar(text, -shift)
Try Decryption¶
decrypted = decrypt_caesar(encrypted, shift_amount)
print("Decrypted Message:", decrypted)
Decrypted Message: Madison
Want More Practices?¶
Try these challenges to test your skills!
-
๐ Try Different Shifts Change the shift number to 5, 10, or even 25. What happens if you shift by 26?
-
๐ If you move beyond
Z, you should loop back to the start of the alphabet. Thatโs why we need apply modulo 26, since there are 26 letters total. Try writing your own sentence and see how it looks when encrypted! -
๐ก Letter Only Modify the code so it processes only letters. What changes will you need to make?
- Hint: consider using
str.isalpha()to check if a character is a letter before encrypting it.
- Hint: consider using
-
๐ Wrap-Around Logic What happens if you encrypt the letter
'Z'with a shift of 1? Does it wrap around to'A'? -
๐ต๏ธโโ๏ธ Code Cracker Mode Try to guess the shift used to encrypt a message. Can you write code that tries all 26 possibilities?
-
๐ง Bonus Challenge Write a function that asks the user for input (with
input()) and prints the encrypted version.