How cryptography works

The algorithms themselves — key exchange, public-key encryption, signatures, symmetric ciphers, and hashes, one interactive walkthrough at a time.

Asymmetric cryptography

The major problem of symmetric cryptography is key distribution - sending private keys over public channels is unsafe, and meeting in person is obviously not convenient.

Public-key cryptography addresses this using mathematical trapdoor functions: operations that are easy to compute with a public key but infeasible to reverse without a corresponding private key. This allows secure key exchange, encryption, and digital signatures even when the public key is openly shared.

Symmetric cryptography

Asymmetric cryptography is powerful, but it is comparatively expensive. Real systems usually use it first to exchange a secret or authenticate the parties, then move to symmetric encryption for the actual message data.

That second phase is where symmetric cryptography takes over. Once both sides share the same secret key, they can encrypt large amounts of data much faster than with public-key operations.

Algebraic structures

Groups

G

Before finite fields show up in cryptography, the older story starts with polynomial equations. A polynomial like x2−5x+6=0x^2 - 5x + 6 = 0 has roots, in this case 22 and 33. For quadratics there is a formula for the roots. For cubics and quartics there are formulas too. But for fifth-degree equations, mathematicians kept running into a wall.

Galois looked at the problem from a strange and brilliant angle: instead of trying to stare harder at the roots themselves, ask which ways you can rearrange the roots while preserving all the algebraic relationships between them. Those rearrangements are symmetries of the equation.

That is the important bridge. A symmetry is not just a visual idea; it can be an action. Rotate a square and it still fits itself. Swap certain roots of an equation and the hidden arithmetic still fits. When these actions can be combined, undone, and kept inside the same collection, they form a group.

Group definition

Definition: If SS is a set, ∗\ast is a binary operation on SS, and e∈Se\in S, then G=(S,e,∗)G=(S,e,\ast) is called a group if:

  1. ∀a,b∈S, ∃c∈S such that a∗b=c\forall a,b\in S,\ \exists c\in S\text{ such that }a\ast b=c

    Closure. The operation ∗\ast takes elements of the group as inputs and returns another element of the group. It does not produce something outside the set.

  2. (a∗b)∗c=a∗(b∗c),∀a,b,c∈S(a\ast b)\ast c = a\ast(b\ast c),\quad \forall a,b,c\in S

    Associativity. Parentheses do not change the result. If three elements are combined in the same order, it does not matter which pair is combined first.

  3. a∗e=a=e∗a,∀a∈Sa\ast e = a = e\ast a,\quad \forall a\in S

    Identity. Combining any element with ee returns the same element, from either side. Such an ee is called the identity element or neutral element. For ordinary multiplication, 11 is the identity because a⋅1=a=1⋅aa\cdot1=a=1\cdot a.

  4. ∀a∈S, ∃a−1∈S such that a∗a−1=e=a−1∗a\forall a\in S,\ \exists a^{-1}\in S\text{ such that }a\ast a^{-1}=e=a^{-1}\ast a

    Inverse. Every element has another element that undoes it. Combining an element with its inverse returns the identity ee, from either side.

  5. a∗b=b∗a,∀a,b∈Sa\ast b=b\ast a,\quad \forall a,b\in S

    Commutative (opt). A group is commutative, also named abelian, if changing the order of two elements does not change the result. This is an extra property: not every group has it.

Group examples

Imagine a square on a plane. You can turn it, flip it, or leave it alone, and after each move it still fits exactly back into the same square-shaped place.

Together these eight motions form D4D_4, the symmetry group of a square: four rotations, four flips, and a rule for combining any two moves. Use the buttons to build a sequence, watch the marked square move, and read the table as the record of what happens when one motion follows another.

Cayley table for square motions in D4
apply / stateR0R90R180R270HVDD'
R0R0R90R180R270HVDD'
R90R90R180R270R0D'DHV
R180R180R270R0R90VHD'D
R270R270R0R90R180DD'VH
HHDVD'R0R180R90R270
VVD'HDR180R0R270R90
DDVD'HR270R90R0R180
D'D'HDVR90R270R180R0

Motions

Sequence

