Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 80 ยท Cryptography Basics

Cryptography in Plain Language

Learn the difference between encoding, encryption and hashing, how symmetric and public-key crypto differ, and why you must never roll your own cryptography.

Cryptography is the toolbox that makes the rest of security possible. It lets two parties talk privately across a hostile network, lets you check that a file has not been altered, and lets a website prove who it is. You do not need advanced mathematics to use it well. You need to know which tool does which job, and which mistakes beginners keep making.

The most common mistake is mixing up three different things: encoding, encryption and hashing. They look similar (unreadable-looking strings of characters) but they solve different problems, and confusing them leads to real breaches. This lesson separates them clearly, introduces symmetric and asymmetric encryption, and explains why the first rule of cryptography is "do not invent your own". It follows The Attack Lifecycle and MITRE ATT&CK.

Three things that look alike

Encoding Encryption Hashing
Purpose Represent data in another format Keep data confidential Fingerprint data, check integrity
Needs a secret key? No Yes No (keyed variants exist, see HMAC)
Reversible? Yes, by anyone Yes, with the key No (one-way)
Examples Base64, URL encoding, hex, UTF-8 AES, ChaCha20, RSA SHA-256, SHA-3, Argon2 (for passwords)
Security value None Confidentiality Integrity, password storage

Encoding exists so data survives a journey through systems that expect certain characters, such as putting binary into an email or a URL. There is no secret: anyone can undo it. Encryption scrambles data so that only someone with the right key can read it. Hashing compresses any input into a fixed-size fingerprint that cannot be turned back into the original.

flowchart LR
    P["Plain data"] -->|"encode"| E1["Encoded: same data, other format"]
    E1 -->|"decode: anyone can"| P
    P -->|"encrypt with key"| C["Ciphertext"]
    C -->|"decrypt with key"| P
    P -->|"hash"| H["Fixed-size fingerprint"]
    H -.->|"cannot go back"| P

Encoding and encryption are reversible (encryption only with the key). Hashing is a one-way street.

Base64 is not encryption

A string like c2VjcmV0LXBhc3N3b3JkLTEyMw== looks mysterious, but it is just Base64. It appears in HTTP Basic authentication headers, configuration files and tokens, and people regularly mistake it for protection. Here is how little protection it gives.

Python Base64 hides nothing

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 second string is the Basic authentication format user:password. Decoding it took no effort. Whenever you see credentials "protected" with Base64, treat them as plain text. The same applies to hex, URL encoding and ROT13.

Hashing is one-way, and very sensitive

A hash function turns any input into a fixed-length output. The same input always gives the same output, a tiny change gives a completely different result, and you cannot work backwards. That makes hashes ideal for fingerprints. You will go deep on them in Hash Functions and Integrity.

Python Same input, same hash; tiny change, different hash

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 first and third lines match (deterministic), and the others share nothing visible with them (sensitive to tiny changes). The length never changes, so a hash does not contain the message; it cannot be "decrypted".

Symmetric encryption: one shared key

In symmetric encryption the same secret key locks and unlocks the data. It is fast and used for the bulk of real-world encryption: disk encryption, database encryption, the data stream inside a secure web connection. Modern standards include AES and ChaCha20.

The weakness is the key distribution problem: how do two people who have never met agree on a secret key without an eavesdropper learning it? Also, with n people who all need private channels with each other, you need a different key for every pair, which becomes n(n-1)/2 keys.

Asymmetric (public-key) encryption: a key pair

Asymmetric cryptography gives each party a pair of mathematically linked keys: a public key that can be shared with the world and a private key that never leaves its owner. Depending on how you use the pair you get different services:

  • Encryption: anyone can encrypt a message with your public key, and only your private key can decrypt it.
  • Digital signatures: you sign with your private key; anyone can verify with your public key that the message came from you and was not altered.
  • Key exchange: two parties derive a shared secret over an open channel (for example Diffie-Hellman).

Public-key operations are slow compared with symmetric ones, so real systems combine them: use public-key methods to agree on a fresh symmetric key, then encrypt the data with that fast symmetric key. This hybrid pattern is exactly what TLS, the protocol behind HTTPS, does. Public-Key Cryptography and TLS, Certificates and PKI cover it in detail.

sequenceDiagram
    participant A as Alice
    participant B as Bob
    Note over B: Has a key pair<br/>(public and private)
    B->>A: Here is my public key
    A->>A: Create a random session key
    A->>B: Session key, encrypted with Bob's public key
    B->>B: Decrypt with the private key
    Note over A,B: Both now hold the same session key
    A->>B: Bulk data encrypted with the fast symmetric key

The hybrid pattern: slow public-key cryptography protects a small secret; fast symmetric cryptography protects the data.

Python How many keys do n people need?

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.

For 10,000 people, pairwise symmetric keys explode to almost 50 million, while public-key cryptography needs only one key pair each. That scalability, plus the ability to sign, is why public-key methods exist even though they are slower.

Never roll your own crypto

Cryptographers repeat one rule: do not design your own algorithms or protocols. Good algorithms are designed by experts, then attacked by thousands of researchers for years before anyone trusts them. A scheme you invent, however clever it seems, has had one reviewer: you.

To see why homemade schemes fail, here is the classic beginner idea, XOR-ing a message with a short repeating key. It looks scrambled, and falls to the simplest attack: if you know or can guess a few words of the plaintext, you recover the key.

Python Why a homemade repeating-key cipher fails

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.

Seven guessed characters exposed the key, and with it every message. The same weakness appears in homemade "obfuscation" of config values or licence keys. Attackers know these tricks well.

What to do instead:

  1. Use a well-reviewed library and its high-level, hard-to-misuse interfaces (for example libsodium-style APIs, or your platform's recommended crypto library).
  2. Use standard, current algorithms: AES-GCM or ChaCha20-Poly1305 for encryption, SHA-256 or SHA-3 for hashing, Argon2, scrypt or PBKDF2 for passwords, Ed25519 or ECDSA for signatures.
  3. Keep keys out of code and manage them properly (see Randomness, Key Management and Secrets).
  4. Prefer protocols like TLS over designing your own handshake.
  5. Remember that crypto protects data in transit and at rest; it does not fix a broken login page or an injection bug.

The strength of a well-designed system comes from the secrecy of the key only. Assume an attacker knows the algorithm (this is called Kerckhoffs's principle). If the system stays secure anyway, the design is sound.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A config file stores a password as Base64. How secure is it?

    Base64 only changes the representation of data. There is no key and no secret, so decoding is trivial.
  2. 2. Which tool is the right choice for storing a fingerprint of a downloaded file to check later that it has not changed?

    A hash gives a fixed-size fingerprint that changes completely if the file changes, which is exactly what an integrity check needs.
  3. 3. Why do real systems like TLS combine public-key and symmetric cryptography?

    Public-key cryptography solves the key-distribution problem but is slow. The hybrid approach uses it to establish a fresh symmetric key that then protects the data efficiently.
  4. 4. What does 'never roll your own crypto' advise?

    Cryptographic designs need years of public scrutiny. Homemade schemes almost always hide flaws that standard, reviewed ones do not have.

Summary

  • Encoding changes format (no secret), encryption provides confidentiality (needs a key), hashing provides one-way fingerprints for integrity.
  • Base64, hex and URL encoding are not security measures.
  • Symmetric encryption uses one shared key and is fast; asymmetric uses a public and a private key and solves key distribution and signing.
  • Real protocols are hybrids: public-key methods to agree on a key, symmetric encryption for the data.
  • Use standard algorithms from reviewed libraries; the key is the only secret, never the algorithm.

Next: Hash Functions and Integrity