Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 40 ยท Getting Started

Encoding, Hashing and Encryption

Tell encoding, hashing and encryption apart. See why Base64 and hex are not secret, work with bytes in Python and learn which tool fits which job.

Beginners (and some production systems) mix up three ideas that look similar on the screen: all of them turn readable data into a string of strange characters. But they have completely different purposes. Encoding changes the format of data so it can be stored or sent, and anyone can reverse it. Hashing boils data down to a fixed-size fingerprint and cannot be reversed. Encryption hides data using a secret key, and only the key holder can reverse it.

Mistaking one for another causes real vulnerabilities. A password "protected" with Base64 is stored in plain sight. A hash used where you need to read the data back is useless. This lesson shows each tool in action, so you can tell them apart in code review and choose the right one.

Bytes, not text

Cryptographic functions work on bytes, not on Python strings. A string is a sequence of characters; a bytes object is a sequence of numbers from 0 to 255. You convert between them with an encoding such as UTF-8. If you need a refresher, read bytes and bytearray first.

Python Text to bytes and back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This is the first of the three transformations, and a very mild one: UTF-8 is an agreement about how characters map to bytes, not a secret. Keep in mind that when this tutorial says "encode", it always means a public, reversible format change.

Encoding: a format, not a secret

Encodings exist because some channels cannot carry arbitrary bytes. Email and URLs were designed for printable text, and JSON cannot hold raw binary. Base64 turns every 3 bytes into 4 printable characters. Hexadecimal turns every byte into 2 characters. Neither uses a key. There is nothing to guess.

Python Base64 and hex

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Anyone who sees cGFzc3dvcmQxMjM= can decode it in one line, with no key and no effort. Base64 makes data 33% larger and adds no protection at all. When you see an API that "encrypts" a token by Base64-encoding it, that is a bug.

flowchart LR
    A["Plain bytes"] -- "encode (no key)" --> B["Base64 / hex text"]
    B -- "decode (no key)" --> A

Encoding is a two-way street with no secret involved: anyone can walk both directions.

Hashing: a fingerprint

A hash function reads any amount of data and produces a fixed-size output called a digest. SHA-256 always produces 32 bytes, whether the input is one letter or one terabyte. The same input always gives the same digest, and a tiny change in the input changes the digest completely.

Python A one-way fingerprint

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

There is no key and no "unhash". The digest does not contain enough information to rebuild a long input, because many different inputs share the same fixed-size output space (hash functions compress). You can only check a guess: hash the guess and compare. That is exactly what makes hashing useful for verifying downloads and for storing passwords (with the extra care covered in password hashing). The details come in cryptographic hash functions.

Encryption: secrecy with a key

Encryption scrambles data using an algorithm and a key. The result, the ciphertext, looks random. With the right key you can decrypt it and recover the original exactly; without the key you cannot. It is the only one of the three that protects confidentiality.

Here is a toy cipher built from a hash function. It is not production-grade (real code should use AES-GCM or ChaCha20-Poly1305 from a vetted library), but it shows what the interface of encryption looks like: a key goes in, and the transformation is reversible only with that key.

Python A toy keyed cipher

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The ciphertext looks like noise and the wrong key does not reveal anything useful. Compare that with the Base64 example, where there was no key at all. The same function both encrypts and decrypts here because XOR undoes itself, a trick explored fully in XOR and the one-time pad.

Side by side

Encoding Hashing Encryption
Purpose Format conversion Fingerprint, verification Confidentiality
Reversible? Yes, by anyone No Yes, with the key
Uses a key? No No (HMAC adds one) Yes
Output size Grows with input Fixed About the input size
Examples Base64, hex, UTF-8, URL encoding SHA-256, SHA-3, BLAKE2 AES-GCM, ChaCha20
Classic mistake Treating it as security Treating it as reversible Reusing keys or nonces
flowchart TD
    Q["What do you need?"] --> A{"Anyone may read it,<br/>just needs a safe format?"}
    A -- "yes" --> E["Encoding"]
    A -- "no" --> B{"Need to read the<br/>data back later?"}
    B -- "no, only compare / verify" --> H["Hashing"]
    B -- "yes, but only with a secret" --> C["Encryption"]

A quick decision guide. Real designs often combine all three.

Using them together

Real systems layer these tools. A typical secure message is encrypted (confidentiality), the ciphertext is Base64-encoded so it fits in a JSON field (encoding), and a hash or tag lets the receiver verify nothing changed (integrity). The order matters, but notice the pattern: encoding is always the last, cosmetic step on the way out and the first step on the way in.

Python All three in one pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only the middle step needed a secret. The Base64 layers are packaging, and the keyed tag detects the single flipped bit. You will meet this "encrypt, then authenticate" pattern properly in authenticated encryption.

Warning: Never use a plain hash (or Base64) to "protect" a secret you need to keep. Short, guessable inputs such as passwords can be recovered by hashing guesses until one matches. Use a slow, salted password hash, covered later in the tutorial.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A developer stores API tokens as Base64 strings 'so that they are encrypted'. What is wrong?

    Base64 is a reversible format change that needs no secret. Anyone who sees the string can decode it immediately.
  2. 2. Which property is true of SHA-256 for every input?

    A hash function maps inputs of any size to a fixed-size digest (32 bytes for SHA-256), always producing the same digest for the same input.
  3. 3. You want to let a user recover their original data later, but only if they have a secret. Which tool do you need?

    Only encryption is both reversible and dependent on a secret key. Hashing is one-way and encodings need no key.
  4. 4. Why does Python's hashlib.sha256('text') raise a TypeError?

    Hashing operates on bytes. You choose an encoding such as UTF-8 explicitly with text.encode('utf-8'), so there is no hidden guess about the byte representation.

Summary

  • Encoding (Base64, hex, UTF-8) changes the format and is reversible by anyone; it provides no secrecy.
  • Hashing makes a fixed-size, one-way fingerprint; you can compare but not reverse.
  • Encryption uses a secret key to give confidentiality and is reversible only with that key.
  • Cryptographic APIs use bytes; convert text explicitly with .encode("utf-8").
  • Real protocols combine the three: encrypt, authenticate, then encode for transport.

Next: Classical Ciphers and Why They Break.