No motions applied yet.

Net effect R0

Now consider the whole integer line. Start on an integer, add another integer, and the result is still somewhere on that same line. This group is (Z,+)(\mathbb{Z}, +): the set of all integers with addition as the operation. The identity element is 0, because adding 0 leaves every integer unchanged.

Integer line

-100-99-98-97-96-95-94-93-92-91-90-89-88-87-86-85-84-83-82-81-80-79-78-77-76-75-74-73-72-71-70-69-68-67-66-65-64-63-62-61-60-59-58-57-56-55-54-53-52-51-50-49-48-47-46-45-44-43-42-41-40-39-38-37-36-35-34-33-32-31-30-29-28-27-26-25-24-23-22-21-20-19-18-17-16-15-14-13-12-11-10-9-8-7-6-5-4-3-2-10123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100

Motions

Sequence

No additions yet.

Current integer 0

Now take the same addition idea and bend it into a 12-hour clock. This circular group is (Z12,+)(\mathbb{Z}_{12}, +): adding hours wraps around after 11 and lands back on 0, so the result always stays inside the same twelve positions. The identity element is 0, because adding 0 does not move the marker.

Clock 12

01234567891011

Motions

Sequence

No clock moves yet.

Clock position 0 modulo 12

Permutation groups (shuffling)

Take three objects: A B C. There are six possible rearrangements. Together they form S3S_3, the permutation group on three objects.

The operation is simple: do one shuffle after another. The result is still one of the six rearrangements, every shuffle can be undone, and ABC acts as the identity shuffle. This is the same kind of group Galois studied when he rearranged roots of equations.

A
A
A
B
B
B
C
C
C
Cayley table for permutation shuffles in S3
shuffle / stateABCACBBACBCACABCBA
ABCABCACBBACBCACABCBA
ACBACBABCBCABACCBACAB
BACBACCABABCCBAACBBCA
BCABCACBAACBCABABCBAC
CABCABBACCBAABCBCAACB
CBACBABCACABACBBACABC

Shuffles

Sequence

No shuffles yet.

Algebraic structures

Rings

R
G

Groups gave us one operation at a time. A ring introduces a second operation, allowing the same set to support both addition and multiplication in a compatible way.

The integers are the prototype. You can add integers, subtract them, and multiply them, and every result stays inside the integers. Addition behaves symmetrically and reversibly, while multiplication interacts with addition through the distributive laws:

a(b+c)=ab+aca(b+c)=ab+ac

This combination of two interacting operations is the central idea behind rings.

Historically, rings grew out of attempts to generalize ordinary arithmetic to more complicated settings: polynomial expressions, modular arithmetic, algebraic integers, and eventually finite fields used in modern cryptography.

A ring keeps enough arithmetic structure to talk about equations, divisibility, and factorization, but unlike a field, division is not always possible.

Ring definition

Definition: If RR is a non-empty set, ++ is a binary operation on RR, and ⋅\cdot is a binary operation on RR, then (R,+,⋅)(R,+,\cdot) is called a ring if:

  1. +:R×R→R,(a,b)↦a+b⋅:R×R→R,(a,b)↦a⋅b\begin{aligned}{}+ &: R\times R\to R,\quad (a,b)\mapsto a+b\\ \cdot &: R\times R\to R,\quad (a,b)\mapsto a\cdot b\end{aligned}

    Binary operations. Addition and multiplication each take two elements of RR and return an element of RR.

  2. a+(b+c)=(a+b)+c,∀a,b,c∈Ra+(b+c)=(a+b)+c,\quad \forall a,b,c\in R

    Additive associativity. Parentheses do not change a sum of three elements.

  3. ∃0∈R such that a+0=a=0+a,∀a∈R\exists 0\in R\text{ such that }a+0=a=0+a,\quad \forall a\in R

    Additive identity. Adding 00 leaves every element unchanged, from either side.

  4. ∀a∈R, ∃−a∈R such that a+(−a)=0=(−a)+a\forall a\in R,\ \exists -a\in R\text{ such that }a+(-a)=0=(-a)+a

    Additive inverse. Every element has an additive inverse that returns the additive identity 00 from either side.

  5. a+b=b+a,∀a,b∈Ra+b=b+a,\quad \forall a,b\in R

    Additive commutativity. Addition ignores order. Together, the previous four addition rules say that (R,+)(R,+) is an abelian group.

  6. a⋅(b⋅c)=(a⋅b)⋅c,∀a,b,c∈Ra\cdot(b\cdot c)=(a\cdot b)\cdot c,\quad \forall a,b,c\in R

    Multiplicative associativity. Parentheses do not change a product of three elements.

  7. a⋅(b+c)=a⋅b+a⋅c(a+b)⋅c=a⋅c+b⋅c,∀a,b,c∈R\begin{aligned}a\cdot(b+c)&=a\cdot b+a\cdot c\\ (a+b)\cdot c&=a\cdot c+b\cdot c,\quad \forall a,b,c\in R\end{aligned}

    Distributivity. Multiplication is compatible with addition from both the left and the right.

