# Cryptography **Cryptology** > “The science and art of writing and solving codes to hide the meaning of messages.” **Symmetric** > “Encryption methods in which both the encryption and decryption algorithms use the same key.” **Asymmetric** > “Methods which use separate, but related, private and public keys.” **Protocols** > “The application of cryptographic algorithms in secure systems.” **Cryptanalysis** > “The science and art of breaking cryptosystems.” ### Modern Cryptography (1970-) **Fundamentally different** - a scientific and mathematical discipline **Rigorously tested** - New approaches tested, justified through mathematical proofs and theory **Extremely powerful** - Ciphers usually take milliseconds to use and lifetimes of the universe to break **Wider uses** - including message integrity and authenticity **Civilian use** - everyone benefits from cryptography now ## Ciphers - Ciphers have been used for thousands of years - Usually based around either transposition or substitution #### Caesar Cipher - An early substitution cipher, we replace each letter of plain text with a shifted letter $n$ letters away from the letter - Therefore our key is an integer $-25\leq n \leq 25$ ### Modular Arithmetic - Modular arithmetic is a system of arithmetic for finite sets of integers - Common sets include - $\mathbb{N} = \{1,2,3,...\}$ - $\mathbb{Z} = \{..., -3, -2, -1, 0,1,2,3,...\}$ - Also $\mathbb{Q}, \mathbb{R}, \mathbb{C}$ - Cryptography is almost always interested in finite sets - This is useful as it avoids overflow errors - When we add or multiply two 1 byte binary digits, the result will always be 1 byte ###### Congruence Let $a, r, m \in \mathbb{Z}$ and $m > 0$ $a \equiv r (mod\space m)$ if $\frac{m}{a-r}$ Check: $a=12, m=7$ $a\equiv 5 (mod\space 7)$ $\frac{7}{12-5}$ ✅ This can be rewritten as: $a = q\cdot m+r$ ###### Equivalence Classes - The sets of all integers **mod 5** form a series of equivalence classes - All these numbers act the same in any modulo sum For example $74\cdot 62 - 47 (mod \space 5) \equiv 74\%5 \cdot 62\%5 - 47\%5$ Also works with exponentiation $3^8\space (mod\space 7)$ $3^2 = 3\cdot 3 = 9 \equiv 2\space (mod\space 7)$ $3^4 = 3^2\cdot 3^2 = 2\cdot 2 \equiv 4\space (mod\space 7)$ $3^8 = 3^4 \cdot 3^4 = 4\cdot 4 = 16 \equiv 2 \space (mod\space 7)$ #### Integer Rings - Modular arithmetic forms what in mathematics we would call a Ring ###### Ring Definition The integer ring $\mathbb{Z}_m$ consists of: 1. The set $\mathbb{Z}_m = \{0, 1,\ldots m-1\}$ 2. Two operations $+$ and $\cdot$ for all $a, b \in \mathbb{Z}_m$ such that: 1. $a+b \equiv c \space (mod\space m), (c\in \mathbb{Z})$ 2. $a\cdot b \equiv d \space (mod\space m), (d\in \mathbb{Z})$ Any time you add or multiply any two numbers in the set, the result is always in the set. We use $\equiv$ instead of $=$ as it could be an intermediate number, e.g. 12 instead of 2. ##### Properties of Rings - We can add or multiply any two numbers in the ring, and the result is in the ring - It is closed - Addition and multiplication are associative - $(a+b)+c\space =\space a+(b+c)$ - There is a neutral element 0 for addition - $a + 0 \equiv a\space mod \space m$ - The additive inverse always exists - $a + (-a) = 0\space mod \space m$ - There is a neutral element for multiplication - $a\cdot 1 \equiv a\space mod\space m$ - The multiplicative inverse exists for some but not all elements - $a\cdot a^{-1} \equiv 1 \space mod \space m$ #### Modular Inversion > In rings, the multiplicative inverse exists for some but not all elements - Multiplicative inverses allow us to *divide* by a number $$ \frac{b}{a} \equiv b \cdot a^{-1} \space (mod \space m) $$ - Not all numbers in a ring have an inverse, you can determine whether one exists quite simply: $$ gcd(a,m)=1 $$ Example $3\cdot 9 \equiv 1 \space (mod\space 26)$ $5\cdot 9 \equiv 19 \space (mod\space 26)$ $19\cdot 3 \equiv 57 \equiv 5\space (mod\space 26)$ Here a=3 and b=5, we can *divide* by 19 to get back to 5. #### Shift Cipher We can formalise the shift cipher using modular arithmetic Let $x, y, k \in \mathbb{Z}_{26}$ $$ e_k(x) = y \equiv x+k \space (mod \space 26) \\ d_k(y) = x \equiv y-k \space (mod \space 26) $$ ##### Frequency Analysis - The frequency of occurrences of each character is very consistent - The longer a cipher text is, the easier this becomes #### Affine Cipher We can extend the shift cipher into an affine cipher Let $x,y,a,b \in \mathbb{Z}_{26}$ $$ e_k(x) = y \equiv a\cdot x+b\space (mod \space 26)\\ d_k(y) = x \equiv a^{-1}\cdot(y-b)\space (mod \space m) $$ where $k=(a,b)$ and $gcd(a,26)=1$ This is a multiplication and an addition analogous to $y=mx+c$ In an Affine cipher, letters can be themselves - The keyspace of an affine cipher - a can be 0-25 - b can be 0-12 - 25*12=300 - More secure than a Caesar cipher Frequency analysis can still be used; in this case the columns will not only be shifted, but jumbled as well. - This is not hard to crack #### The Vigenere Cipher - An early stream cipher, the Vigenere cipher is a shift cipher with a running key - Unlike the Caesar cipher, the key is repeated for as long as required. - It is the equivalent to multiple interleaved Caesar ciphers - Spreads out occurrences of characters, making frequency analysis hard.