Encoding is not encryption
一个常见的混淆
- 人们常常把“编码(encoded)”和“加密(encrypted)”混为一谈。它们并不相同。
- 编码改变数据的格式,以便存储或传输 —— 它不保密。
- 加密改变数据,使得只有持有密钥的人才能读懂 —— 它是保密的。
Base64 —— 一种著名的编码
- Base64 用 64 个安全字符来重写字节。它常用来把图片或文件塞进文本里(比如电子邮件)。
import base64
secret = base64.b64encode(b"hello").decode()
print(secret) # aGVsbG8=
print(base64.b64decode(secret).decode()) # back to hello
- 任何人都能解码它 —— 不需要密钥。所以 base64 什么也藏不住。
这为什么和安全有关
- 如果一个网站用 base64 来“保护”你的密码,那它根本没有被保护。
- 真正的保护需要加密(配密钥)或哈希(单向)。编码只是一件外衣。
试一试
- 下面把
hello编码成 base64。然后想象一下解码它 —— 记住它并不是秘密。
涵盖:IGCSE 2.3 / A-Level 17.1(加密)的基础。
常见错误
- Base64 是编码——任何人都能还原,所以毫无安全性。
- 编码改变的是格式;加密保护的是内容。
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Use base64 to encode the word hello. Encode its bytes and print the result as text. (Notice: anyone can decode it — encoding is 不 secret.)
点击“运行”查看此处输出。
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
An app 'protected' a message as base64 — prove that protects nothing. Decode captured with base64.b64decode and print the text. No key needed.
点击“运行”查看此处输出。
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
A site should store a 散列值 of your password, never the password or its base64. Write check_login(attempt, stored), which returns True when the SHA-256 hash of attempt (as hex text) equals stored. Use hashlib.sha256(attempt.encode()).hexdigest(). (This check leaves out the salt and slow hash of lesson 07 to stay short.)
点击“运行”查看此处输出。