Number Theory and Cryptography
Number Theory and Cryptography
Number theory studies the properties of integers, including divisibility, primes, and remainders. Cryptography uses mathematical methods to protect messages and verify who sent them. Many cryptographic methods depend on modular arithmetic: calculations in which numbers wrap around after reaching a fixed modulus.
The main ideas build on one another: divisibility leads to remainders and congruences; prime numbers and greatest common divisors help determine when modular equations can be solved; and these tools support algorithms, error-detection methods, and cryptographic systems.
Divisibility and Modular Arithmetic
Divisibility
For integers a and b, with a=0, a divides b if there is an integer c such that
The notation a∣b means that a divides b. If no such integer c exists, the notation is a∤b.
When a∣b:
a is a divisor or factor of b.
b is a multiple of a.
The quotient b/a is an integer.
For example, 3∣12 because 12=3⋅4, but 3∤7 because 7/3 is not an integer.
Basic divisibility properties
For integers a,b,c, with a=0:
If a∣b and a∣c, then a∣(b+c).
If a∣b, then a∣bc for every integer c.
If a∣b and b∣c, then a∣c.
For the first property, write b=as and c=at for integers s and t. Then
so a divides b+c.
A linear combination of b and c has the form mb+nc, where m and n are integers. If a divides both b and c, then it divides every linear combination of them:
This fact is used throughout number theory, particularly in arguments involving greatest common divisors.
The division algorithm
For any integer a and positive integer d, there is exactly one pair of integers q and r satisfying
The four quantities are:
Dividend: a, the number being divided.
Divisor: d, the positive number by which it is divided.
Quotient: q, written adivd.
Remainder: r, written amodd.
The remainder must be nonnegative and strictly smaller than the divisor. The requirement matters for negative dividends. For example,
so −11div3=−4 and −11mod3=1. The expression −11=3(−3)−2 is also an equation, but it does not give the permitted remainder because −2<0.
For a positive example,
so 101div11=9 and 101mod11=2.
Congruences
Let m be a positive integer. Integers a and b are congruent modulo m if m divides their difference:
The integer m is the modulus. Congruence means that the two integers have the same remainder when divided by m.
For example, 17≡5(mod6) because 17−5=12, which is divisible by 6. In contrast, 24≡14(mod6) because their difference, 10, is not divisible by 6.
The equivalence
holds for some integer k. Thus, congruent numbers differ by a whole-number multiple of the modulus. The value of k can be negative, corresponding to moving backward by steps of size m.
Congruence is different from the remainder function:
In a≡b(modm), “modulo m” describes a relation that is either true or false.
In amodm, the expression gives the remainder after division.
The two ideas are connected by
A clock provides a useful model. On a twelve-hour clock, 3 o’clock and 15 o’clock indicate the same position because their difference is 12:
Addition and multiplication of congruences
Congruences can be added and multiplied. If
then
and
In calculations, a number can therefore be replaced by any congruent number before adding or multiplying. For example, modulo 5, 7≡2 and 11≡1. Therefore,
and
Adding the same integer to both sides of a congruence, or multiplying both sides by the same integer, preserves the congruence.
Division is not generally safe. For example, 14≡8(mod6), but dividing both numbers by 2 gives 7 and 4, which are not congruent modulo 6. A precise condition for valid cancellation is given in the section on inverses and solving congruences.
Arithmetic modulo m
The set
contains one representative for each possible remainder modulo m. Arithmetic modulo $m means performing ordinary addition or multiplication and then taking the remainder modulo $m.
For example, in Z11,
and
The results always belong to Z11.
Modular arithmetic has many familiar properties:
Closure: Adding or multiplying two elements of Zm produces another element of Zm.
Associativity: The grouping of additions or multiplications does not affect the result.
Commutativity: The order does not affect the result: a+b=b+a and ab=ba.
Identities: 0 is the additive identity and 1 is the multiplicative identity.
Additive inverses: Every element has an additive inverse. For nonzero a in Zm, its inverse is m−a; zero is its own additive inverse.
Distributivity: Multiplication distributes over addition: a(b+c)=ab+ac.
Multiplicative inverses do not always exist. In Z6, no multiple of 2 is congruent to 1, so 2 has no multiplicative inverse modulo 6. The existence of an inverse depends on the greatest common divisor of the number and the modulus.
Integer Representations and Algorithms
Positional notation and bases
In a positional numeral system, each digit represents a multiple of a power of the base. In base ten,
Any base b>1 can be used. Every positive integer n has a unique base-b expansion
where each digit satisfies 0≤ai<b, and the leading digit ak is nonzero. A base is usually shown as a subscript; for instance, (1011)2 is a binary numeral.
The same written digits can represent different values in different bases. For example, (111)2=7 while (111)8=73.
Binary, octal, and hexadecimal
Binary is base two and uses the digits 0 and 1. Its place values, from right to left, are
Each place is a power of two. A binary numeral is converted to decimal by adding the place values that have a digit of 1. For example,
Computers commonly represent data in binary because electronic components can represent two distinct states.
Octal is base eight and uses the digits 0 through 7. Its place values are powers of eight: 1,8,64,512,…. For example,
Hexadecimal is base sixteen. It uses the digits 0 through 9 and the letters A through F, with
Its place values are powers of sixteen. For example,
Base choices also appear in history and everyday measurement: ancient Maya used base twenty, and ancient Babylonians used base sixty. The latter is reflected in the sixty minutes per hour and sixty seconds per minute.
Converting from decimal to another base
Repeated division converts a positive integer n to base b:
Divide n by b. Record the remainder; it is the rightmost digit.
Divide the quotient by b and record that remainder as the next digit.
Continue dividing each new quotient by b.
Stop when the quotient is zero.
Read the remainders in reverse order, from last to first.
For example, converting 12345 to base eight gives these successive remainders:
Reading the remainders backward gives (30071)8. Checking confirms that
Direct conversion between binary, octal, and hexadecimal
Because 8=23, one octal digit corresponds exactly to a group of three binary bits. Because 16=24, one hexadecimal digit corresponds to four binary bits. Conversion between these bases can therefore be done by regrouping bits rather than by performing full arithmetic.
For binary-to-octal conversion, group bits in threes starting from the right. Add leading zeros if necessary.
For binary-to-hexadecimal conversion, group bits in fours starting from the right. Again, add leading zeros if necessary.
Convert each group into its corresponding octal or hexadecimal digit.
For example, grouping a binary string into blocks of three yields one octal digit per block; grouping the same bits into blocks of four yields hexadecimal digits. Leading zeros used to complete the first group do not change the number’s value.
Binary addition
A computer can add binary numbers one bit at a time, moving from right to left. At each position, it adds the two input bits and the carry from the previous position. If the total is u, the output bit is the remainder when u is divided by 2, and the new carry is the quotient:
A final carry becomes a new leading bit. Adding two n-bit integers takes O(n) bit operations because each of the n positions requires a fixed amount of work.
Binary multiplication
Binary multiplication uses shifted partial products:
For each bit of the multiplier, produce a partial product.
If the bit is 1, shift the multiplicand left by that bit’s position. A shift left by j positions multiplies the number by 2j.
If the bit is 0, the partial product is zero.
Add the partial products.
There are n partial products for an n-bit multiplier, and adding each one takes roughly O(n) bit operations. The resulting multiplication algorithm takes O(n2) bit operations.
Fast modular exponentiation
Cryptographic computations often require finding bn(modm) when n and m are large. Repeatedly multiplying by b would require roughly n multiplications. Repeated squaring instead uses the binary expansion of the exponent.
If
then
The powers b,b2,b4,b8,… are obtained by repeatedly squaring. Only the terms corresponding to 1 bits in the exponent are multiplied together.
For example, 11=(1011)2=8+2+1, so
Repeated squaring obtains 32=9, 34=81, and 38=6561, after which the selected powers are multiplied. This requires only a number of multiplications proportional to logn, rather than n.
For modular exponentiation, the remainder is taken after each multiplication. Intermediate values therefore stay below m. The process is essential in public-key cryptography because it makes enormous modular powers practical to compute.
Primes and Greatest Common Divisors
Prime and composite numbers
A prime is an integer greater than 1 whose only positive divisors are 1 and itself. An integer greater than 1 that is not prime is composite. The number 1 is neither prime nor composite.
Examples:
7 is prime because no integer strictly between 1 and 7 divides it.
9 is composite because 9=3⋅3.
Unique prime factorization
Every integer greater than 1 can be written as a product of primes, and this factorization is unique up to the order of the factors. This is the Fundamental Theorem of Arithmetic. Primes can be thought of as the basic building blocks of the positive integers.
Examples include
The number 641 is prime; testing prime divisors up to 641 is sufficient to establish this.
The Sieve of Eratosthenes and trial division
The Sieve of Eratosthenes finds all primes up to a chosen bound:
List the integers from 2 to the bound.
Keep 2 and cross out its larger multiples.
Continue with the next number not crossed out, crossing out its larger multiples.
Stop when the numbers used for crossing out exceed the square root of the bound.
The numbers left uncrossed are prime.
For numbers up to 100, only the primes 2,3,5,7 are needed, because 100=10. The primes left are the primes from 2 through 97.
The reason the sieve can stop at the square root is that every composite number n has a prime divisor no larger than n. If n=ab and both factors were greater than n, their product would exceed n.
The same fact gives trial division: to test whether n>1 is prime, test possible divisors from 2 through n. If none divides n, then n is prime. This method is correct but impractical for very large numbers because the number of trial divisors grows dramatically.
Infinitely many primes
Euclid’s proof that there are infinitely many primes uses contradiction. Suppose there were only finitely many, listed as p1,p2,…,pn. Form
The integer q is either prime or has a prime divisor. None of the listed primes can divide q, because each would then divide both p1p2⋯pn and q, and so would divide their difference, 1. Thus q has a prime divisor missing from the alleged complete list, a contradiction. Therefore, there are infinitely many primes.
Special forms and open questions about primes
A Mersenne prime has the form 2p−1, where p is prime, and the resulting number is itself prime. For instance, 22−1=3, 23−1=7, and 25−1=31 are prime, while
is not. A prime exponent does not guarantee a Mersenne prime. Mersenne numbers are important partly because specialized fast tests exist for this form.
The Prime Number Theorem estimates the number of primes up to a large number x as approximately
Equivalently, a randomly chosen positive integer near n has probability roughly 1/lnn of being prime. Primes become less frequent as numbers grow, but they do not stop appearing.
No nonconstant polynomial with integer coefficients produces a prime for every integer input. A polynomial can produce many prime values in a row and still eventually fail. For example,
is prime for n=1 through 40, but at n=41 it equals 412, which is composite. This illustrates the difference between evidence and proof.
Important conjectures remain unresolved:
Goldbach’s conjecture: Every even integer greater than 2 is the sum of two primes. It has been checked computationally over an enormous range, but no general proof is known.
Infinitely many primes of the form n2+1: This is believed to be true but has not been proved. A weaker result establishes that infinitely many such values are prime or products of at most two primes.
Twin prime conjecture: There are infinitely many prime pairs that differ by 2, such as 3 and 5 or 11 and 13. No proof is known.
Greatest common divisor and relative primality
A common divisor of two integers divides both. Their greatest common divisor, written gcd(a,b), is the largest positive common divisor.
For example, the divisors shared by 24 and 36 include 1,2,3,4,6,12, so
Since 17 is prime and does not divide 22, the only common positive divisor of 17 and 22 is 1:
Two integers are relatively prime or coprime if their greatest common divisor is 1. A list is pairwise relatively prime if every pair in it is relatively prime. It is not enough for the list as a whole to have no single factor shared by every entry; every pair must be checked. For example, 10,17,21 are pairwise relatively prime, but 10,19,24 are not because gcd(10,24)=2.
Finding the greatest common divisor from prime factorizations
If the prime factorizations of a and b are known, the greatest common divisor is formed by taking each prime to the smaller of its exponents in the two factorizations. A missing prime has exponent zero.
For example,
Taking the smaller exponents gives
This method can be inefficient for large numbers because finding prime factorizations is difficult.
Least common multiple
The least common multiple, written lcm(a,b), is the smallest positive integer divisible by both a and b. From prime factorizations, it is found by taking each prime to the larger of its exponents.
For example, if the factorizations are 23⋅35⋅72 and 24⋅33, then
For positive integers,
For each prime, the gcd uses the smaller exponent and the lcm uses the larger one; together, these are the two exponents that occur in the product ab.
The Euclidean algorithm
The Euclidean algorithm computes a greatest common divisor without factoring. If
then
It repeatedly replaces the pair by the divisor and remainder until the remainder is zero. The last nonzero remainder is the greatest common divisor.
For example,
The last nonzero remainder is 7, so gcd(287,91)=7.
The key reason the algorithm works is that the pairs (a,b) and (b,r) have exactly the same common divisors. If a number divides both a and b, it divides a−bq=r. Conversely, if it divides both b and r, it divides bq+r=a.
The algorithm terminates because each remainder is a nonnegative integer smaller than the previous divisor. Such a sequence cannot decrease forever. Its running time is logarithmic in the size of the smaller input, so it is efficient even for very large integers.
Bézout’s identity
For positive integers a and b, there are integers s and t such that
This is Bézout’s identity. The integers s and t are called Bézout coefficients and may be negative. For example,
so −2 and 1 are Bézout coefficients for 6 and 14.
The coefficients can be found by two passes through the Euclidean algorithm:
Forward pass: Write down all divisions in the Euclidean algorithm until the gcd is reached.
Backward pass: Start with the last nonzero remainder and repeatedly substitute earlier remainder expressions until the gcd is written as a linear combination of the original inputs.
For example,
The last nonzero remainder is 18. Back-substitution gives
The extended Euclidean algorithm carries the coefficients along during the forward pass, avoiding a separate backward pass.
Consequences of Bézout’s identity
If gcd(a,b)=1 and a∣bc, then a∣c. Bézout’s identity gives integers s,t such that sa+tb=1. Multiplying by c gives
Both terms on the left are divisible by a: the first contains a factor of a, and the second is divisible by a because a∣bc. Therefore, a∣c.
A related fact applies to primes: if a prime p divides a product a1a2⋯an, then p divides at least one factor. This property is important in proving the uniqueness of prime factorization.
Uniqueness of prime factorization
To see why prime factorization is unique, suppose an integer had two different prime factorizations. Cancel all primes appearing in both lists. A prime remaining on one side divides the product of the primes on the other side. By the prime-divides-a-product property, it must divide one of those primes. Since a prime’s only positive divisors are 1 and itself, the two primes must be equal, contradicting the cancellation of all shared primes. Therefore, once primes are arranged in order, the factorization is unique.
Cancelling in a congruence
Suppose
and gcd(c,m)=1. Then c can be cancelled:
The coprimality condition is essential. The earlier example 14≡8(mod6) can be written as 2⋅7≡2⋅4(mod6), but 2 cannot be cancelled because gcd(2,6)=2. When the common factor of c and m is 1, Bézout’s identity supplies the divisibility argument that makes cancellation valid.
Solving Congruences
Linear congruences and modular inverses
A linear congruence has the form
where x is unknown. If x is a solution, then x+km is also a solution for every integer k, so solutions occur in congruence classes.
A multiplicative inverse of a modulo m is an integer aˉ satisfying
An inverse acts like a reciprocal in modular arithmetic. It allows a coefficient to be removed from a congruence by multiplying both sides by the inverse; direct division is not generally safe.
An inverse exists exactly when
When it exists, it is unique modulo m. Bézout’s identity explains why: if gcd(a,m)=1, then there are integers s,t with
Reducing modulo m removes the multiple tm and gives sa≡1(modm), so s is an inverse of a.
For example, the Euclidean algorithm gives
so 1=7−2(3). Modulo 7, this means (−2)3≡1(mod7). Thus −2 is an inverse of 3 modulo 7, and its representative in Z7 is 5. Indeed, 5⋅3=15≡1(mod7).
To solve 3x≡4(mod7), multiply both sides by the inverse −2:
Thus every solution is congruent to 6 modulo 7.
An inverse does not always exist, and a linear congruence can have no solution or several solution classes. For example, 2x≡3(mod6) has no solution because the left side is always even modulo 6. In contrast, 2x≡2(mod6) has two solution classes: x≡1(mod6) and x≡4(mod6).
Finding an inverse by the extended Euclidean algorithm
To find the inverse of a modulo m:
Check that gcd(a,m)=1.
Run the Euclidean algorithm on a and m.
Use back-substitution to express 1 as a linear combination of a and m.
Reduce that equation modulo m. The coefficient of a is an inverse.
For example, to find the inverse of 101 modulo 4620, the Euclidean algorithm ends with gcd 1. Back-substitution gives
Modulo 4620, the term −35(4620) is congruent to zero. Therefore,
so 1601 is an inverse of 101 modulo 4620.
The Chinese Remainder Theorem
The Chinese Remainder Theorem solves a system of congruences with pairwise relatively prime moduli. If m1,…,mn are pairwise relatively prime, then for any target remainders a1,…,an, the system
has exactly one solution modulo
“Unique modulo M” means there is one representative from 0 through M−1; every other integer solution differs from it by a multiple of M.
A constructive proof also gives a method for finding the solution:
Compute M=∏kmk.
For each k, define Mk=M/mk.
Since the moduli are pairwise relatively prime, Mk is relatively prime to mk.
Find yk, the inverse of Mk modulo mk.
Form $$ x=\sum_k a_kM_ky_k. $$
Modulo mk, every term other than akMkyk is zero because it contains the factor mk. The remaining term is congruent to ak, so the constructed x satisfies every congruence.
For the system
the product modulus is M=105. The corresponding values are M1=35, M2=21, and M3=15. Their inverses modulo the respective moduli are 2,1,1. Thus,
The value 23 leaves remainders 2,3,2 upon division by 3,5,7, respectively. It is the smallest positive solution, and all solutions are congruent to 23 modulo 105.
Back-substitution for systems
A second way to solve a system is to turn one congruence at a time into an equation and substitute into the next. The equivalence
for an integer t makes this possible.
For example, begin with x≡1(mod5), so write x=5t+1. Substituting into x≡2(mod6) gives 5t≡1(mod6). Since 5 is its own inverse modulo 6, t≡5(mod6), so write t=6u+5. Then x=30u+26. Substitution into x≡3(mod7) gives 2u+5≡3(mod7), so u≡6(mod7). Writing u=7v+6 gives
and therefore x≡206(mod210).
Fermat’s Little Theorem
If p is prime and p∤a, Fermat’s Little Theorem states that
A related form, valid for every integer a, is
The theorem simplifies large exponents. For example, modulo 11, 710≡1. Since 222=22(10)+2,
Pseudoprimes and primality testing
Fermat’s theorem can produce a useful compositeness test. If n>2 and
then n is definitely composite. If the congruence holds, however, it does not prove that n is prime. A composite number that passes the test for a particular base is a pseudoprime to that base.
For example, 341=11⋅31 is composite, but
Because 340=34(10), this implies 2340≡1(mod341), so 341 is a pseudoprime to base 2.
Testing several bases makes a false positive less likely. Pseudoprimes are much less common than primes over many ranges, but these tests provide evidence rather than a proof of primality.
Primitive roots and discrete logarithms
For a prime p, a primitive root modulo $p is a number r whose powers produce every nonzero element of Zp before repeating. It is a generator of the nonzero residues modulo p.
For example, the powers of 2 modulo 11 are
They include every nonzero residue, so 2 is a primitive root modulo 11. By contrast, the powers of 3 modulo 11 return to 1 after five powers and reach only five nonzero values, so 3 is not a primitive root.
Every prime has at least one primitive root. If r is a primitive root and a is nonzero modulo p, there is a unique exponent e modulo p−1 such that
This exponent is the discrete logarithm of a to the base r modulo p. For example, modulo 11 with base 2, the discrete logarithm of 3 is 8, because 28≡3(mod11).
Computing a modular power from a known exponent is efficient using repeated squaring. Recovering the exponent from the result is generally believed to be difficult for large parameters. This asymmetry is useful in cryptography.
Applications of Congruences
Hashing
A hashing function maps a record’s key to a location in a table. A simple hash function is
where m is the number of locations. Its output lies between 0 and m−1.
Different keys can map to the same location; this is a collision. Collisions are unavoidable when the set of possible keys is larger than the table.
One simple collision-handling method is linear probing. If the location h(k) is occupied, test
until an open location is found. The modular reduction makes the search wrap from the end of the table to the beginning.
Pseudorandom numbers
A computer follows deterministic instructions, so a sequence generated by a fixed rule is not truly random. It may nevertheless appear random; such numbers are called pseudorandom.
A common method is the linear congruential generator:
where m is the modulus, a the multiplier, c the increment, and x0 the seed. Each output lies between 0 and m−1. Dividing outputs by m produces values between 0 and 1.
For example, with m=9, a=7, c=4, and x0=3, the sequence is
The sequence repeats after nine outputs. The period is the number of outputs before the sequence begins repeating. A useful generator needs a long period. If c=0, the generator is pure multiplicative; a zero seed then produces zero forever. Some parameter choices can give very long periods, but the quality of a generator depends on more than its period.
Check digits
A check digit is an extra digit chosen so that a weighted sum of the digits satisfies a modular condition. When a digit is mistyped, the weighted sum often changes and the error is detected. A check digit can reveal that a string is invalid without necessarily identifying or correcting the error.
Universal Product Code
A twelve-digit Universal Product Code uses the last digit as its check digit. For the first eleven digits:
Digits in odd-numbered positions are multiplied by 3.
Digits in even-numbered positions are used as they are.
The check digit is chosen so the total, including that digit, is congruent to 0 modulo 10.
For example, if the weighted sum of the first eleven digits is 98, then the check digit is 2, because 98+2=100, a multiple of 10. The alternating weights also help detect transpositions of neighboring digits.
ISBN-10
An ISBN-10 uses weights 1 through 10 on its ten digits and checks whether
where di is the digit in position i. The final check symbol may be X, which represents 10.
If the first nine digits contribute 189 to the weighted sum, the check digit d10 must satisfy
Since 189≡2(mod11) and 10≡−1(mod11), this gives d10=2. If the required check symbol is 10, it is written as X.
The prime modulus 11 makes ISBN-10 particularly effective at detecting single-digit errors and transpositions. The weighted sum changes in a way that cannot be cancelled by a nonzero weight modulo a prime.
Cryptography
The Caesar cipher and shift ciphers
A cipher transforms a plaintext message into ciphertext so that only someone with the appropriate key can recover it. Encryption creates the concealed message; decryption reverses the transformation.
The Caesar cipher shifts each letter three positions forward, wrapping from the end of the alphabet to the beginning. To express it mathematically, encode letters as numbers from 0 to 25 and encrypt a letter value p by
For example, Y is 24, so it becomes (24+3)mod26=1, or B. Decryption shifts back:
A shift cipher generalizes the Caesar cipher by using a key k:
There are only 26 possible keys, so an attacker can try every key. This is a brute-force attack.
Affine ciphers
An affine cipher encrypts a letter value p by
The cipher must be one-to-one so that decryption is possible. This happens exactly when
For example, a=7 and b=3 give a valid cipher because gcd(7,26)=1. The letter K has value 10, so it encrypts to
which is V.
To decrypt, solve c≡ap+b(mod26). Since a has an inverse aˉ modulo 26,
The condition that makes the affine map invertible is the same condition that guarantees the needed modular inverse.
Cryptanalysis and frequency analysis
Cryptanalysis is the analysis of ciphertext to recover a message or key without being given the key. In English, some letters occur more frequently than others; E is especially common.
A shift cipher preserves the pattern of letter frequencies while shifting the letters. An attacker can:
Count the occurrences of each ciphertext letter.
Guess that the most common ciphertext letter corresponds to E.
Infer a candidate shift.
Decrypt and check whether the result resembles English.
Try another likely correspondence if the first guess fails.
For example, if K is the most common ciphertext letter and is assumed to represent E, the shift is 6. Shifting the ciphertext backward by 6 can reveal a message such as “THE EARLY BIRD GETS THE WORM.”
Ciphers that substitute one fixed ciphertext symbol for each plaintext symbol are monoalphabetic. Their letter-frequency patterns remain visible, making frequency analysis effective.
Transposition ciphers and block ciphers
A transposition cipher rearranges letter positions without changing the letters. Its key is a permutation of the positions in a fixed-size block. To encrypt, divide the message into blocks, rearrange each block according to the permutation, and pad the final block if needed. Decryption uses the inverse permutation.
A transposition cipher changes the order of letters but preserves every single-letter frequency exactly. Therefore, block processing alone does not guarantee protection from frequency analysis. More generally, block ciphers transform groups of symbols together, so a plaintext letter need not always map to the same ciphertext letter.
Cryptosystems
A cryptosystem can be described by five components:
P: the set of plaintext messages.
C: the set of ciphertext messages.
K: the keyspace, the set of possible keys.
E: the set of encryption functions.
D: the set of decryption functions.
For a key k, encryption is written Ek and the corresponding decryption is written Dk. A basic correctness requirement is
for every plaintext p: decrypting an encrypted message must recover the original.
In a shift cipher, messages are strings of values in Z26, the key is a shift k∈Z26, encryption adds k modulo 26, and decryption subtracts k modulo 26.
Private-key and public-key cryptography
In private-key cryptography, the sender and receiver share a secret key. Knowing the encryption key generally makes it possible to find the decryption key, so both parties must protect the shared secret and arrange to exchange it securely.
Public-key cryptography separates the keys. The encryption key can be made public, allowing anyone to encrypt a message for its owner, while the owner keeps the decryption key secret. The design aims to make encryption easy but decryption infeasible without the private key.
RSA
RSA is a public-key cryptosystem based on modular exponentiation and the difficulty of factoring large integers.
Key generation
Choose two distinct large primes p and q.
Compute n=pq.
Choose an exponent e relatively prime to $$ (p-1)(q-1). $$
Find d, the inverse of e modulo (p−1)(q−1): $$ ed\equiv1\pmod{(p-1)(q-1)}. $$
The public key is (n,e); the private key includes d. The primes p and q must also remain secret.
Encryption and decryption
A plaintext block M is encrypted by
It is decrypted by
Messages are encoded as numbers and split into blocks smaller than n. In a simple letter encoding, each letter can be represented by a two-digit number; the blocks must be short enough to remain below the modulus.
A small demonstration uses p=43 and q=59, so n=2537, with public exponent e=13. Here
Since 13 is relatively prime to 2436, it has an inverse modulo 2436; that inverse is d=937. In an actual RSA system, the primes and modulus are much larger. The small example is for illustration only.
Large powers are computed by repeated squaring, reducing modulo n at each step. The full, enormous integer power is never constructed.
Why RSA is intended to be secure
Multiplying two large primes is efficient, and finding large probable primes is practical. Given n=pq, recovering p and q by factoring is believed to be difficult for appropriately large n. If an attacker can factor n, the attacker can compute (p−1)(q−1) and then find d. The security claim depends on the practical difficulty of factoring; it is not a proof that factoring is impossible.
Diffie–Hellman key exchange
The Diffie–Hellman protocol lets two parties establish a shared key over a public channel:
Alice and Bob publicly agree on a prime p and a primitive root a modulo p.
Alice chooses a private exponent k1 and sends $$ a^{k_1}\pmod p. $$
Bob chooses a private exponent k2 and sends $$ a^{k_2}\pmod p. $$
Alice raises Bob’s value to k1; Bob raises Alice’s value to k2.
Both obtain the same key because
An eavesdropper sees the public parameters and the two exchanged powers, but recovering a private exponent from one of those powers requires solving a discrete logarithm problem. For sufficiently large parameters, this is intended to be computationally difficult.
Digital signatures
A digital signature aims to establish authenticity and integrity rather than confidentiality. A receiver should be able to verify that a message was signed by the holder of a particular private key.
In a simplified RSA signature scheme, a sender uses the private exponent d to transform a message block, and anyone can apply the public exponent e to verify that the original block is recovered. The fact that the private key is required to produce a valid transformation supports the claim that the holder signed it.
This raw method is only a teaching model. Signing message blocks directly is insecure: an attacker can choose a number s and compute se(modn), producing a pair that passes the basic RSA check. Real systems instead sign a carefully designed hash of the message using secure signature padding. Hashing binds the signature to the message while avoiding the vulnerabilities of raw textbook RSA.

Ask anything
We've read this whole study set, so questions about it get the best answers.