This convention does not require a multiplicative identity or commutative multiplication. Those extra properties are named separately as a ring with identity or a commutative ring.

Ring examples

Now consider the whole integer line again. Start on an integer, add another integer, subtract another integer, or multiply by another integer, and the result is still somewhere on that same line. This ring is (Z,+,⋅)(\mathbb{Z}, +, \cdot): the set of all integers with addition and multiplication as the operations. The additive identity is 0, because adding 0 leaves every integer unchanged. We start this example at 2.

Division is different. Dividing one integer by another does not necessarily land back on an integer: 3÷2=1.53 \div 2 = 1.5, which is outside Z\mathbb{Z}. That is why the integers form a ring, but not a field.

Integer line

-100-99-98-97-96-95-94-93-92-91-90-89-88-87-86-85-84-83-82-81-80-79-78-77-76-75-74-73-72-71-70-69-68-67-66-65-64-63-62-61-60-59-58-57-56-55-54-53-52-51-50-49-48-47-46-45-44-43-42-41-40-39-38-37-36-35-34-33-32-31-30-29-28-27-26-25-24-23-22-21-20-19-18-17-16-15-14-13-12-11-10-9-8-7-6-5-4-3-2-10123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100

Operations

Sequence

No ring operations yet.

Current integer 2 in Z\mathbb{Z}

Now take the same ring idea and bend it into a 12-hour clock. This ring is (Z12,+,⋅)(\mathbb{Z}_{12}, +, \cdot): the twelve clock positions with addition and multiplication performed modulo 12. Add, subtract, or multiply, and the result always wraps back into one of the same twelve positions.

Clock 12

01234567891011

Operations

Sequence

No clock ring operations yet.

Clock position 5 modulo 12

Now narrow the integer line to only the even integers. This set is 2Z2\mathbb{Z}: all integer multiples of 2. Adding, subtracting, or multiplying two even integers always produces another even integer, so the arithmetic stays inside the same set.

The odd positions are not shown below because they are not part of 2Z2\mathbb{Z}. Since 11 is not an even integer, 2Z2\mathbb{Z} has no multiplicative identity. Some definitions of rings require a ring to have one, and some allow rings without one.

Even integers line

-100-98-96-94-92-90-88-86-84-82-80-78-76-74-72-70-68-66-64-62-60-58-56-54-52-50-48-46-44-42-40-38-36-34-32-30-28-26-24-22-20-18-16-14-12-10-8-6-4-202468101214161820222426283032343638404244464850525456586062646668707274767880828486889092949698100

Operations

Sequence

No even-integer operations yet.

Current even integer 4 in 2Z2\mathbb{Z}

A polynomial ring extends a familiar number system by introducing a formal variable while keeping all coefficient arithmetic inside the original ring. In Z[x]\mathbb{Z}[x], every polynomial has integer coefficients, so adding or multiplying polynomials simply combines integers and still produces another polynomial in the same ring.

The variable xx acts as a symbolic placeholder rather than a specific number. The structure comes from the coefficients and the rules for combining powers of xx.

