Cryptography
Cryptography is an essential feature of modern life. The field of cryptography aims to ensure the confidentiality, integrity, and authenticity of information by employing robust encryption techniques. Cryptography is used in almost all modern communication systems.
Our goal in this lecture is to be able to use mathematics to communicate securely over an insecure network.
Along the way we will learn about some key mathematical concepts, including:
- Modular arithmetic
- Classical cryptography
- Public Key cryptography
Learning Outcomes
- Solve equations involving the modulus operation
- Evaluate whether a congruence holds under modulus
- Perform addition under modulus
- Perform subtraction under modulus
- Calculate the additive inverse of a number under modulus
- Evaluate simple encryption and decryption cypher functions, such as those for caesar and atbash
- Define prime numbers
- Find prime factors of a number
- Find the greatest common denominator of two numbers
- Perform multiplication under modulus
- Apply division in modular arithmetic and calculate the multiplicative inverse of a number under modulus
- Perform division under modulus
- Encrypt and decrypt strings using affine cypher
- Encrypt and decrypt strings by creating a one time pad
- Encrypt and decrypt using a simple stream cypher by evaluating provided random number generator
- Describe the basic ideas of public key cryptography
- Evaluate both sides of a Diffie-Hellman Key Exchange
Introduction
A key tool in mathematical cryptography is modular arithmetic. Modular arithmetic is also widely used for computer science.
Modular arithmetic is useful for cyclic systems, such as calendars. It is useful for cryptographic systems where you are rotating values within a fixed alphabet. The computational properties of exponentiation and logarithms in modular arithmetic (i.e. it is easy to compute the exponential but hard to compute the logarithm) make it useful for public-key cryptography. Public-key cryptography is essential in securing the modern Internet.
Modular arithmetic is also useful when we are dealing with systems that can only represent a finite set of numbers and 'overflow' if the numbers get too big, such as storing numbers as a fixed number of bits in a computer.
Overview
Our goal is to be able to understand the necessary mathematics to communicate securely over an insecure channel. We will work towards our goal step by step.
First we will look at classical cryptography and understand how to mathematically represent a basic cypher. We will learn modular arithmetic in order to encrypt and decrypt using the Caesar and Affine cyphers.
We will then turn to modern cryptography. We will learn how to use one-time pads and stream cyphers and pseudorandom number generators
Finally, we will address the problem of securely agreeing on the keys for our encryption when an eavesdropper is listening to everything we say. To do this, we will see the example of Diffie Hellman key exchange.
Classical Cryptography
Classical cryptography refers to the traditional methods of encryption and decryption that were practised before modern computer-based methods.
Classical cryptography has a long and rich history, with roots dating back thousands of years. Early civilizations, such as the Egyptians, Greeks, and Romans, developed rudimentary methods of encryption to protect messages from being intercepted. These methods often involved simple substitution ciphers, where each letter or symbol in the plaintext was replaced with a different letter or symbol in the ciphertext according to a predefined rule or key.
During World War II, the Enigma machine was used by the German military to encrypt their communication. The Enigma machine employed rotor-based encryption, creating a complex and constantly changing substitution cipher. Although the Enigma machine was initially considered unbreakable, it was eventually deciphered by the efforts of cryptanalysts, most notably the British codebreakers at Bletchley Park, led by Alan Turing.
Classical cryptography laid the foundation for modern cryptographic techniques. While classical methods are no longer considered secure for protecting sensitive information against sophisticated attacks, they hold historical significance and provide insights into the evolution of cryptographic principles. The study of classical cryptography helps in understanding the challenges faced by early cryptographers and the subsequent advancements that led to the development of stronger encryption algorithms and protocols in the modern era.
Atbash
Let's start by considering a simple cypher. Indeed, one of the simplest.
Atbash is a simple cypher dating from biblical times. It was originally used to encrypt the Hebrew alphabet. Its function is simple. Each letter is replaced the letter found by reversing the alphabet. a
is replaced by z
, b
is replaced by y
, and so on, as shown in the table below:
Plaintext | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Ciphertext | Z | Y | X | W | V | U | T | S | R | Q | P | O | N | M | L | K | J | I | H | G | F | E | D | C | B | A |
Atbash is one of a class of cyphers called monoalphabetic substitution cyphers, because each letter is always substituted for the same letter, wherever it occurs in the plaintext.
Atbash, as you can work out for yourself, is reversible. By applying the same process to an encrypted word, the plaintext is recovered. Thus if I was to give you the word:
Cyphertext = shibboleth
Plaintext | h | s | r | y | y | l | o | v | g | s |
---|---|---|---|---|---|---|---|---|---|---|
\( X \) | 7 | 18 | 17 | 24 | 24 | 11 | 14 | 21 | 6 | 18 |
\( 25-X \) | 18 | 7 | 8 | 1 | 1 | 14 | 11 | 4 | 19 | 7 |
Cyphertext | s | h | i | b | b | o | l | e | t | h |
To study such cyphers mathematically, it can be helpful to express them as a mathematical function. To do so, we represent each letter of the alphabet is a number. a
is 0, b
is 1, c
is 3, etc. For the present cypher, we will call the value of a given letter $X$. We will express the encrypted form as the result of the function $E(X)$.
Thus the encrypted form of a
, which is represented by the value 0, is $E(0) = 25 - 0$. 25 is the value that represents z
, the last letter in the alphabet.
We could write any number of functions to encrypt text. Once we have decide on a way to convert plaintext symbols into numbers (e.g. by representing a
with 0, etc.) we can then perform any computations on these numbers that we like. However, not all computations are going to be equally useful as cyphers. For instance, while the cypher $E(X) = 0$ (replace all letters with 0 or a
) is unbreakable, the plaintext is completely unrecoverable!
Caesar Cypher
One problem that we quickly encounter when playing with cyphers is that we only have 26 letters in the alphabet (and any alternative alphabet we might choose is finite). The cypher text must fit within the range of values that can be represented with our alphabet.
The Caesar cypher is a simple shift cypher used by Julius Caesar in his private correspondence. To use it, find the value for each letter in the plain text and add a number, such as 3. This is the cypher's key. In order to prevent the letters at the end of the alphabet falling off the end, we will make the alphabet 'wrap around' through the use of the modulus operation $mod$. Thus the Caesar cypher can be written as the function:
With a key of $k=3$, the letter z
becomes c
. The 'wrapping' is achieved by the modulus operation.
Play with the tool below to see how changing the value of $k$ affects the cyphertext. Observe how the values 'wrap around' at the end of the alphabet due to the mod operation.
k =
Plaintext | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
\( X \) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
\( X + 3 \) | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 |
\( (X + 3) \mod 26 \) | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 0 | 1 | 2 |
Cyphertext | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z | a | b | c |
To understand the mathematics behind these sorts of cyphers we need to understand the mod operation and modular arithmetic.
Modulus Basics
We saw that to understand cryptography we need to understand modular arithmetic.
You will be familiar with performing arithmetic on a number line. The numbers run from negative infinity, past 0, to positive infinity. Now imagine we cut the number line so it only runs between 0 and a finite number, say 11. Now bend the number line into a circle. Now what happens if you start with the last number (here, 11), and add 1 more? You wrap around back to 0!
If this is hard to imagine, realise that the same idea applies when performing arithmetic on a 12-hour clock. For example, ten o'clock plus three hours is one o'clock, not thirteen o'clock.
When working under modulus, we only work with integer values from 0 (inclusive) to our modulus (exclusive). That is, we are performing arithmetic on a finite set of integers. For example, when we add numbers together, we do not always get a larger value because the values 'wrap around', as illustrated in the figure below:
+ () = 4
If we are performing modular arithmetic, we write the modulus we are using at the end in brackets. For example, a simple addition under modulus $5$ might be written:
The symbol $\equiv$ will be explained below.
Modulus Operation
You might have noticed there is a relationship between modular arithmetic and the remainder of a division. For example, if I were to perform the integer division of 12 and 4, I would find that the remainder is the same as the value of 12 under the modulus 4:
As I have done here, we can apply modulus as an operation when working in otherwise normal arithmetic. We do this using the $\\mod$ operation.
The modulus operation, written mod
(and written in most programming languages as %
) takes two arguments $a$ and $b$, and returns the remainder from dividing $a$ by $b$. Note that mathematically, mod
returns a positive integer, however most programming languages treat %
as the remainder, which can be negative and in some situations a decimal.
To find $x \mod m$ of any x, you can follow the following process:
- If \( x < 0 \), repeatedly add $m$ until \( 0 \leq x < m \)
- If \( x \geq m \), repeatedly subtract $m$ until \( 0 \leq x < m \)
Remember, your result will always be in the range \( 0 \leq x < m \)
Modular Addition
Calculate the following additions under modulus (without using the tool above).
Congruence
A key property of modular arithmetic is the equivalence of different (non-equal) numbers. You might have noticed the following when performing addition under modulus (if you didn't take a look at the questions above again):
- Under modulus $m$, the result of adding $x$ to a number is the same as adding $(x+m)$
In other words, if we start at a number, and count on the value of our modulus, we get back to the place we started! We can count on as many multiples of our modulus as we like, but every whole multiple of our modulus will get us back to the same place. This indicates a fundamental equivalence between different numbers in modular arithmetic.
Two numbers in our modular arithmetic $a$ and $b$ are equivalent (called congruent) if they differ by a multiple of the modulus (i.e. if $a-b = kx$). Congruence is written using the symbol $\\equiv$. We can express this 'getting back where you started' property with the formula:
It may be helpful to visualise this using the circle below. The modulus 12 is the set of natural numbers smaller than 12, including 0: \( \{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 \} \). The central circle below contains all these numbers. Outside are shown the first three different numbers that are congruent to each middle number under this modulus (though in principle this could be continued indefinitely). For example, 12, 24, and 36 are all congruent to 0 modulo 12.
This gives us sets of numbers called congruence classes. In the above there are $12 congruence classes, which we might label \( {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} \).
The congruence class $0$ is the set of numbers \( \{ 0, 12, 24, 36, 48, 60, 72, \dots \} \)
The congruence class $1$ is the set of number \( \{ 1, 13, 25, 37, 49, 61, 73, \dots \} \)
The congruence class $2$ is the set of number \( \{ 2, 14, 26, 38, 50, 62, 74, \dots \} \)
The congruence class $3$ is the set of number \( \{ 3, 15, 27, 39, 51, 63, 75, \dots \} \)
...and so on.
When we are working under modulus, the different elements of a congruence class are completely equivalent.
You could also think about modular arithmetic being as arithmetic on these classes, For example, under a modulus of 12, if you take something in the class \( 11 \) (\( \{11, 23, 35, \dots \} \)) and add something from the class $1$ (\( \{1, 13, 25, \dots \} \)) and you will get something from the class $0$ (\( \{0, 12, 24, \dots \} \)).
Modular Subtraction
We have already seen modular addition. Modular subtraction is similar.
To calculate \( a - b \pmod{m} \) we can first calculate $a-b$ and then use the mod operation. Remember the result will be non-negative.
(Note: The arrow below is drawn backwards. SVGs are hard to work with.)
- () = 0
\[ A - B \pmod{12} \]\[ 2 - 2 \pmod{12} \equiv 0 \]Additive Inverse
Subtraction can also be seen as the addition of the additive inverse of a number.
The additive inverse of a number is the number you need to add to $a$ to get 0. Usually this is straightforward. The additive inverse of 5 is -5 in normal arithmetic because 5 + (-5) = 0.
However when working under modulus, we do not have negative numbers! Still, because of the 'wrapping' effect, there exists an additive inverse for every number under modulus.
For all \( w \in \mathbb{Z}_n \) there exists a $z$ such that \( w + z \equiv 0 \pmod{n} \).
For example, the additive inverse of \( 5 \pmod{7} \) is \( 2 \) because \( 5 + 2 \equiv 0 \pmod 7 \).
Modular Subtraction Questions
Answer the following questions using additive inverses (without using the tool above).
Cracking the Caesar Cypher
A brute force attack is when you try every possible key.
Imagine we receive a password encrypted using the Caesar cypher that we want to decode. We do not know the key, but we might guess it is an English word.
vzrugilvk
As we saw above, the caesar cypher encryption function is
We can rearrange this formula to identify what the original letter must have been:
We can express this as a decryption function. This is purely a relabelling of the equation above. By comparing it to the encryption function you can see the subtraction operation is the inverse of the addition operation (and vice versa).
As we do not know the key, we can try all of the possibilities. As the alphabet contains 26 letters, our modulus is 26, thus there are only 26 possible keys (all other keys are congruent with one of these 26).
key | possible plaintext | key | possible plaintext |
---|---|---|---|
0 | vzrugilvk | 13 | imehtvyix |
1 | uyqtfhkuj | 14 | hldgsuxhw |
2 | txpsegjti | 15 | gkcfrtwgv |
3 | swordfish | 16 | fjbeqsvfu |
4 | rvnqcehrg | 17 | eiadpruet |
5 | qumpbdgqf | 18 | dhzcoqtds |
6 | ptloacfpe | 19 | cgybnpscr |
7 | osknzbeod | 20 | bfxamorbq |
8 | nrjmyadnc | 21 | aewzlnqap |
9 | mqilxzcmb | 22 | zdvykmpzo |
10 | lphkwybla | 23 | ycuxjloyn |
11 | kogjvxakz | 24 | xbtwiknxm |
12 | jnfiuwzjy | 25 | wasvhjmwl |
We find that the plaintext is an English word when $k=3$, so this is likely the original password.
CypherText | v | z | r | u | g | i | l | v | k |
---|---|---|---|---|---|---|---|---|---|
$X$ | 21 | 25 | 17 | 20 | 6 | 8 | 11 | 21 | 10 |
$(X - 3) \mod 26$ | 18 | 22 | 14 | 17 | 3 | 5 | 8 | 18 | 7 |
Plaintext | s | w | o | r | d | f | i | s | h |
A known plaintext attack is when you know (or guess) part of the original message (the plaintext).
We receive a message encrypted using the Caesar cypher that we want to decode.
l oryh brx
We might guess that this is an English sentence. We might further guess that the first word l
is an English word, such as "I" or "a". We will try "I" first.
Guessing that i
was encoded as l
, and knowing the caesar cypher encryption function is
We can substitute values of $X$ and $E(X)$ to solve for $k$. We know that i
= 8, which is what we assume $X$ to be, and l
= 11, which is what we assume $E(X)$ to be.
We can rearrange to find $k$:
Thus, if our guess that l
is i
is correct, the key would be $ k = 3$.
By decrypting using this key, we find what looks like the original message: i love you
CypherText | l | o | r | y | h | b | r | x | ||
---|---|---|---|---|---|---|---|---|---|---|
$X$ | 11 | 14 | 17 | 24 | 7 | 1 | 17 | 23 | ||
$(X - 3) \mod 26$ | 8 | 11 | 14 | 21 | 4 | 24 | 14 | 20 | ||
Plaintext | i | l | o | v | e | y | o | u |
Affine Cypher
Let us see a cypher that is a little more interesting than the Caesar cypher. The table below illustrates the Affine Cypher. This is another monoalphabetic substitution cypher, which is defined by the encryption function:
This cypher has two keys, $a$ and $b$.
Plaintext | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
\( X \) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 |
\( 3X + 3 \) | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 36 | 39 | 42 | 45 | 48 | 51 | 54 | 57 | 60 | 63 | 66 | 69 | 72 | 75 | 78 |
\( (3X + 3) \mod 26 \) | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 1 | 4 | 7 | 10 | 13 | 16 | 19 | 22 | 25 | 2 | 5 | 8 | 11 | 14 | 17 | 20 | 23 | 0 |
Cyphertext | d | g | j | m | p | s | v | y | b | e | h | k | n | q | t | w | z | c | f | i | l | o | r | u | x | a |
To encode this cypher we need to perform multiplication, which is straightforward, as you will see. However, decoding the cypher is a little more challenging. The inverse of multiplication is division, and modular division is rather counter-intuitive. First we will understand multiplication and then we will consider the inverse of multiplication, division.
Modular Multiplication
Multiplication is repeated addition, so other than the wrapping property we have already described, multiplication under modulus works as normal.
We can calculate \( a \times b \pmod{m} \) by first calculating $a \times b$, and then using the modulus operation.
x () = 4
Multiplicative Inverse
To decode the Affine cypher we will need to perform the inverse of multiplication, which is division.
The multiplicative inverse of a number $a$ is the number you need to multiply by $a$ to get 1. This can be notated $a^-1$.
Like additive inverse, this is straightforward for arithmetic on the real numbers (i.e. where we can have fractional answers). For instance, the multiplicative inverse of $ 5 $ is \( \frac{1}{5} \), because \( 5 \times \frac{1}{5} = 1 \).
However, when we are working under modulus $m$, we can only use those whole numbers that are permitted under our modulus (i.e. between 0 and $m-1$). This should be clear if you think about the Affine cypher. If we tried to decrypt text encoded with the affine cypher, and the inverse of multiplication gave us a decimal value, it would not correspond to a letter in our alphabet!
Recall that the multiplicative inverse is the number that you need to multiply by to get to 1. Under modulus, this number must be a whole number. It happens that this may in fact be larger than dividend because the wrapping effect can still lead to a result of 1.
For example, the multiplicative inverse of \( 5 \pmod7 \) is $3$, because $5 \times 3 \equiv 1 \pmod{7}$. This is defined because 5 and 7 are co-prime. Their greatest common denominator is 1.
Note that, under a modulus of $n$, the multiplicative inverse of a number $w$ is only defined if $n$ and $w$ are co-prime (i.e. the greatest common divisor of $w$ and $n$ is $1$). Otherwise it is undefined. We will come back to primeness and co-primeness of numbers later in the lecture.
In summary, for all \( w \in \mathbb{Z}_n \), where $w$ and $n$ are co-prime, the multiplicative inverse $a$ is the value such that:
Modular Division
Now we have worked out the modular multiplicative inverse, we can perform modular division, which will let us decrypt the affine cypher.
Observe that division by a number $w$ is the same as multiplication by the multiplicative inverse of $w$. For instance:
Thus to divide by a number $w$ under modulus $n$, we multiply by the multiplicative inverse of $w$, if it is defined. If the multiplicative inverse of $w$ is not defined (because $w$ and $n$ are not co-prime) then the division is not possible (its result is undefined).
When we want to perform the inverse of multiplication, which is division, we can instead multiply by the multiplicative inverse. We can apply this to decrypting the Affine cypher.
Affine Cypher Decryption
Imagine we receive a password encrypted using the Affine cypher that we want to decode.
crdwjb
The affine cypher encryption function is
We can rearrange this formula to identify what the original letter must have been.
We can express this as a decryption function. As you can see, the inverse of the multiplication operation is the multiplicative inverse.
Assuming we know the keys are $a=3$ and $b=1$, we can work out that $a^1 = 9$ (because $3 \\times 9 \\equiv 1 \\pmod26$) and evaluate the decryption function for each letter of the cyphertext.
Cyphertext | c | r | d | w | j | b |
---|---|---|---|---|---|---|
$X$ | 2 | 17 | 3 | 22 | 9 | 1 |
$3^-1(X - 1) \mod 26$ | 9 | 14 | 18 | 7 | 20 | 0 |
Plaintext | j | o | s | h | u | a |
Note that the key $a$ must be co-prime with the modulus $m$ for the decryption function to work. Thus it is essential when picking keys for the affine cypher to be able to check whether two numbers are co-prime.
Prime Numbers
As we have seen in calculating the multiplicative inverse, primeness and co-primeness are particularly important in modular arithmetic. Prime numbers are incredibly important for cryptography, so it is worth taking some time to understand them.
Number theory is a branch of mathematics that is interested in the patterns that we can observe in the numbers. Number theory most often deal with just the natural numbers. Considering the natural numbers, we might ask: are all numbers essentially the same, or do they have some interesting structure?
One way in which the natural numbers have structure is in their divisibility. Some numbers can be divided by other numbers and give a result that is itself a natural number. For instance, $6 \div 3 = 2$. Thus we might say that $2$ divides $6$ (written $2 \mid 6$). Note that not all numbers fit this relationship. For instance, $4$ does not divide $6$ and give a result in the natural numbers. On the other hand, $1$ divides every number. When considering divisibility, certain patterns start to emerge.
A prime number, or a prime, is a number that is only divisible by itself and $1$. For instance, $2$, $3$, $5$, and $7$ are prime numbers. There is no natural number $x$ such that $x \mid 7$, or any other prime for that matter. A prime number has no factors (other than 1 and itself), thus it is not possible to divide a prime number by a whole number to get a whole-numbered result. It's important to note that 1 is not considered to be a prime number.
Composite Numbers and Factor Trees
All other numbers are composite numbers, because they can be expressed as the product of some set of other numbers. Composite numbers can be visualised as a prime factor tree.
The number 56 can be expressed as 2 x 2 x 2 x 7, all prime numbers.
To draw a prime factor tree, check whether the value can be divided by any prime numbers, starting with 2 and working upwards. If you find that it can be divided by a prime, make this the first branch of your factor tree. One branch is the prime factor, the other is the result of dividing by this prime factor. Repeat the process until your final result is prime.
Sieve of Erathosthenes
The Sieve of Erathosthenes dates from the 3rd Century BC and is a method for finding primes.
- Write down some integers starting at 2.
- Start from the leftmost number, which is 2. Strike out all other numbers that are divisible by this number (i.e. 2)
- Go to the next number, skipping any that have been removed. Repeat the above step, striking out all other numbers that are divisible by it. Continue to apply rule for each number in turn.
- All numbers that survive to the end are primes.
(Note: this is still a bit buggy, I hope it gives the idea)
The full set of primes up to 20 is \( \{2, 3, 5, 7, 11, 13\} \)
Euclidean Algorithm
When using the Affine cypher, it is necessary to know whether two integers are co-prime. If you pick a key for the Affine cypher that is not co-prime with the modulus, the decryption function will not work. Two numbers are co-prime when their greatest common divisor is $1$.
The greatest common divisor (GCD) of two integers is the largest integer that divides both numbers.
The Euclidean Algorithm is a way of finding the GCD of two numbers $a$ and $b$. This algorithm is given as pseudocode below.
1
2
3
4
5
6
7
function Euclid(a: integer, b: integer): integer
{
if (b == 0)
return a;
else
return Euclid(b, a mod b);
}
a | b | b = 0 | a mod b |
---|---|---|---|
false | 4 | ||
14 | 4 | false | 2 |
4 | 2 | false | 0 |
2 | 0 | true | |
GCD | 2 |
Modern Cryptography
Classical cryptography relies on manual encryption methods, or - as in the case of the Enigma machine - mechanical devices. Modern cryptography leverages the power of computers and sophisticated mathematical algorithms to provide robust security. However, while this increases the security of modern cryptography, modern cryptography must also be resistant to attacks from adversaries with significant computational resources.
One-Time Pads
The concept behind one-time pads dates back to the early 20th century. The one-time pad is a form of encryption that provides theoretically perfect security when a perfectly random key is used. However, its strength and its weakness is the size of its key, which must be at least the same length as the message.
Say we had a message such as
the quick brown fox jumps over the lazy dog
To avoid revealing the lengths of words, it is common practice to write the message in 5-character blocks. Of course, we can also encode spaces as just another character. For how, however, we will continue to only think about encrypting letters of the alphabet
thequ ickbr ownfo xjump sover thela zydog
Now let us use a key that is the same length as the cypher. Here I have picked the first line of the William Gibson novel Neuromancer.
thesk yabov ethep ortwa sthec oloro ftele
The process of using a one-time pad is simple. As before, we replace each letter with a number, which we do to the plaintext and the key. $a=0$, $b=1$, etc. We then apply a simple function. To encrypt the $i$th letter of the plaintext, $P_i$, we simply add the $i$th letter of the key, $K_i$, and take the modulus.
Plaintext | t | h | e | q | u | i | c | k | b | r | o | w | n | f | o | x | j | u | m | p | s | o | v | e | r | t | h | e | l | a | z | y | d | o | g | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
19 | 7 | 4 | 16 | 20 | 8 | 2 | 10 | 1 | 17 | 14 | 22 | 13 | 5 | 14 | 23 | 9 | 20 | 12 | 15 | 18 | 14 | 21 | 4 | 17 | 19 | 7 | 4 | 11 | 0 | 25 | 24 | 3 | 14 | 6 | |||||||
Key | t | h | e | s | k | y | a | b | o | v | e | t | h | e | p | o | r | t | w | a | s | t | h | e | c | o | l | o | r | o | f | t | e | l | e | ||||||
19 | 7 | 4 | 18 | 10 | 24 | 0 | 1 | 14 | 21 | 4 | 19 | 7 | 4 | 15 | 14 | 17 | 19 | 22 | 0 | 18 | 19 | 7 | 4 | 2 | 14 | 11 | 14 | 17 | 14 | 5 | 19 | 4 | 11 | 4 | |||||||
Cyphertext | m | o | i | i | e | g | c | l | p | m | s | p | u | j | d | l | a | n | i | p | k | h | c | i | t | h | s | s | c | o | e | r | h | z | k | ||||||
12 | 14 | 8 | 8 | 4 | 6 | 2 | 11 | 15 | 12 | 18 | 15 | 20 | 9 | 3 | 11 | 0 | 13 | 8 | 15 | 10 | 7 | 2 | 8 | 19 | 7 | 18 | 18 | 2 | 14 | 4 | 17 | 7 | 25 | 10 |
To perform this on bits instead of letters, the XOR operation can be used. Any operation could be used so long as you can perform the inverse to recover the plaintext if you know the key. As we saw with the Caesar cypher, the inverse of addition is subtraction.
Note that for a one-time pad to be secure it is essential that the key is random (unlike here, where an English sentence was used). Also, the key must not used more than once. If either of these requirements are broken, an attacker could potentially break the encryption.
Stream Cypher
It is inconvenient to have to use a key the same length as the message. Both parties to the encryption - the sender and the receiver - need to have the same key. Thus if you want to send a gigabyte of data, you must already have shared a gigabyte-long key. This key must have been securely communicated. This is rarely practical.
Imagine you want to encrypt a mobile phone conversation. The amount of data encrypted could be very large, however you do not want to have to store gigabytes of key on the phone storage. For this purpose, a related approach called a stream cypher is used.
By generating the key using a pseudorandom number generator (PNRG), a smaller key can be used. This key is used to generate a pseudorandom keystream. This keystream is combined with the plaintext message in the same way as a one time pad. PRNGs produce deterministic output based on the seed, ensuring that the same seed will always produce the same sequence of random numbers. The steps are as follows:
- Seed a PNRG with the key
- Generate the first number
- Add this number to the first letter (byte, etc.) of the plaintext
- Generate the second number and repeat
What has been achieved here is to compress the key that we used in our one time pad. For example, instead of needing a gigabyte-long key to encrypt a gigabyte of data, we might use a 128-bit number to seed our pseudo random number generator. However, the very fact that the key can be compressed means that it cannot be truly random, and thus cannot be perfectly secure. Secure random number generation is an essential feature of modern cryptography.
Random Number Generation
Secure random number generation is an essential component of cryptographic systems. Randomness is crucial to prevent adversaries from predicting or guessing cryptographic secrets, as any pattern or predictability could be exploited to break the encryption.
To achieve secure random number generation, cryptographic algorithms rely on various sources of entropy, such as physical processes or unpredictable events, to create an initial seed. This seed is then processed through a cryptographic algorithm known as a pseudo-random number generator (PRNG) to generate a stream of random numbers. However, to achieve true security, PRNGs must be periodically reseeded with fresh entropy to maintain randomness.
Key Exchange
All of the schemes we have seen so far rely on two parties being able to share a key. The key might be long (as with a one-time pad), or short (as with a stream cypher or Caesar cypher). Unfortunately, it is rarely possible to agree this key in person, safe from potential eavesdroppers. Usually the key needs to be agreed over an insecure communications channel. However, what is to stop an attacker listening for your key?
The problem is as follows:
* How do you communicate over an insecure channel with someone with whom you've never met, when you haven't already exchanged keys?There are secure key exchange protocols, such as Diffie Hellman Key Exchange, which works using modular arithmetic.
Diffie Hellman Key Exchange
Now that you understand modular arithmetic, the mathematics behind Diffie Hellman key exchange are relatively simple.
Alice and Bob publicly agree on two numbers, $p$ (which must be a large prime number to be secure, usually at least 2048 bits) and $g$. They each secretly choose a number, which is called their private key. Alice picks a number $a$ and Bob picks a number $b$.
Public information | value | Secret information | value |
---|---|---|---|
modulus ($p$) | 19 | Alice's private key ($a$) | 4 |
base ($g$) | 4 | Bob's private key ($b$) | 4 |
From this, Alice and Bob each generate a new number called a public key. They do this by finding the modular exponent of the agreed base $b$ with their own private key. The modulus used is $p$, a large prime number they have publicly agreed upon. They share their public keys with each other, making them public and visible to potential eavesdroppers.
\[ A = g^a \mod p \]\[ A = 4^{4} \mod 19 = 9 \]\[ B = g^b \mod p \]\[ B = 4^{4} \mod 19 = 9 \]Public information | value |
---|---|
Alice's public key ($A$) | 9 |
Bob's public key ($B$) | 9 |
Alice calculates the shared secret using her private key.
\[ s = B^a \mod p \]\[ s = 9^{4} \mod 19 = 6 \]Bob calculates the shared secret using his private key.
\[ s = A^b \mod p \]\[ s = 9^{4} \mod 19 = 6 \]Now Alice and Bob have agreed on the same number $s$ without this number ever being made public. This can be used as the key for another encryption algorithm, such as a stream cypher.
Modular Exponentiation
Modular exponentiation is a key operation used in Diffie Hellman Key Exchange. This is because it is computationally quick to calculate the exponent of a number but hard to reverse this process by calculating the logarithm.
Exponentiation under modulus can be calculated as normal, i.e. to perform $a^b \\mod p$ we can first calculate $a^b$, and then find the modulus under $p$.
However, if $a$ and $b$ are very big, as is often case in public-key cryptography, this is computationally inefficient as the numbers involved in the calculation are enormous.
Summary
Further Reading
Algorithms for finding prime numbers: https://www.baeldung.com/cs/prime-number-algorithms
The Neal Stephenson novel Cryptonomicon. Stephenson's novel follows two parallel groups of codebreakers, one in World War II, another in the present day. Cryptonomicon explores cryptography, mathematics, and computer science. It explores themes of cryptography, data security, and the development of a decentralized digital currency.