Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 40 ยท Getting Started

What Is Cryptography?

Learn what cryptography is for: confidentiality, integrity, authentication and non-repudiation, how threat models work, Kerckhoffs's principle and its limits.

Cryptography is the science of protecting information when someone you do not trust can see it, copy it or change it. Every time you open a web page over HTTPS, unlock a phone, pay by card or install a software update, cryptography is working quietly in the background. Without it, anyone on the same Wi-Fi network could read your messages and anyone on the path to a server could swap a download for malware.

The word comes from Greek roots meaning "hidden writing", but modern cryptography is much more than secret messages. It is a toolbox of mathematical building blocks: ciphers that scramble data, hash functions that fingerprint it, signatures that prove who wrote it, and protocols that combine these pieces so strangers can build trust. In this tutorial you will build many of those pieces yourself in Python, break weak ones, and learn which real tools to use. This first lesson sets out the vocabulary and the mindset.

The four goals

Cryptographers rarely say "make it secure". They name a specific property they want, because each property needs a different tool.

  • Confidentiality means only the intended people can read the data. Encryption provides it.
  • Integrity means the data was not changed on the way. Hash functions and message authentication codes provide it.
  • Authentication means you know who you are talking to, or who sent a message. Passwords, message authentication codes, digital signatures and certificates provide it.
  • Non-repudiation means a sender cannot later deny having sent something. Only digital signatures give this, because a shared secret key could have been used by either side.

These goals are independent. Encrypted data can still be altered by an attacker who does not understand it. A message with a perfect integrity check can still be read by the whole world. Most real systems need several goals at once, which is why you will see them combined in later lessons (for example authenticated encryption).

flowchart LR
    A["Sender"] -- "message over<br/>an open channel" --> B["Receiver"]
    E["Attacker"] -. "reads (breaks confidentiality)" .-> A
    E -. "changes (breaks integrity)" .-> B
    E -. "pretends to be the sender<br/>(breaks authentication)" .-> B

Three different attacks on the same channel, each defeating a different goal.

Alice, Bob and Eve

Cryptography texts use a standard cast to keep explanations short. Alice wants to send something to Bob. Eve is an eavesdropper who can read everything on the network but cannot change it. Mallory is an active attacker who can read, delete, replay and modify messages. You will see these names throughout the tutorial, and they are just labels for roles: Alice might be a browser, Bob a bank server, Eve a stranger at a coffee shop.

What is a threat model?

Before choosing a tool, you must decide what you are defending against. That description is the threat model. It answers questions such as:

  • Who is the attacker (a curious neighbour, a criminal gang, a government)?
  • What can they do: only watch traffic, or also modify it? Do they have your device?
  • How much computing power and time do they have?
  • What are they trying to get: the data, a login, your money, your silence?

A threat model keeps you honest. A lock that stops a casual thief is useless against someone with bolt cutters, and a tool that is perfect against Eve may do nothing against Mallory. Encryption of a database on disk protects you if the disk is stolen, but it does nothing if an attacker logs in to the running application with a stolen password, because the application decrypts the data for every legitimate query.

Kerckhoffs's principle

In 1883 the Dutch linguist Auguste Kerckhoffs wrote that a cipher system must not depend on secrecy of the method: it should be able to fall into the enemy's hands without harm, and security should rest only on the key. Modern cryptographers restate this as assume the attacker knows the algorithm.

This sounds risky, but it is the opposite. Secret algorithms are fragile for three reasons:

  1. An algorithm cannot be changed easily once it has leaked, but a key can be replaced in seconds.
  2. Public algorithms are examined by thousands of experts, who find the flaws before attackers do. Secret designs get no such review.
  3. A system with one tiny secret (the key) is far easier to protect than a system whose secrecy relies on a large piece of software.

Try it. The cipher below is a deliberately weak toy (you will learn exactly why in XOR and the one-time pad), but it shows the principle: the method is completely public, and only the key makes the output unreadable.

