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
Before finite fields show up in cryptography, the older story starts with polynomial equations. A polynomial like x2−5x+6=0 has roots, in this case 2 and 3. 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 S is a set, ∗ is a binary operation on S, and e∈S, then G=(S,e,∗) is called a group if:
- ∀a,b∈S, ∃c∈S such that a∗b=c
Closure. The operation ∗ takes elements of the group as inputs and returns another element of the group. It does not produce something outside the set.
- (a∗b)∗c=a∗(b∗c),∀a,b,c∈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.
- a∗e=a=e∗a,∀a∈S
Identity. Combining any element with e returns the same element, from either side. Such an e is called the identity element or neutral element. For ordinary multiplication, 1 is the identity because a⋅1=a=1⋅a.
- ∀a∈S, ∃a−1∈S such that a∗a−1=e=a−1∗a
Inverse. Every element has another element that undoes it. Combining an element with its inverse returns the identity e, from either side.
- a∗b=b∗a,∀a,b∈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 D4, 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.
| apply / state | R0 | R90 | R180 | R270 | H | V | D | D' |
|---|---|---|---|---|---|---|---|---|
| R0 | R0 | R90 | R180 | R270 | H | V | D | D' |
| R90 | R90 | R180 | R270 | R0 | D' | D | H | V |
| R180 | R180 | R270 | R0 | R90 | V | H | D' | D |
| R270 | R270 | R0 | R90 | R180 | D | D' | V | H |
| H | H | D | V | D' | R0 | R180 | R90 | R270 |
| V | V | D' | H | D | R180 | R0 | R270 | R90 |
| D | D | V | D' | H | R270 | R90 | R0 | R180 |
| D' | D' | H | D | V | R90 | R270 | R180 | R0 |
Motions
Sequence
No motions applied yet.
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,+): the set of all integers with addition as the operation. The identity element is 0, because adding 0 leaves every integer unchanged.
Integer line
Motions
Sequence
No additions yet.
Now take the same addition idea and bend it into a 12-hour clock. This circular group is (Z12,+): 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
Motions
Sequence
No clock moves yet.
Permutation groups (shuffling)
Take three objects: A B C. There are six possible rearrangements. Together they form S3, 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.
| shuffle / state | ABC | ACB | BAC | BCA | CAB | CBA |
|---|---|---|---|---|---|---|
| ABC | ABC | ACB | BAC | BCA | CAB | CBA |
| ACB | ACB | ABC | BCA | BAC | CBA | CAB |
| BAC | BAC | CAB | ABC | CBA | ACB | BCA |
| BCA | BCA | CBA | ACB | CAB | ABC | BAC |
| CAB | CAB | BAC | CBA | ABC | BCA | ACB |
| CBA | CBA | BCA | CAB | ACB | BAC | ABC |
Shuffles
Sequence
No shuffles yet.
Algebraic structures
Rings
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:
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 R is a non-empty set, + is a binary operation on R, and ⋅ is a binary operation on R, then (R,+,⋅) is called a ring if:
- +⋅:R×R→R,(a,b)↦a+b:R×R→R,(a,b)↦a⋅b
Binary operations. Addition and multiplication each take two elements of R and return an element of R.
- a+(b+c)=(a+b)+c,∀a,b,c∈R
Additive associativity. Parentheses do not change a sum of three elements.
- ∃0∈R such that a+0=a=0+a,∀a∈R
Additive identity. Adding 0 leaves every element unchanged, from either side.
- ∀a∈R, ∃−a∈R such that a+(−a)=0=(−a)+a
Additive inverse. Every element has an additive inverse that returns the additive identity 0 from either side.
- a+b=b+a,∀a,b∈R
Additive commutativity. Addition ignores order. Together, the previous four addition rules say that (R,+) is an abelian group.
- a⋅(b⋅c)=(a⋅b)⋅c,∀a,b,c∈R
Multiplicative associativity. Parentheses do not change a product of three elements.
- a⋅(b+c)(a+b)⋅c=a⋅b+a⋅c=a⋅c+b⋅c,∀a,b,c∈R
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,+,⋅): 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.5, which is outside Z. That is why the integers form a ring, but not a field.
Integer line
Operations
Sequence
No ring operations yet.
Now take the same ring idea and bend it into a 12-hour clock. This ring is (Z12,+,⋅): 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
Operations
Sequence
No clock ring operations yet.
Now narrow the integer line to only the even integers. This set is 2Z: 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 2Z. Since 1 is not an even integer, 2Z has no multiplicative identity. Some definitions of rings require a ring to have one, and some allow rings without one.
Even integers line
Operations
Sequence
No even-integer operations yet.
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], every polynomial has integer coefficients, so adding or multiplying polynomials simply combines integers and still produces another polynomial in the same ring.
The variable x acts as a symbolic placeholder rather than a specific number. The structure comes from the coefficients and the rules for combining powers of x.
Polynomials
Operation
Algebraic structures
Fields
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 0.
That extra reversibility is the key difference from rings like Z. In the integers, 3÷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 F is a non-empty set, + is a binary operation on F, and ⋅ is a binary operation on F, then (F,+,⋅) is called a field if:
- (F,+,⋅) is a commutative ring
Ring structure. All ring axioms hold, and multiplication is commutative: a⋅b=b⋅a for all a,b∈F.
- ∃1F∈F with 1F=0 such that 1F⋅a=a=a⋅1F,∀a∈F
Multiplicative identity. The element 1F leaves every element unchanged under multiplication, from either side, and it is different from 0.
- ∀a∈F, a=0⇒∃a−1∈F such that a⋅a−1=1F=a−1⋅a
Multiplicative inverse. Every non-zero element has an inverse inside F. This is the rule that makes division possible inside the field.
Equivalently, the addition part (F,+) is an abelian group, the non-zero elements of F form an abelian group under multiplication, and multiplication distributes over addition.
Field properties
1. Uniqueness of inverse. If a∈F and a=0, then the inverse a−1 is unique.
So the two supposed inverses are equal. Therefore there cannot be two different inverses of a; the inverse is unique.
2. Multiplication by zero. If a∈F, then a⋅0=0.
3. No zero divisors. If a,b∈F and a⋅b=0, then a=0 or b=0.
Now suppose a⋅b=0. If a=0, we are done. If a=0, then:
Therefore a product in a field can be 0 only when at least one of its factors is 0.
4. Distinctiveness of sums and products. If a,b,c∈F and b=c, then adding the same element keeps the results distinct:
Suppose instead that a+b=a+c. Add −a to both sides:
This contradicts b=c. So a+b and a+c are distinct.
For products, if a=0 and b=c, then:
Suppose instead that a⋅b=a⋅c. Multiply both sides by a−1:
Again this contradicts b=c. So multiplying by a non-zero element also preserves distinctness.
Field examples
The rational numbers Q are all fractions qp where p,q∈Z and q=0. This field is (Q,+,⋅): adding, subtracting, multiplying, and dividing by a non-zero rational number all stay inside Q.
Rational numbers
A
B
Operation
The real numbers R fill the continuous number line. They include rational numbers, numbers like 2 and π, and every decimal point location between them. This field is (R,+,⋅): adding, subtracting, multiplying, and dividing by a non-zero real number all stay inside R.
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), after Evariste Galois, a remarkably influential mathematician who helped lay the foundations of modern algebra before dying at just 20 years old.
GF(2) is the smallest finite field. It has 2 elements {0,1} and arithmetic is performed modulo 2.
| a + b (mod 2) | 0 | 1 |
|---|---|---|
| 0 | 0 | 1 |
| 1 | 1 | 0 |
| a x b (mod 2) | 0 | 1 |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 1 |
GF(3) is a finite field. It has 3 elements {0,1,2} and arithmetic is performed modulo 3.
| a + b (mod 3) | 0 | 1 | 2 |
|---|---|---|---|
| 0 | 0 | 1 | 2 |
| 1 | 1 | 2 | 0 |
| 2 | 2 | 0 | 1 |
| a x b (mod 3) | 0 | 1 | 2 |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 0 | 1 | 2 |
| 2 | 0 | 2 | 1 |
GF(4) cannot be built as four-step clock arithmetic on 0,1,a,b; watch where the field rules break.
| x + y (mod 4) | 0 | 1 | a | b |
|---|---|---|---|---|
| 0 | - | - | - | - |
| 1 | - | - | - | - |
| a | - | - | - | - |
| b | - | - | - | - |
| x * y (mod 4) | 0 | 1 | a | b |
|---|---|---|---|---|
| 0 | - | - | - | - |
| 1 | - | - | - | - |
| a | - | - | - | - |
| b | - | - | - | - |
Conditions to construct a finite field
The integers modulo n, Zn={0,1,2,…,n−1}, form a field if and only if n is prime.We prove the equivalence in two directions.
If n is prime
Take any non-zero element a∈Zn, a=0.
Thus x is a multiplicative inverse of a. Every non-zero element has an inverse.
If n is composite
Suppose n is composite, so n=ab with 1<a,b<n. View a and b as residues in Zn.
The elements a and b are zero divisors. That means both are non-zero in Zn, but their product is zero. A field cannot have this happen because every non-zero element must have an inverse. If a⋅b=0 and a−1 existed, multiplying by a−1 would force b=0.
More generally, a finite field can contain only a prime-power number of elements. Every finite field has size pm for some prime p and positive integer m. Conversely, a finite field exists for every such size.
The fields constructed using integers modulo a prime correspond to the simplest case m=1. These are called prime fields. When m>1, the construction is no longer just arithmetic modulo pm. The system Zpm={0,1,…,pm−1} contains zero divisors, so it cannot be a field. Indeed, p⋅pm−1=pm≡0(modpm) even though both factors are non-zero modulo pm. Therefore finite fields of size pm for m>1 must be constructed in a different way.
Calculation in prime fields
Elements are the integers 0,1,…,p−1.
Add, subtract, and multiply as usual — then reduce modulo p.
Division is multiplication by the modular inverse: a÷b=a⋅b−1(modp), found via the Extended Euclidean Algorithm.
Modulus space (mod 7)
Integers space
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
Message
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)
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
Message
S-box
P-box
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)
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) — 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.
Key
128 bits
Message
128 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:
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:
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:
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:
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.