A(x)x2−3x+2x^2-3x+2
B(x)2x+12x+1
Result(x2−3x+2)⋅(2x+1)=2x3−5x2+x+2\left(x^2-3x+2\right)\cdot\left(2x+1\right)=2x^3-5x^2+x+2
-5-2.502.55-418.80172.8

Polynomials

Operation

Algebraic structures

Fields

F
R
G

A field is a ring where division works for every non-zero element. You can add, subtract, multiply, and divide without leaving the same set, as long as you never divide by 00.

That extra reversibility is the key difference from rings like Z\mathbb{Z}. In the integers, 3÷23 \div 2 leaves the set. In a field, every non-zero element has a multiplicative inverse inside the set, so division can be rewritten as multiplication by that inverse.

Field definition

Definition: If FF is a non-empty set, ++ is a binary operation on FF, and ⋅\cdot is a binary operation on FF, then (F,+,⋅)(F,+,\cdot) is called a field if:

  1. (F,+,⋅) is a commutative ring(F,+,\cdot)\text{ is a commutative ring}

    Ring structure. All ring axioms hold, and multiplication is commutative: a⋅b=b⋅aa\cdot b=b\cdot a for all a,b∈Fa,b\in F.

  2. ∃1F∈F with 1F≠0 such that 1F⋅a=a=a⋅1F,∀a∈F\exists 1_F\in F\text{ with }1_F\neq0\text{ such that }1_F\cdot a=a=a\cdot1_F,\quad \forall a\in F

    Multiplicative identity. The element 1F1_F leaves every element unchanged under multiplication, from either side, and it is different from 00.

  3. ∀a∈F, a≠0⇒∃a−1∈F such that a⋅a−1=1F=a−1⋅a\forall a\in F,\ a\neq0\Rightarrow\exists a^{-1}\in F\text{ such that }a\cdot a^{-1}=1_F=a^{-1}\cdot a

    Multiplicative inverse. Every non-zero element has an inverse inside FF. This is the rule that makes division possible inside the field.

Equivalently, the addition part (F,+)(F,+) is an abelian group, the non-zero elements of FF form an abelian group under multiplication, and multiplication distributes over addition.

Field properties

1. Uniqueness of inverse. If a∈Fa\in F and a≠0a\neq0, then the inverse a−1a^{-1} is unique.

Suppose b,c∈F are both inverses of a.\text{Suppose } b,c\in F \text{ are both inverses of } a.
a⋅b=1F=b⋅aanda⋅c=1F=c⋅aa\cdot b=1_F=b\cdot a \quad \text{and} \quad a\cdot c=1_F=c\cdot a
b=b⋅1F=b⋅(a⋅c)=(b⋅a)⋅c=1F⋅c=c\begin{aligned}b&=b\cdot1_F\\ &=b\cdot(a\cdot c)\\ &=(b\cdot a)\cdot c\\ &=1_F\cdot c\\ &=c\end{aligned}

So the two supposed inverses are equal. Therefore there cannot be two different inverses of aa; the inverse is unique.

2. Multiplication by zero. If a∈Fa\in F, then a⋅0=0a\cdot0=0.

a⋅0=a⋅(0+0)=a⋅0+a⋅0a\cdot0=a\cdot(0+0)=a\cdot0+a\cdot0
Cancel a⋅0 from both sides, so a⋅0=0.\text{Cancel }a\cdot0\text{ from both sides, so }a\cdot0=0.

3. No zero divisors. If a,b∈Fa,b\in F and a⋅b=0a\cdot b=0, then a=0a=0 or b=0b=0.

Now suppose a⋅b=0a\cdot b=0. If a=0a=0, we are done. If a≠0a\neq0, then:

b=1F⋅b=(a−1⋅a)⋅b=a−1⋅(a⋅b)=a−1⋅0=0\begin{aligned}b&=1_F\cdot b\\ &=(a^{-1}\cdot a)\cdot b\\ &=a^{-1}\cdot(a\cdot b)\\ &=a^{-1}\cdot0\\ &=0\end{aligned}

Therefore a product in a field can be 00 only when at least one of its factors is 00.

4. Distinctiveness of sums and products. If a,b,c∈Fa,b,c\in F and b≠cb\neq c, then adding the same element keeps the results distinct:

