Cybersecurity: encrypting and breaking a cipher
Encrypting means making a message unreadable without the key. The Caesar cipher shifts every letter by a fixed number: with a shift of 3, A becomes D. It's a good example for understanding… and for seeing why you should never invent your own encryption.
With only 26 possible keys, you can try them all. Even smarter: frequency analysis. In English, the most frequent letter is e, so the most frequent letter of the encrypted message probably stands for e, which reveals the shift. In real life we use proven algorithms like AES, through libraries such as cryptography.
def caesar(text, shift):
result = ""
for ch in text:
if ch.isalpha():
base = ord("A") if ch.isupper() else ord("a")
result += chr((ord(ch) - base + shift) % 26 + base)
else:
result += ch
return result
secret = caesar("Meet me at 6", 3)
print(secret)
print(caesar(secret, -3))