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

Sources

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.

Knowledge check

  1. 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).
  2. 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).
  3. 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.

Related