Encoding, hashing and encryption are different
Unit 1 of 5 in Cryptography basics.
You will be able to tell encoding, hashing and encryption apart and say which one fits a goal.
Lesson
Three tools that get mixed up
People say 'encrypted' for all of these, and mix them up. They answer three different questions.
Encoding changes the form of data so it fits somewhere, and anyone can undo it. Hashing turns data into a short fixed-size fingerprint that cannot be turned back. Encryption scrambles data with a key so only someone with the right key can read it.
Encoding: a different costume, no secret
Base64, from RFC 4648, represents any bytes using printable characters, for places restricted to US-ASCII text. The alphabet and procedure are public, and no key is involved. If you see a secret that is only Base64, you can read it.
Hashing: a fingerprint, one way
The Secure Hash Standard specifies hash algorithms that produce a message digest, a short value for a message. The digests are used to detect whether messages have been changed since the digests were generated. Comparing the digest of a file you received with the digest the publisher posted is a quick check that it is the file you expected, and the lab for this unit shows how one changed character changes the result.
OWASP describes hashing as a one-way function: it is impossible to 'decrypt' a hash and obtain the original value. That is exactly what you want for checking a password, and exactly what you do not want when you need the data back.
Encryption: a lock with a key
Encryption is two-way: with the key you get the original data back. AES, the Advanced Encryption Standard, uses keys of 128, 192 or 256 bits to encrypt and decrypt data in blocks of 128 bits. Block ciphers like AES are, in NIST's words, the foundation for many cryptographic services, especially those that provide confidentiality of data.
OWASP's rule of thumb for passwords is a good summary of the whole unit: passwords should be securely hashed, not encrypted or stored in plaintext, because a hash cannot be reversed even if an attacker obtains it, while encrypted data can be recovered by anyone who gets the key.
Key points
- Encoding is reversible by anyone; it hides nothing.
- A hash is a fixed-size fingerprint that cannot be reversed; use it to detect change and to check passwords.
- Encryption is reversible with the key; use it when you need the data back.
- AES encrypts in 128-bit blocks with 128, 192 or 256-bit keys.
Sources
- RFC 4648: The Base16, Base32, and Base64 Data Encodings
- NIST FIPS 180-4: Secure Hash Standard (SHS)
- NIST FIPS 197: Advanced Encryption Standard (AES)
- OWASP Password Storage Cheat Sheet
Practice in the labs
Do each of the three by hand: decode, fingerprint, and break a toy cipher so you see why real ones are different.
- Encoding is not encryption: decode Base64, hex, and URL encoding: You decode Base64, hex and URL encoding without a key, so 'encoded is not encrypted' sticks.
- Hash a file and verify a download: You fingerprint a file, change one character and watch the hash change completely.
- Break a classical cipher with frequency analysis (and learn why modern crypto is different): You break a toy cipher with counting alone, which shows why modern ciphers are built differently.
Knowledge check
- You need to check a user's password at login without ever being able to recover it. Which tool? Answer: Hashing. A hash is one-way, so even if the stored values leak they cannot be turned back into passwords. Encryption can be reversed by anyone with the key (OWASP).
- A token in a log is 'cGFzc3dvcmQ6aHVudGVyMg=='. How protected is it? Answer: Not protected: it looks like Base64 and anyone can decode it. Base64 is an encoding with a public alphabet and no key (RFC 4648).
- What are digests from the Secure Hash Standard used for? Answer: To detect whether messages have changed since the digests were generated. FIPS 180-4 says digests are used to detect whether messages have been changed since the digests were generated.
Back to the syllabus or see all classes.