a+b≠a+ca+b\neq a+c

Suppose instead that a+b=a+ca+b=a+c. Add −a-a to both sides:

(−a)+(a+b)=(−a)+(a+c)((−a)+a)+b=((−a)+a)+c0+b=0+cb=c\begin{aligned}(-a)+(a+b)&=(-a)+(a+c)\\ ((-a)+a)+b&=((-a)+a)+c\\ 0+b&=0+c\\ b&=c\end{aligned}

This contradicts b≠cb\neq c. So a+ba+b and a+ca+c are distinct.

For products, if a≠0a\neq0 and b≠cb\neq c, then:

a⋅b≠a⋅ca\cdot b\neq a\cdot c

Suppose instead that a⋅b=a⋅ca\cdot b=a\cdot c. Multiply both sides by a−1a^{-1}:

a−1⋅(a⋅b)=a−1⋅(a⋅c)(a−1⋅a)⋅b=(a−1⋅a)⋅c1F⋅b=1F⋅cb=c\begin{aligned}a^{-1}\cdot(a\cdot b)&=a^{-1}\cdot(a\cdot c)\\ (a^{-1}\cdot a)\cdot b&=(a^{-1}\cdot a)\cdot c\\ 1_F\cdot b&=1_F\cdot c\\ b&=c\end{aligned}

Again this contradicts b≠cb\neq c. So multiplying by a non-zero element also preserves distinctness.

Field examples

The rational numbers Q\mathbb{Q} are all fractions pq\frac{p}{q} where p,q∈Zp,q\in\mathbb{Z} and q≠0q\neq0. This field is (Q,+,⋅)(\mathbb{Q},+,\cdot): adding, subtracting, multiplying, and dividing by a non-zero rational number all stay inside Q\mathbb{Q}.

(34)÷(−56)=−910\left(\frac{3}{4}\right)\div\left(-\frac{5}{6}\right)=-\frac{9}{10}

Rational numbers

A

B

Operation

The real numbers R\mathbb{R} fill the continuous number line. They include rational numbers, numbers like 2\sqrt{2} and π\pi, and every decimal point location between them. This field is (R,+,⋅)(\mathbb{R},+,\cdot): adding, subtracting, multiplying, and dividing by a non-zero real number all stay inside R\mathbb{R}.

(1.8)÷(−4.5)=−0.4\left(1.8\right)\div\left(-4.5\right)=-0.4

Real numbers

Operation

In cryptography, we often work inside finite arithmetic systems instead of the infinite number line. The goal is to keep every operation inside a fixed set while preserving the behavior of addition, multiplication, and division by non-zero elements.

A finite field is simply a field with finitely many elements. It is also called a Galois field, written GF(q)\mathrm{GF}(q), after Evariste Galois, a remarkably influential mathematician who helped lay the foundations of modern algebra before dying at just 20 years old.

GF(2)\mathrm{GF}(2) is the smallest finite field. It has 2 elements {0,1}\{0,1\} and arithmetic is performed modulo 22.

Addition table for GF(2)
a + b (mod 2)01
001
110
Multiplication table for GF(2)
a x b (mod 2)01
000
101

GF(3)\mathrm{GF}(3) is a finite field. It has 3 elements {0,1,2}\{0,1,2\} and arithmetic is performed modulo 33.

Addition table for GF(3)
a + b (mod 3)012
0012
1120
2201
Multiplication table for GF(3)
a x b (mod 3)012
0000
1012
2021

GF(4)\mathrm{GF}(4) cannot be built as four-step clock arithmetic on 0,1,a,b0,1,a,b; watch where the field rules break.

Four-element cyclic addition attempt
x + y (mod 4)01ab
0----
1----
a----
b----
Partial multiplication table for the failed four-element field attempt
x * y (mod 4)01ab
0----
1----
a----
b----

Conditions to construct a finite field

Zn is a field⟺n is prime\mathbb{Z}_n\text{ is a field}\Longleftrightarrow n\text{ is prime}

The integers modulo nn, Zn={0,1,2,…,n−1}\mathbb{Z}_n=\{0,1,2,\ldots,n-1\}, form a field if and only if nn is prime.We prove the equivalence in two directions.

