Mã hóa đối xứng
Locking with a key
- Encryption scrambles a message so only someone with the key can read it.
- In symmetric encryption, the same key locks and unlocks. Sender and receiver must both know it.
The Caesar cipher
- The oldest example: shift every letter forward by a fixed number. With shift 3,
a → d,b → e, and so on. - The key is the shift. Decrypting just shifts back.
text = "attack"
shift = 3
out = "".join(chr((ord(c) - 97 + shift) % 26 + 97) for c in text)
print(out) # dwwdfn
Why Caesar is weak
- There are only 25 possible shifts, so a computer can try them all in a blink.
- This is a brute-force attack on the key — exactly what real encryption must resist.
What real ciphers do
- Modern symmetric ciphers like AES use 128-bit or longer keys: 2¹²⁸ is about 3 × 10³⁸ possible keys, far too many to try one by one.
- The big problem they leave: how do two people share the secret key safely? The next lesson solves it.
Covers: IGCSE 2.3 (symmetric encryption), A-Level 17.1.
Common mistakes
- One shared key both encrypts and decrypts.
- The hard part is sharing that key safely.
Mã hóa đối xứng
Cipher Caesar sử dụng một khóa chung để mã hóa và giải mã.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Mã hóa hello bằng mã Caesar: dịch chuyển mỗi chữ cái tiến lên shift vị trí (vòng qua z quay trở lại a). Xây dựng chuỗi đã mã hóa và in ra.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Giờ hãy phá vỡ nó — đó là lý do Caesar yếu. Bạn đã chặn được cipher, và bạn nghi ngờ văn bản gốc là attack. Thử mọi bước dịch chuyển từ 1 đến 25, dịch chuyển lùi, và in bước dịch chuyển biến cipher thành attack.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Người nhận biết khóa, nên họ có thể đảo ngược phép dịch chuyển. Viết decrypt(text, shift), di chuyển mỗi chữ cái thường lùi lại shift (vòng từ a về z) và giữ nguyên khoảng trắng cùng các ký tự khác.
Nhấn Chạy để xem kết quả ở đây.