owned this note
owned this note
Published
Linked with GitHub
---
tags: vaje, kirv, classical
hackmd: https://hackmd.io/bHFTSlfYSregNEaunFK-Og
plugins: mathjax
---
# Cryptography and computer security - Tutorial 6.10.2020
---
## Modular arithmetic
$$
\begin{aligned}
a \equiv b \pmod{n} &\Leftrightarrow \exists k \in \mathbb{Z}: a - b = kn \\
a \bmod{n} &= \text{the remainder when dividing $a$ by $n$} \\
a \bmod{n} = b &\Leftrightarrow a \equiv b \pmod{n} \ \land \ 0 \le b \le n-1 \\
a \equiv b \pmod{n} &\Leftrightarrow a \bmod{n} = b \bmod{n}
\end{aligned}
$$
* <i>$\mathbb{Z}_n = \{0, 1, 2, \dots, n-1\}$</i>
* <i>$x^{-1} = y =$</i> inverse of <i>$x \in \mathbb{Z}_n \Leftrightarrow xy \equiv 1 \pmod{n}$</i>
* <i>$\mathbb{Z}_n^* = \{x \in \mathbb{Z}_n \mid x \text{ is invertible}\}$</i>
* <i>$|\mathbb{Z}_n^*| = \phi(n)$</i>
+ <i>$\phi(p^k) = (p-1) p^{k-1}$</i> (<i>$p$</i> is a prime)
+ <i>$\phi(mn) = \phi(m) \phi(n)$</i> (<i>$m \bot n$</i>)
### Exercise 1
Evaluate <i>$2020 \bmod{13}$</i> and <i>$(-2020) \bmod{13}$</i>.
```
2020 / 13 = 155
- 13
72
- 65
70
- 65
5
```
* <i>$2020 \bmod{13} = 5$</i>
* <i>$(-2020) \bmod{13} = (-5) \bmod{13} = 8$</i>
---
### Exercise 2
Solve <i>$x + 4 \equiv 2 \pmod{7}$</i> and <i>$3x - 4 \equiv 4 \pmod{7}$</i>.
$$
\begin{aligned}
x + 4 &\equiv 2 \pmod{7} \\
\exists k \in \mathbb{Z}: x + 4 - 2 &= 7k \\
\exists k \in \mathbb{Z}: x - (-2) &= 7k \\
x &\equiv -2 \pmod{7}
\end{aligned}
$$
$$
\begin{aligned}
3x - 4 &\equiv 4 \pmod{7} \\
3x &\equiv 8 \pmod{7} \\
3x &\equiv 1 \pmod{7} \\
\exists k \in \mathbb{Z}: 3x - 1 &= 7k \\
\exists k \in \mathbb{Z}: 3x &= 7k + 1 & k = 2 + 3t &\quad (k \equiv 2 \pmod{3}) \\
\exists t \in \mathbb{Z}: 3x &= 7(2 + 3t) + 1 \\
\exists t \in \mathbb{Z}: 3x &= 15 + 21t \\
\exists t \in \mathbb{Z}: x &= 5 + 7t \\
\exists t \in \mathbb{Z}: x - 5 &= 7t \\
x &\equiv 5 \pmod{7}
\end{aligned}
$$
<i>$3 \bot 7$</i> ... <i>$3$</i> and <i>$7$</i> are *coprime* (<i>$\gcd(3, 7) = 1$</i>)
---
### Exercise 3
List all the invertible elements in <i>$\mathbb{Z}_{26}$</i> and determine their inverses.
* <i>$\mathbb{Z}_{26} = \{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\}$</i>
* <i>$26 = 2 \cdot 13$</i>
* <i>$\mathbb{Z}_{26}^* = \{1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25\}$</i>
* <i>$\phi(26) = \phi(2) \phi(13) = 1 \cdot 12 = 12$</i>
$$
\begin{aligned}
1^{-1} &\equiv 1 \pmod{26} & 25^{-1} &\equiv 25 \pmod{26} \\
3^{-1} &\equiv 9 \pmod{26} & 23^{-1} &\equiv 17 \pmod{26} \\
9^{-1} &\equiv 3 \pmod{26} & 17^{-1} &\equiv 23 \pmod{26} \\
5^{-1} &\equiv 21 \pmod{26} & 21^{-1} &\equiv 5 \pmod{26} \\
7^{-1} &\equiv 15 \pmod{26} & 19^{-1} &\equiv 11 \pmod{26} \\
15^{-1} &\equiv 7 \pmod{26} & 11^{-1} &\equiv 19 \pmod{26}
\end{aligned}
$$
---
### Exercise 4
Prove that <i>$a \bmod{n} = b \bmod{n}$</i> if and only if <i>$a \equiv b \pmod{n}$</i>.
---
## Classical ciphers
Cryptosystem: <i>$(\mathcal{P}, \mathcal{C}, \mathcal{K}, \mathcal{E}, \mathcal{D})$</i>
* <i>$\mathcal{P}$</i>: set of plaintexts
* <i>$\mathcal{C}$</i>: set of ciphertexts
* <i>$\mathcal{K}$</i>: set of keys
* <i>$\mathcal{E}$</i>: set of encryption functions <i>$E : \mathcal{P} \times \mathcal{K} \to \mathcal{C}$, $E_k(x) = y$</i>
* <i>$\mathcal{D}$</i>: set of decryption functions <i>$D : \mathcal{C} \times \mathcal{K} \to \mathcal{P}$, $D_k(y) = x$</i>
* <i>$\forall x \in \mathcal{P} \ \forall k \in \mathcal{K}: D_k(E_k(x)) = x$</i>
Caesar cipher:
* <i>$\mathcal{P} = \mathcal{C} = \mathcal{K} = \mathbb{Z}_{26}$</i>
* <i>$E_k(x) = (x+k) \bmod{26}$</i>
* <i>$D_k(x) = (x-k) \bmod{26}$</i>
Substitution cipher:
* <i>$\mathcal{P} = \mathcal{C} = \mathbb{Z}_{26}$</i>
* <i>$\mathcal{K} = S_{26}$</i> (all permutations on 26 elements)
* <i>$E_\pi(x) = \pi(x)$</i>
* <i>$D_\pi(x) = \pi^{-1}(x)$</i>
### Exercise 5
If an encryption function <i>$E_k$</i> is identical to the decryption function <i>$D_k$</i>, then the key <i>$k$</i> is said to be an *involutory key*. Find all involutory keys for the Caesar cipher over <i>$\mathbb{Z}_{26}$</i>.
$$
\begin{aligned}
k &\equiv -k \pmod{26} \\
2k &\equiv 0 \pmod{26} \\
k &\equiv 0 \pmod{13} \\
k &\in \{0, 13\}
\end{aligned}
$$
---
### Exercise 6
An affine cipher has <i>$E_k(x) = 5x+1 \bmod{26}$</i> as its encryption function. What is its decryption function?
Affine cipher:
* <i>$\mathcal{P} = \mathcal{C} = \mathbb{Z}_{26}$</i>
* <i>$\mathcal{K} = \mathbb{Z}_{26}^* \times \mathbb{Z}_{26}$</i>
* <i>$E_{(a, b)}(x) = (ax + b) \bmod{26}$</i>
* <i>$D_{(a, b)}(x) = ?$</i>
---
### Exercise 7
Suppose that <i>$k = (a, b)$</i> is a key for the affine cipher over <i>$\mathbb{Z}_n$</i>. Prove that <i>$k$</i> is involutory if and only if <i>$a^{-1} \bmod{n} = a$</i> and <i>$b(a+1) \equiv 0 \pmod{n}$</i>.
---
### Exercise 8
Determine all involutory keys for the affine cipher over <i>$\mathbb{Z}_{26}$</i>.
---
### Exercise 9
Is it possible that an affine cipher over <i>$\mathbb{Z}_{26}$</i> encrypts *H* to *N* and *I* to *R*?
---
### Exercise 10
Decrypt the following ciphertext, which has been obtained from a substitution cipher, with word division preserved. Help yourself with [this website](https://www.boxentriq.com/code-breaking/cryptogram).
```
YI QCLJMXNCTJEL, T QTFPTC QYJEFC YP XIF XW MEF PYBJUFPM TIO BXPM
DYOFUL ZIXDI FIQCLJMYXI MFQEIYGAFP. YM YP T MLJF XW PASPMYMAMYXI
QYJEFC YI DEYQE FTQE UFMMFC YI MEF JUTYIMFKM YP CFJUTQFO SL
T UFMMFC PXBF WYKFO IABSFC XW JXPYMYXIP OXDI MEF TUJETSFM.
```