If nn is prime

Take any non-zero element a∈Zn, a≠0a\in\mathbb{Z}_n,\ a\neq0.

a∈Zn,a≠0a\in\mathbb{Z}_n,\quad a\neq0gcd⁡(a,n)=1since n is prime\gcd(a,n)=1\quad\text{since }n\text{ is prime}
ax+ny=1,x,y∈Zax+ny=1,\quad x,y\in\mathbb{Z}from Bézout's identity since gcd⁡(a,n)=1\gcd(a,n)=1
ny≡0(modn)ny\equiv0\pmod nax≡1(modn)ax\equiv1\pmod n

Thus xx is a multiplicative inverse of aa. Every non-zero element has an inverse.

∴ Zn is a field\therefore\ \mathbb{Z}_n\text{ is a field}

If nn is composite

Suppose nn is composite, so n=abn=ab with 1<a,b<n1<a,b<n. View aa and bb as residues in Zn\mathbb{Z}_n.

n=abn = ab1<a<n,1<b<n1 < a < n,\qquad 1 < b < na≢0(modn),b≢0(modn)a \not\equiv 0 \pmod n,\qquad b \not\equiv 0 \pmod nab=n≡0(modn)ab = n \equiv 0 \pmod n
a≠0,b≠0,a⋅b=0a \neq 0,\qquad b \neq 0,\qquad a\cdot b = 0zero divisors

The elements aa and bb are zero divisors. That means both are non-zero in Zn\mathbb{Z}_n, but their product is zero. A field cannot have this happen because every non-zero element must have an inverse. If a⋅b=0a\cdot b=0 and a−1a^{-1} existed, multiplying by a−1a^{-1} would force b=0b=0.

∴ Zn is not a field\therefore\ \mathbb{Z}_n\text{ is not a field}

More generally, a finite field can contain only a prime-power number of elements. Every finite field has size pmp^m for some prime pp and positive integer mm. Conversely, a finite field exists for every such size.

The fields constructed using integers modulo a prime correspond to the simplest case m=1m=1. These are called prime fields. When m>1m>1, the construction is no longer just arithmetic modulo pmp^m. The system Zpm={0,1,…,pm−1}\mathbb{Z}_{p^m}=\{0,1,\ldots,p^m-1\} contains zero divisors, so it cannot be a field. Indeed, p⋅pm−1=pm≡0(modpm)p\cdot p^{m-1}=p^m\equiv0\pmod{p^m} even though both factors are non-zero modulo pmp^m. Therefore finite fields of size pmp^m for m>1m>1 must be constructed in a different way.

GF(pm),p prime, m∈Z≥1\mathrm{GF}(p^m),\quad p\text{ prime},\ m\in\mathbb{Z}_{\ge1}
m=1m>1

Calculation in prime fields

Elements are the integers 0,1,…,p−10, 1, \ldots, p-1.

Add, subtract, and multiply as usual — then reduce modulo pp.

Division is multiplication by the modular inverse: a÷b=a⋅b−1(modp)a \div b = a \cdot b^{-1} \pmod{p}, found via the Extended Euclidean Algorithm.

Modulus space (mod 7)

Integers space

A
B
3+4=7≡0(mod7)3 + 4 = 7 \equiv 0 \pmod{7}

A Feistel network is a block-cipher structure: it encrypts one fixed size block at a time under a shared secret key. DES is the classic Feistel-based block cipher, and the same idea also appears in designs such as Blowfish and Twofish.

Each round applies a simple reversible pattern: one half is modified using a keyed function and XOR, while the other half is just passed through and then the two halves are swapped. Even though the round function itself is not invertible, the use of XOR (which can be undone by applying the same value again) and the swap ensures that every step can be reversed in the opposite order, making the entire construction invertible.

Key

K1=0x4K2=0xBK3=0x3K4=0x9
byte 1 0x4Bbyte 2 0x39

Message

byte 1 0x43

Start with L0=0100 and R0=0011. The block is already split into left and right halves.

