Encoding and encryption · Cyber Security Basics
Is Base64 encryption? What actually keeps a message secret?
Course contentsWhy you need it
Real leaks happen because someone believed scrambled-looking text was protected. Encoding, encryption and hashing look alike on screen but do very different jobs. Mixing them up is one of the most common security mistakes in code reviews.
The intuition
- Encoding changes the alphabet so data can travel safely, like writing a message in Morse code. Anyone can turn it back: there's no key.
- Encryption is a lock. Only someone with the key can turn the scrambled text back into the original.
- Hashing is a fingerprint (lesson 3): you can't get the original back at all.
Two tools for this lesson:
ord("a")gives a character's number (97) andchr(97)turns the number back into"a".^is XOR: it flips bits. XOR with the same key twice gives the original back, which is why it appears inside real ciphers.
Definition
Encoding vs encryption
Encoding transforms data into another format using a public scheme and needs no key to reverse. Encryption transforms data so that it can only be reversed with a secret key.
Key
The secret value that controls an encryption: the same algorithm with a different key gives a different result.
Examples
Base64 hides nothing
Try it yourselfBase64 turns bytes into letters and digits so they fit in emails and URLs. Decoding needs no secret.
import base64
secret = "pin=4821"
encoded = base64.b64encode(secret.encode()).decode()
print(encoded)
print(base64.b64decode(encoded).decode())- L3
.encode()turns text into bytes; Base64 works on bytes. - L4Looks scrambled, but it's just another alphabet.
cGluPTQ4MjE= - L5Anyone can decode it. No key was involved.
pin=4821
A Caesar cipher has a key
Try it yourselfEach letter moves key places along the alphabet. Shifting back by the same key unlocks it.
def caesar(text, key):
out = ""
for ch in text:
if "a" <= ch <= "z":
out += chr((ord(ch) - ord("a") + key) % 26 + ord("a"))
else:
out += ch
return out
locked = caesar("attack at dawn", 3)
print(locked)
print(caesar(locked, -3))- L4Only lowercase letters move; spaces stay.
- L5Letter → 0 to 25, add the key,
% 26wraps z back to a, then back to a letter. - L10Lock the message with key 3.
- L11a→d, t→w, ...
dwwdfn dw gdzq - L12The same key in reverse unlocks it.
attack at dawn
Common mistakes
Treating Base64 as protection
API keys and passwords "hidden" in Base64 are readable by anyone who sees them. If something must stay secret, encrypt it or don't store it.
Inventing your own cipher
Caesar and simple XOR are great for learning and broken in practice: there are only 26 Caesar keys. Real systems use well-tested algorithms such as AES through a library, and TLS for data on the network.
Quick quiz
1. What does this print?
def caesar(text, key):
out = ""
for ch in text:
out += chr((ord(ch) - ord("a") + key) % 26 + ord("a"))
return out
print(caesar("xyz", 2))Show the answer
Answer: A
x→z, y→a (wraps around), z→b. The % 26 is what wraps.
2. Which of these can anyone turn back into the original, without a key?
Show the answer
Answer: A
Base64 is encoding: public and reversible. AES needs the key, and a hash can't be reversed at all.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictXOR twice
Type exactly what this program prints.
- ModifyCapital letters too
This Caesar cipher only shifts lowercase letters. Change it so capital letters shift too and stay capital:
caesar("Hello, World", 3)should return"Khoor, Zruog". - WriteBreak the cipher
Caesar has only 26 keys, so an attacker can simply try them all. Write
crack(locked, word)that tries every key from 0 to 25 and returns the key that was used to lock the text, given onewordyou know appears in the original message.The
caesarfunction is already there. Decrypting with keykmeanscaesar(locked, -k).
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Encoding | 编码 | Changing data into another format with a public, reversible scheme such as Base64. |
| Encryption | 加密 | Scrambling data so only someone with the key can read it. |
| Key | 密钥 | The secret value that controls encryption and decryption. |
| XOR | 异或 XOR | A bit operation; XOR with the same key twice returns the original value. |