By 1nfocalypse
- Summary
- Some Background
- Terms
- Shift Ciphers
- Vigenère Cipher
- Feistel Network
- More Background
- RSA
- ElGamal
- Elliptic Curves
- Additional Information
- Recommended Reading
- Problems
- Thanks and Acknowledgements
This document is intended to serve as a reference guide along with as a resource for mathematical cryptography. While it may be useful to utilize it to supplement for self study, it is not intended to replace an appropriate curriculum. This document is also not meant to be a comprehensive guide to cryptography, and as such, not all systems or features will be talked about, i.e. NTRU, signatures, etc. There will, however, be recommended reading delineated at the bottom of this guide that will be much more comprehensive. This guide assumes knowledge of algebra and some number theory, however, anything past high school level will be explained, albeit with some brevity. It is highly encouraged to pursue study in these areas yourself for further information.
Most informationation is sourced from Coding Theory and Cryptography: The Essentials 2Ed, Wikipedia, and personal notes from a university mathematics course. Some of the contents here either have or will have toy implementations of them in C++ on my main profile. These projects are ongoing, and as such may not be completed or fully functional by the time of reading. Current implementations are:
Throughout, mathematical notation and terminology will be utilized. This section serves to familiarize people who have never encountered this before. If you are comfortable with mathematics, please feel free to skip this section.
| : This is the symbol meaning that something is given, or to be assumed. It is read given.
: : This is the symbol for "such that," and read in the same manner.
$0 \bigoplus 0 = 0$ $0 \bigoplus 1 = 1$ $1 \bigoplus 0 = 1$ $1 \bigoplus 1 = 0$
for each element in the bitstring.
Let us begin by introducing some terms utilized in mathematical cryptography. We will begin with
From this, we will then define the
We must also define the encryption and decryption functions, and along with them, the
The
The
The encryption space can be thought of simply as another message space, however, it is after the encryption function has been applied to the original message. The key space can be thought of simply as where the keys utilized in the encryption and decryption functions are stored.
With this in mind, let us proceed to the first
Shift Ciphers are one of, if not the simplest, kinds of ciphers. Originating with Julius Caesar, and hence sometimes called the
Given the mappings:
$A \leftrightarrow 0$ $B \leftrightarrow 1$ $C \leftrightarrow 2$ - and so on, as depicted below,
Let the
$C_{1} = I + 3 \rightarrow C_{1} = 8 + 3\ \mathbf{mod}\ 26= 11$ $C_{2} = L + 3 \rightarrow C_{2} = 11 + 3\ \mathbf{mod}\ 26 = 14$ $C_{3} = I + 3 \rightarrow C_{3} = 8 + 3\ \mathbf{mod}\ 26 = 11$ $C_{4} = K + 3 \rightarrow C_{4} = 10 + 3\ \mathbf{mod}\ 26 = 13$ $C_{5} = E + 3 \rightarrow C_{5} = 4 + 3\ \mathbf{mod}\ 26 = 7$ $C_{6} = C + 3 \rightarrow C_{6} = 2 + 3\ \mathbf{mod}\ 26 = 5$ $C_{7} = R + 3 \rightarrow C_{7} = 17 + 3\ \mathbf{mod}\ 26 = 20$ $C_{8} = Y + 3 \rightarrow C_{8} = 24 + 3\ \mathbf{mod}\ 26 = 1$ $C_{9} = P + 3 \rightarrow C_{9} = 15 + 3\ \mathbf{mod}\ 26 = 18$ $C_{10} = T + 3 \rightarrow C_{10} = 19 + 3\ \mathbf{mod}\ 26 = 22$ $C_{11} = O + 3 \rightarrow C_{11} = 14 + 3\ \mathbf{mod}\ 26 = 17$ $C_{12} = G + 3 \rightarrow C_{12} = 6 + 3\ \mathbf{mod}\ 26 = 9$ $C_{13} = R + 3 \rightarrow C_{13} = 17 + 3\ \mathbf{mod}\ 26 = 20$ $C_{14} = A + 3 \rightarrow C_{14} = 0 + 3\ \mathbf{mod}\ 26 = 3$ $C_{15} = P + 3 \rightarrow C_{15} = 15 + 3\ \mathbf{mod}\ 26 = 18$ $C_{16} = H + 3 \rightarrow C_{16} = 7 + 3\ \mathbf{mod}\ 26 = 10$ $C_{17} = Y + 3 \rightarrow C_{17} = 24 + 3\ \mathbf{mod}\ 26 = 1$
From this, we map the numbers resulting from
C = LOLNHFUBSWRJUDSKB
Applying the decryption function,
$M_{1} = L - 3 \rightarrow M_{1} = 11 - 3\ \mathbf{mod}\ 26= 8$ $M_{2} = O - 3 \rightarrow M_{2} = 14 - 3\ \mathbf{mod}\ 26 = 11$ $M_{3} = L - 3 \rightarrow M_{3} = 11 - 3\ \mathbf{mod}\ 26 = 8$ - and so on,
Repeating the mapping from numbers to letters with the output of
M = ILIKECRYPTOGRAPHY
Thus concluding the encryption and decryption of a Shift Cipher.
Given that the Shift Cipher takes place over the English alphabet and has a constant k, one must only perform at most 25 shifts to be able to decipher the code, making it computationally inexpensive to break. A frequency analysis can also be performed, which uses the fact that certain characters occur at certain frequencies in the English language. By measuring the frequency at which characters occur in the ciphertext, it is possible to take an educated guess as to which characters they represent, and break the code in that manner. It also requires that it be given that a key can be communicated without risk of interception, as otherwise, the entire system would be vulnerable.
The Vigenère Cipher is similar to a shift cipher, however, utilizes a variable value of k based on a
Let k = "ILIKE" and m = "CRYPTOGRAPHY"
-
$k_{1}$ = I = 8 -
$k_{2}$ = L = 11 -
$k_{3}$ = I = 8 -
$k_{4}$ = K = 10 -
$k_{5}$ = E = 4
The above step is purely done for the reader's reference below.
$C_{1} = C + I \rightarrow C_{1} = 2 + 8\ \mathbf{mod}\ 26= 10$ $C_{2} = R + L \rightarrow C_{2} = 17 + 11\ \mathbf{mod}\ 26 = 2$ $C_{3} = Y + I \rightarrow C_{3} = 24 + 8\ \mathbf{mod}\ 26 = 6$ $C_{4} = P + K \rightarrow C_{4} = 15 + 10\ \mathbf{mod}\ 26 = 25$ $C_{5} = T + E \rightarrow C_{5} = 19 + 4\ \mathbf{mod}\ 26 = 23$ $C_{6} = O + I \rightarrow C_{6} = 14 + 8\ \mathbf{mod}\ 26 = 22$ - and so on.
The result of
To decode a Vigenère Cipher, we simply subtract the value of
$M_{1} = K - I \rightarrow M_{1} = 10 - 8\ \mathbf{mod}\ 26= 2$ $M_{2} = C - L \rightarrow M_{2} = 2 - 11\ \mathbf{mod}\ 26 = 17$ $M_{3} = G - I \rightarrow M_{3} = 6 - 8\ \mathbf{mod}\ 26 = 24$ $M_{4} = Z - K \rightarrow M_{4} = 25 - 10\ \mathbf{mod}\ 26 = 15$ $M_{5} = X - E \rightarrow M_{5} = 23 - 4\ \mathbf{mod}\ 26 = 19$ $M_{6} = W - I \rightarrow M_{6} = 22 - 8\ \mathbf{mod}\ 26 = 14$ - and so on.
The result of
In 1863, a man named Friedrich Kasiski proposed a method of determining the key length within a Vigenère Cipher by measuring the difference between numerical locations of patterns within the ciphertext. For example, if one is to note the pattern IQE at location 110, 138, and 226, it is likely that the keyword length divides the difference between these locations. The differences 138 - 110 = 28 and 226 - 110 = 116 leave us with two non-prime numbers, which in turn are factored into primes as
Feistel networks are the conclusion of the "Classical Cryptography" portion of this writeup. A Feistel Network is a
At this point, the Feistel Nework functions as follows.
- Let i be defined as 0,1,...,r-1,r
- Let
$L_{i}$ and$R_{i}$ be the Left and Right half of the bitstring being processed, with that being initialized as the cleartext such that$L_{0}$ is the left half of the cleartext and$R_{0}$ is the right half of the cleartext. - For each round r, calculate:
$L_{i+1} = R_{i}$ $R_{i+1} = L_{i} \bigoplus F(R_{i},K_{i})$
Which yields a ciphertext C =
- Let i be defined as r,r-1,...,1,0
- Let
$R_{i+1}$ be initialized as the$\mathit{left}$ half of the ciphertext C, and$L_{i+1}$ be initialized as the$\mathit{right}$ half of the ciphertext C - For each round r, calculate:
$R_{i} = L_{i+1}$ $L_{i} = R_{i+1} \bigoplus F(L_{i+1},K_{i})$
Which yields the plaintext message M. A visual of this process is shown below.
Feistel Networks are also able to utilize unbalanced
We do! While DES, the most well-known implementation of Feistel Networks, is no longer in use, it is not due to the Feistel Network itself. Feistel Networks, and modified Feistel Networks, are still in use with numerous protocols, such as Twofish, Camellia, and many others. From this point forward, all algorithms mentioned are in active use in modern cryptographic protocols.
From this point forward, some additional background in Abstract Algebra and Number Theory must be provided. If you are already comfortable in these fields, please feel free to skip ahead to RSA.
We must first define one of the foundations for the rest of the writeup, which is an algebraic structure known as a
- Associativity:
$\forall a,b,c \in G, a \bullet (b \bullet c) = (a \bullet b) \bullet c$ - Identity:
$\exists i \in G : \forall g \in G, i \bullet a = a, a \bullet i = a$ . - Inverse:
$\forall a \in G, \exists b : a \bullet b = i, b \bullet a = i$ , where$i$ is the identity element. - Closure:
$\forall a,b \in G, \exists c \in G : a \bullet b = c$ .
We must also define the term
-
$\forall a,b \in G, a \bullet b = b \bullet a$ .
Groups have or may have certain properties, such as:
- Order: The number of elements within the group. A group must have some order, represented
$ord(G)$ or$|G|$ . - Generators: An element
$a \in G$ such that$a^n$ enumerates all elements of$G$ (simplified for relevancy).
We are interested in two kinds of groups in this writeup:
- Groups called
$\mathbb{Z}_{p}^*$ groups, which consist of all numbers coprime with p, which in turn means that they share no common factors other than 1. - Groups formed with Elliptic Curves under a special definition of addition.
In terms of generators, we are interested in finding the order of elements in
Given the order of
Examples of
$\mathbb{Z}_{8}^* = \{1,3,5,7\}$ $\mathbb{Z}_{7}^* = \{1,2,3,4,5,6\}$
You may notice that
We must also bring in the problem of
Given this, we must introduce two problems, that, as of the time of writing, are currently not solveable in polynomial time, however, are easily verified to be true.
- Integer factorization. RSA utilizes two large integers,
$p$ and$q$ , then multiplies them together to form$n \in \mathbb{N}$ such that the only factors of$n$ are, indeed,$p$ and$q$ . It then tasks the eavesdropper with factoring$n$ in order to recover the cleartext from the ciphertext. - The Discrete Logarithm. Given a group
$G$ , let$a,b \in G$ . The discrete logarithm problem asks the eavesdropper to find$k \in \mathbb{Z} : b^k = a$ . This is the problem utilized by ElGamal.
Lastly, we will discuss Euler's Totient Function, which has the notation
$\phi(n) = n\prod_{p|n}\left( 1-\frac{1}{p} \right)$
In English, this simply means, for each prime number that divides
It is of note that if
Thus concludes our essential bits from Abstract Algebra and Number Theory. Now back to the cryptography!
RSA, or Rivest-Shamir-Adleman, is an asymmetric-key cryptography algorithm, also known as a public-key cryptography algorithm. Public key cryptography works via a publication of a public key by a user, such that anyone may use this public key to encrypt their message
Key Generation for RSA is as follows:
- Let
$p,q$ be sufficiently large prime numbers. Let$n = p * q$ .$p$ and$q$ should be kept secret. - Take
$\phi(pq) = (p-1)(q-1) = \phi$ . Choose an encryption exponent,$e$ , such that$2 < e < \phi$ , and$e$ is coprime with$\phi$ . - Determine
$d$ as$d \equiv e^{-1}$ - Publish (
$n$ ,$e$ ), keeping$d$ private.
Some of you may be scratching your heads, asking yourselves why we are able to trivialize finding the inverse for the encryption exponent when it would seem that
- Let
$n$ be the number of rows,$x_{i}$ be a factor you must solve for,$r_{i}$ be the remainder given$x_{i}$ , and$s_{1}, s_{2}, s_{3}$ be columns to the right, such that$s_{1} = 0, s_{2} = 1, s_{3} = s_{1} - x_{i} * s_{2}$ . - This results in
$\phi = x_{1} * e + r_{1} |$ ($0$ ) ($1$ ) ($0-x_{1}*1$ ) - Upon computing this, generate
$row_{2}$ as$e = x_{2} * r_{1} + r_{2} |$ ($1$ ) ($0-x_{1} * 1$ ) ($1 - x_{2} * s_{2}$ ) (Please note, the reference to$s_{2}$ here refers to the value of$s_{2}$ on$row_{2}$ , not$row_{1}$ . - This pattern is repeated until
$r_{n} = 0$ , at which point the value in$s_{2}$ on$row_{n}$ is$\equiv e^{-1} \bmod \phi$ .
$m^e \bmod n \equiv c \bmod n$
$c^d \bmod n \equiv m \bmod n$
The RSA numbers, which can be found here, are the ones utilized in legitimate implementations. Legitimate RSA implementations also utilize padding, as there have been several successful, although highly specific, attacks against the protocol without it. However, with padding, RSA is currently regarded as secure.
The ElGamal Cryptosystem is a cryptosystem that utilizes cyclic multiplicative groups, that is, a group that can be solely generated by one element under the operation of multiplication. This is why our definition of generator is described in the manner that it is. The ElGamal cryptosystem utilizes
a three element public key and a one element private key, with the public key being comprised of a prime, a generator, and the generator raised to the public key modulo the prime. The message is sent as the generator raised to the $k$th power, where
Key Generation for ElGamal is as follows:
- Given some large prime number
$p$ , select$a : 1 \leq a \leq p - 2$ . This is your private key. - Find a generator for the group, which becomes
$\alpha$ . - Publish your public key as (
$p, \alpha, \alpha^a \bmod p$ )
- Randomly select
$k : 1 \leq k \lt p$ - Send (
$\alpha^k , m * (\alpha^a)^k \bmod p$ )
- Take
$\alpha^k$ , and raise it to$p - 1 - \alpha \bmod p$ - Multiply it by the second element.
ElGamal is incredibly flexible, and via the random generation of
-
$log_{\alpha}{\beta}$ , given$\alpha, \beta, p$ - Calculate
$m$ such that$m = \lceil \sqrt{p} \rceil$ , with the marks on either side denoting "ceiling", or an unconditonal round up in the case of a decimal. - Create a table of
$m-1$ entries, with$j$ being the numbers 0 through$m - 1$ , and then below these entries, the value of$\alpha^j \bmod p$ . - Calculate the inverse of the generator using the Extended Euclidean Algorithm, as described above.
- Raise this to the
$m \bmod p = \alpha^{-m}$ . - Calculate values of
$\beta * (\alpha^{-m})^i \bmod p$ , where$i \in \mathbb{N}$ until a value occurs that is in the earlier table's second row. - The solution is the found
$j + i * m$ , where$j$ and$i$ are the indices.
As you can imagine, using this with a very large
Elliptic Curves are a unique class of curves, typically, though not always, resulting from Weierstrass equations of the form
However, to digress, Elliptic Curves are utilized due to the smaller key size they require while retaining the same level of security. In order for an elliptic curve to be formed in the case of using a prime
To actually utilize the group, we must first define addition in context.
In the case of two distinct points,
Where
In the case of adding two of the same points,
Lastly, if we are adding some point and the point at infinity,
With this definition, we have formally enumerated all the standards of our group.
Elliptic Curves can be utilized in multiple cryptosystems, notably ElGamal as described in Neil Koblitz's paper. It is typically implemented using a specialized version of the Discrete Logarithm Problem, as discussed above,
however, the task is that, given some elliptic curve
As stated before, this writeup is not comprehensive, ergo some things do not warrant an entire section, however, should be mentioned. Such topics include Quantum Cryptography, Shor's Algorithm, and the NTRU cryptosystem.
Quantum Cryptography refers to utilizing the properties of quantum mechanics for the purpose of cryptography. Notably, it typically plays on something called the No-Cloning Theorem, which states that it is impossible to create an independent and identical copy of an unknown quantum state. Ergo, if an eavesdropper attempts to read the data in its encrypted state, the state will collapse. Quantum cryptography shows promise for longevity of security, in the sense that information can be called secure if encrypted for longer periods of time than it would have otherwise been with classical encryption. The mathematics required at this point is far beyond the scope of this document, however, it is worth knowing that this type of cryptography is a facet of the field.
Shor's Algorithm makes solving Discrete Logarithms and the RSA Problems less computationally intensive. It is a quantum algorithm that, assuming the quantum computer utilizing it is capable of running it, is able to solve these problems in polynomial time, which is considered satisfactory and not computationally hard. As quantum computer development furthers, the likelihood that one will exist that is capable of utilizing this algorithm increases. Some postulate that this will have catastrophic consequences, as a vast swath of previously secret information will no longer be guaranteed to be. However, many symmetric key algorithms and some others, referred to as post-quantum algorithms, still retain their status as computationally hard, and therefore, secure.
As previously mentioned, there are algorithms that are still secure in a post-quantum world. The most famous is the NTRU cryptosystem, which utilizes an algebraic structure known as a
Here are some good resources for additional information about cryptography, along with further reading for Elliptic Curves and NTRU/Post-Quantum Cryptography.
- Coding Theory and Cryptography: The Essentials 2Ed, by D.C. Hankerson, et. Al.
- Practical Cryptography - Algorithms and Implementations Using C++, edited by Saiful Azad, Al-Sakib Khan Pathan
- Elliptic Curve Cryptosystems, by Neil Koblitz
- Quantum Cryptography | Wikipedia
- Shor's Algorithm | Wikipedia
- NTRUEncrypt | Wikipedia
- Introduction to Modern Cryptography: Principles and Protocols, by Jonathan Katz and Yehuda Lindell
- An Introduction to Mathematical Cryptography, by Jeffrey Hoffstein et. Al.
- A Course in Number Theory and Cryptography, by Neil Koblitz
- Yet Another Introductory Number Theory Textbook - Cryptology Emphasis, by Jonathan Poritz (This one's free!)
- Applied Cryptography: Protocols, Algorithms and Source Code in C
Practice Problems are available here, and solutions are available here.
Thank you for taking the time to read this writeup! It was written over the span of three weeks, and largely in rare, but intense burts, therefore there may be some errors. If you catch any, please submit a PR, and if merged, your name will be added here!
- Could be you!
I would like to extend a thank you to my professor, Dr. Grace, for his time, knowledge, and patience throughout the semester. I would also like to thank the NXFury community for letting me test some of the methods of teaching I employed in this writeup during a live session.