Rule: Li = R(i-1) and Ri = L(i-1) XOR F(R(i-1), Ki)

Encryption chainRound K1FR0, K11110K10x4Round K2FR1, K21010K20xBRound K3FR2, K31001K30x3Round K4FR3, K41001K40x9L001000x4R000110x3L100110x3R110100xAL210100xAR210010x9L310010x9R300110x3L400110x3R400000x0

A substitution-permutation network (SPN) keeps the whole block together instead of splitting it in half. Each round applies three operations in order: XOR with a round key (key mixing), substitution through S-boxes (nonlinear confusion), and bit routing through a P-box (diffusion). AES is the most widely deployed SPN cipher.

Confusion makes the key's effect hard to trace from the ciphertext. Diffusion spreads each plaintext bit's influence across many output bits, so a small input change becomes hard to isolate after a few rounds.

Decryption runs the same rounds in reverse. The lookup tables in inverse order and a second XOR with the same key undo the changes.

Key

byte 1 0x4D

Message

byte 1 0xB2

S-box

In
0000
0001
0010
0011
0100
0101
0110
0111
Out
In
1000
1001
1010
1011
1100
1101
1110
1111
Out

P-box

In
00
11
21
31
40
51
61
71
Out
10
01
12
13
14
05
16
17

The cipher operates on fixed-size blocks (8 bits here). The left nibble is 1011, the right is 0010. Flip any bit above to trace its effect through every stage.

Message = 10110010 (0xB2)

SP Network RoundSubstitutionPermutationMessage1011 00100xB2Key01001101After XOR1111 11110xFFS11111↓0111S21111↓0111Substituted0x7701110111Output0xBB10111011

The Advanced Encryption Standard (AES) stands apart from most ciphers in one important way: it was chosen through an open, public competition. In 1997 NIST invited the global cryptographic community to submit candidates for a new federal standard to replace the aging DES. After five years of public evaluation, analysis, and benchmarking, the algorithm Rijndael— designed by Belgian cryptographers Joan Daemen and Vincent Rijmen — was standardized as AES in 2001. The open process means the cipher has been scrutinized by hundreds of independent researchers, which is a large part of why it is trusted everywhere today.

Like the substitution-permutation network we explored earlier, AES is built from the same core idea: alternate rounds of key mixing, nonlinear substitution, and diffusion. But where our toy SPN operated on a single 8-bit byte with a hand-crafted 16-entry S-box, AES works on a 128-bit block (arranged as a 4×4 matrix of bytes) and derives all of its substitution and mixing values from the mathematics of GF(28)\mathrm{GF}(2^8) — the finite field of 256 elements. Every byte substitution is a field inversion followed by an affine transform, and the mixing step multiplies columns of the state matrix by fixed polynomials over that same field. The result is a cipher whose security properties do not come from an arbitrary black-box lookup table, but from a deliberate algebraic structure.

AES-128AES-192AES-25610 rounds12 rounds14 roundsmessage128 bitsciphertext128 bitskey128 | 192 | 256 bits

Key

128 bits

2B7E151628AED2A6ABF7158809CF4F3C

Message

128 bits

Key mixingRoundRounds 2-10Input state320431F62A838845A53068D73183199810A211E012371307143415k02B 7E ... 3C193DE3BEA0F4E22B9AC68D2AE9F84808SubBytesShiftRowsMixColumnsAddRoundKeySSSSSSSSSSSSSSSSD42711AEE0BF98F1B8B45DE51E415230D4BF5D30E0B452AEB84111F11E2798E5D4BF5D30E0B452AEB84111F11E2798E5MMMM046681E5E0CB199A48F8D37A2806264C046681E5E0CB199A48F8D37A2806264Ck1A0 FA ... 05A49C7FF2689F352B6B5BEA43026A5049SubBytesShiftRowsMixColumnsAddRoundKeyCiphertext3902518421D3024DC5096FB7DC81198510971119126A130B143215128 bits

A block cipher encrypts one fixed size block at a time, e.g. 128 bits for AES. A mode of operation defines how the cipher is applied to data of arbitrary length. Different modes make different trade offs: some support parallel processing, some provide authentication, and some require random IVs or nonces to hide repeating patterns.