Python Public algorithm, secret key

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.

Everything in that function is public, yet without b"alice-key" the output is useless. The strength of a good cipher comes from making key guessing impractical, not from hiding the code.

Warning: "Security through obscurity" (hiding how a system works and hoping nobody finds out) is not a defence. Hiding details can be one extra layer on top of real security, but it must never be the only one.

A tiny history

Cryptography is as old as writing. The Spartans wound a strip of leather around a rod to read a message. Julius Caesar shifted letters by three positions. For centuries, ciphers were made and broken by hand, and the breakers usually won: you will break a few of them in classical ciphers. In the First and Second World Wars, machines such as Enigma automated encryption, and breaking them drove the first electronic computers.

The modern era began in the 1970s with two events. In 1977 the US published the Data Encryption Standard, the first public, standardized cipher. In 1976 Whitfield Diffie and Martin Hellman described public-key cryptography, where two people can agree on a secret without ever having met, which you will explore in Diffie-Hellman. Today the standards are open competitions: AES for encryption, SHA-2 and SHA-3 for hashing, and recently the first post-quantum algorithms.

Integrity versus authentication in code

Hashing alone gives you a fingerprint, but a fingerprint is not proof of origin. If Mallory can change the message, she can also recompute the fingerprint. Authentication needs a secret that Mallory lacks. This demo uses hashlib and hmac from the standard library, which you will study properly later.

Python Integrity needs a secret

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 forged message is rejected because Mallory could not compute the right tag without the key. Notice what this does not give you: both Alice and Bob hold the key, so either could have created a valid tag. That is why a shared key cannot provide non-repudiation, and why digital signatures exist.

What cryptography cannot fix

Cryptography is a strong tool, but it is only one part of security. Real failures almost never come from breaking AES.

  • People and processes. Phishing, weak passwords reused across sites, and a stolen laptop with an unlocked session bypass the mathematics completely.
  • Implementation bugs. A correct algorithm used wrongly (a reused nonce, a missing check, a predictable random number) can be completely broken. This is why the usual advice is never invent your own cryptography; use well-reviewed libraries.
  • Endpoints. Encryption protects data in transit or at rest. If malware runs on the device that holds the plaintext, it reads the data before it is encrypted.
  • Metadata. Even when contents are hidden, who talked to whom, when, and how much often remains visible.
  • Availability. Cryptography does not stop someone from knocking your server offline.

The practical rule: use cryptography to remove specific, named risks from your threat model, and do not expect it to remove all of them.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which security goal does a digital signature provide that a shared-key message authentication code cannot?

    With a shared key, either party could have produced the tag, so neither can be held to it. A signature is made with a private key only the signer holds, so it ties the message to one person.
  2. 2. What does Kerckhoffs's principle say about a secure cipher?

    Assume the attacker knows how the system works. Keys are small and replaceable, while algorithms are large, hard to change and benefit from public review.
  3. 3. An attacker can modify messages on the network but cannot read them because they are encrypted. Which goal is still at risk?

    Plain encryption does not stop modification. Without an integrity check, the attacker can flip bits or swap blocks and the receiver may accept the altered data.
  4. 4. What is a threat model?

    A threat model states the attacker's capabilities and goals, so you can judge whether a defence is actually enough.

Summary

  • Cryptography protects information against attackers who can see or change it; its goals are confidentiality, integrity, authentication and non-repudiation.
  • Each goal needs its own tool, and real systems combine several.
  • A threat model defines the attacker and what they can do. Decide it before choosing defences.
  • Kerckhoffs's principle: assume the attacker knows the algorithm; only the key is secret.
  • A plain hash cannot authenticate; you need a secret (a keyed tag) or a signature.
  • Cryptography cannot fix weak passwords, buggy implementations, compromised devices or metadata leaks.

Next: Encoding, Hashing and Encryption, where you learn to tell three commonly confused tools apart.