Skip to content

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:

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)
Output is 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)
Output is 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)
Output is Decrypted Message: Madison


Want More Practices?

Try these challenges to test your skills!

  1. ๐Ÿ”„ Try Different Shifts Change the shift number to 5, 10, or even 25. What happens if you shift by 26?

  2. ๐Ÿ†š 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!

  3. ๐Ÿ”ก 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.
  4. ๐Ÿ” Wrap-Around Logic What happens if you encrypt the letter 'Z' with a shift of 1? Does it wrap around to 'A'?

  5. ๐Ÿ•ต๏ธโ€โ™€๏ธ Code Cracker Mode Try to guess the shift used to encrypt a message. Can you write code that tries all 26 possibilities?

  6. ๐Ÿง  Bonus Challenge Write a function that asks the user for input (with input()) and prints the encrypted version.