Electronic Codebook (ECB) splits the message into fixed-size blocks and encrypts each block independently with the same key. Conceptually, each ciphertext block is C_i = E_K(P_i).

Same input block - same ciphertext block:

P1
empty bg
->
E_K
->
C1
8f31
P2
edge
->
E_K
->
C2
b674
P3
empty bg
->
E_K
->
C3
8f31

The weakness is that ECB hides each block's contents, but it does not hide when two plaintext blocks are equal. Equal plaintext blocks become equal ciphertext blocks. In image-like data, this means repeated regions can still draw a recognizable outline after encryption. The colors change, but the structure remains visible.

Original
ECB output

Cipher Block Chaining (CBC) mixes each plaintext block with the previous ciphertext block before encryption. The first block uses an initialization vector, or IV, as the previous value. Conceptually, C_i = E_K(P_i xor C_(i-1)), with C_0 = IV.

Same input block + different previous block - different ciphertext block:

P1
empty bg
v
P2
edge
v
P3
empty bg
v
IV
a91c
->
->
E_K
->
C1
34a9
->
->
E_K
->
C2
e812
->
->
E_K
->
C3
6bc0

CBC hides the repeated-block pattern that ECB leaks, but encryption is chained: each block needs the previous ciphertext block first. That makes encryption harder to parallelize. For live data such as streaming video, the chain can add latency and makes independent chunk processing less convenient.

Original
CBC output

Counter Mode (CTR) turns a block cipher into a stream of key material. A nonce is a value used once with a given key. It does not need to be secret, but it must not repeat for that key. CTR combines the nonce with a block counter, encrypts that input, then XORs the encrypted output with the plaintext block. Conceptually, C_i = P_i xor E_K(nonce || i).

Each counter block can be produced independently:

P1
empty bg
v
nonce
counter 0
->
E_K
->
keystream 1
7ac9
->->
C1
c5f1
P2
edge
v
nonce
counter 1
->
E_K
->
keystream 2
91de
->->
C2
36a4
P3
empty bg
v
nonce
counter 2
->
E_K
->
keystream 3
d02b
->->
C3
4e87

CTR output looks patternless when every nonce and counter input is unique, but it is fragile if that uniqueness rule is broken. Reusing the same nonce with the same key repeats the keystream, so comparing ciphertexts can reveal relationships between the original plaintexts. CTR also does not authenticate data: changing a ciphertext bit predictably changes the decrypted plaintext bit.

Original
CTR output

Galois/Counter Mode (GCM) uses counter-mode encryption for confidentiality and adds an authentication tag. The tag covers the ciphertext and optional associated data, or AAD: data that should be authenticated but not encrypted, such as a packet header. A receiver should accept the plaintext only if the tag verifies.

GCM produces ciphertext blocks like CTR, then feeds the ciphertext and AAD into GHASH. GHASH is a fast finite-field hashing step that compresses those values into an authentication value, which becomes part of the final tag:

P1
empty bg
v
nonce
counter 0
->
E_K
->
keystream 1
a441
->->
C1
59e0
P2
edge
v
nonce
counter 1
->
E_K
->
keystream 2
2cb7
->->
C2
8d3c
C1, C2, AAD
data to authenticate
->
GHASH
->
tag
auth check

GCM gives both privacy and tamper detection, but the nonce rule is strict. Reusing a nonce with the same key can break confidentiality and can also weaken the authentication tag. GCM is also only useful if the receiver verifies the tag before trusting or using the decrypted plaintext.

AES-GCM is one of the most widely used forms of authenticated encryption in modern protocols.

Original
GCM output

Cryptographic hash functions

A cryptographic hash function turns any input into a fixed-length fingerprint. The output is deterministic, fast to compute, and changes completely when the input changes, making hashes ideal for integrity checks.

Hashes are one-way. Given only a hash, it should be infeasible to recover the original input or find another input that produces the same digest. Hashes provide tamper evidence, not secrecy.

See how hashes secure blockchains in the main demo: tweak a block and the downstream hashes break. Go to the Blockchain Hashing demo.