Cryptography · Unit 3 · 8 hrs
Asymmetric Ciphers
Exam-focused notes for Asymmetric Ciphers (Cryptography, CSC327): what the TU syllabus asks and how it has actually been tested, with 12 solved past questions from this unit.
Asymmetric Ciphers in short
Asymmetric (public-key) cryptography solves the key-distribution problem that symmetric ciphers cannot: two parties who have never met can still communicate securely. This unit builds the number theory that makes this possible, then applies it to key distribution, Diffie-Hellman key exchange, RSA, and ElGamal.
What this unit covers
- Number Theory: Prime Numbers, Fermat’s Theorem, Euler’s Theorem, Primility Testing, Miller-Rabin Algorithm, Extended Euclidean Theorem, Discrete Logarithms
- Public Key Cryptosystems
- Applications of Public Key Cryptosystems
- Distribution of public key
- Distribution of secret key by using public key cryptography
- Diffie-Helman Key Exchange
- Man-in-the-Middle Attack
- RSA Algorithm
- Elgamal Cryptographic System
Man-in-the-Middle Attack
2082 · 10 marks
Illustrate the man in middle attack in Diffie-Hellman key exchange protocol. Assume the prime number be 19 and 10 as its primitive root. Select 5 as private key and 4 as random integer. Find the cipher text of M = 2 using Elgamal crypto system.[10]
Show solved answerHide answer
Man-in-the-Middle Attack on Diffie-Hellman & ElGamal Cryptosystem
Given Data
- Prime number: $p = 19$
- Primitive root: $g = 10$
- Private key: $x = 5$
- Random integer: $k = 4$
- Message: $M = 2$
Part 1: Man-in-the-Middle Attack on Diffie-Hellman
Normal Diffie-Hellman
Public parameters: $p = 19$, $g = 10$.
- Alice picks private $X_A$, sends $Y_A = g^{X_A} \bmod p$.
- Bob picks private $X_B$, sends $Y_B = g^{X_B} \bmod p$.
- Shared key: $K = Y_B^{X_A} \bmod p = Y_A^{X_B} \bmod p$.
The Attack
An attacker Darth (D) sits between Alice and Bob. Since plain Diffie-Hellman has no authentication, Darth impersonates each side to the other.
Darth chooses two private keys $X_{D1}, X_{D2}$ and computes: $$Y_{D1} = g^{X_{D1}} \bmod p, \qquad Y_{D2} = g^{X_{D2}} \bmod p$$
Alice (A) Darth (D) Bob (B)
|--- Y_A ------------->| |
| |--- Y_D1 ---------------->|
| |<-- Y_B ------------------|
|<-- Y_D2 -------------| |
Steps:
- Alice sends $Y_A$; Darth intercepts it.
- Darth sends $Y_{D1}$ to Bob.
- Bob sends $Y_B$; Darth intercepts it.
- Darth sends $Y_{D2}$ to Alice.
Resulting keys:
| Party | Key computed |
|---|---|
| Alice | $K_1 = Y_{D2}^{X_A} \bmod p$ |
| Darth (with Alice) | $K_1 = Y_A^{X_{D2}} \bmod p$ |
| Darth (with Bob) | $K_2 = Y_B^{X_{D1}} \bmod p$ |
| Bob | $K_2 = Y_{D1}^{X_B} \bmod p$ |
Consequence: Alice shares $K_1$ with Darth (thinking it is Bob), Bob shares $K_2$ with Darth (thinking it is Alice). Darth decrypts, reads, possibly alters, and re-encrypts every message. The attack succeeds because DH does not authenticate the parties.
Part 2: ElGamal Cryptosystem
Step 1: Key Generation
Public key: $$y = g^x \bmod p = 10^5 \bmod 19$$
- $10^2 = 100 \equiv 5 \pmod{19}$
- $10^4 \equiv 5^2 = 25 \equiv 6 \pmod{19}$
- $10^5 \equiv 6 \cdot 10 = 60 \equiv 60 - 57 = 3 \pmod{19}$
$$\boxed{y = 3}$$
Public key = $(p=19,\ g=10,\ y=3)$; Private key = $5$.
Step 2: Encryption of $M = 2$ with $k = 4$
Compute $C_1$: $$C_1 = g^k \bmod p = 10^4 \bmod 19 = 6$$
Compute $C_2$: $$y^k \bmod p = 3^4 \bmod 19 = 81 \bmod 19 = 5$$ $$C_2 = (y^k \cdot M)\bmod p = (5 \cdot 2)\bmod 19 = 10$$
Final Answer
$$\textbf{Ciphertext } (C_1, C_2) = (6,\ 10)$$
Verification (Decryption)
$$M = C_2 \cdot (C_1^{x})^{-1} \bmod p$$
- $6^5 \bmod 19$: $6^2 = 36 \equiv 17$; $6^4 \equiv 17^2 = 289 \equiv 4$; $6^5 \equiv 4 \cdot 6 = 24 \equiv 5$.
- $5^{-1} \bmod 19 = 4$ (since $5 \cdot 4 = 20 \equiv 1$).
- $M = 10 \cdot 4 \bmod 19 = 40 \bmod 19 = 2 \checkmark$
Recovered $M = 2$, matching the original message.
Number Theory
2082 · 5 marks
State Fermat's theorem with example. What is the implication of discrete logarithm? [5]
2080 · 5 marks
How Miller Rabin test is used for primality testing? Show whether the number 561 passes the test. [5]
2079 · 5 marks
What is the condition of for two integers, x and y, to be relatively prime? Find whether 61 is prime or not using Miller-Rabin algorithm. [5]
Show solved answerHide answer
Relatively Prime Condition and Miller-Rabin Test for 61
Given
- Number to test: $n = 61$
- Question asks: (a) condition for two integers $x, y$ to be relatively prime; (b) test primality of 61 using Miller-Rabin.
- No specific witness $a$ is given, so we choose valid witnesses.
Solution
Part 1: Condition for Relatively Prime
Two integers $x$ and $y$ are relatively prime (coprime) if and only if their greatest common divisor is 1:
$$\gcd(x, y) = 1$$
They share no common positive divisor other than 1.
Example: $\gcd(8, 15) = 1$, so 8 and 15 are relatively prime.
Part 2: Miller-Rabin Test for n = 61
Step 1: Write $n - 1 = 2^k \cdot m$, m odd
$$n - 1 = 60 = 2^2 \times 15$$
So $k = 2$, $m = 15$ (odd, confirmed).
Step 2: Choose witness $a = 2$ (valid: $1 < 2 < 60$).
Step 3: Compute $b = a^m \bmod n = 2^{15} \bmod 61$
$$2^{15} = 32768$$ $$32768 \bmod 61: \quad 61 \times 537 = 32757, \quad 32768 - 32757 = 11$$
Cross-check via the alternative decomposition: $b = 11$.
Step 4: Is $b = 1$? No, $11 \neq 1$. Proceed to loop.
Step 5: Loop $i = 0$ to $k-1 = 1$
Check $b = n - 1 = 60$ at start of each iteration:
-
$i = 0$: $b = 11 \neq 60$. Square: $b = 11^2 = 121 \equiv 121 - 61 = 60 \pmod{61}$.
Now $b = 60 = -1 \pmod{61}$ at the moment it is produced.
Note on algorithm form: In the correct Miller-Rabin loop, after squaring we check whether we hit $-1$. The sequence of squarings starting from $2^{15}$ is:
$$2^{15} \equiv 11, \quad 2^{30} \equiv 11^2 = 60 \equiv -1 \pmod{61}$$
Since one of the values $a^{2^i m}$ equals $-1$ (here $2^{30} \equiv -1$), the test declares probably prime. Witness $a = 2$ is a strong liar-free result: 61 passes.
$$11^2 = 121, \quad 121 - 61 = 60 \quad (\text{not } 59)$$
Therefore, with $a = 2$ itself, the test returns Prime (no second witness needed).
Double-check with $a = 7$:
$$7^2 = 49,\ 7^4 = 49^2 = 2401 \equiv 2401 - 39(61)=2401-2379=22$$ $$7^8 = 22^2 = 484 \equiv 484 - 7(61) = 484 - 427 = 57$$ $$7^{15} = 7^8 \cdot 7^4 \cdot 7^2 \cdot 7 = 57 \cdot 22 \cdot 49 \cdot 7 \pmod{61}$$ $$57 \cdot 22 = 1254 \equiv 1254 - 20(61)=1254-1220=34$$ $$34 \cdot 49 = 1666 \equiv 1666 - 27(61)=1666-1647=19$$ $$19 \cdot 7 = 133 \equiv 133 - 2(61) = 11 \pmod{61}$$
So $7^{15} \equiv 11 \neq 1, \neq 60$. Square: $11^2 = 121 \equiv 60 \equiv -1$. Returns Prime.
(Note that $7^{15} \equiv 11$, not $60$; the squaring still gives $-1$, so the conclusion "Prime" holds.)
Conclusion
$$\boxed{61 \text{ is Prime (probably prime by Miller-Rabin)}}$$
2078 · 5 marks
Find the value of $7^{2019} \mod 13$ using Fermat's Little theorem. Define Euler totient function with an example. [5]
2076 · 5 marks
What is the role of the prime number in the Euler totient Function? Find the GCD of 12 and 16 using the Euclidean algorithm. [5]
Show solved answerHide answer
Role of Prime Numbers in Euler's Totient Function and GCD by Euclidean Algorithm
Given Data
- Euler totient function context; find role of prime numbers.
- Numbers for GCD: $a = 16$, $b = 12$.
Part 1: Role of Prime Numbers in Euler's Totient Function
Definition
Euler's Totient Function $\phi(n)$ counts the number of positive integers in the range $1 \le k \le n$ that are relatively prime to $n$ (i.e. $\gcd(k, n) = 1$).
Role of a Prime Number
If $n = p$ is prime, then $p$ has no divisors other than $1$ and $p$ itself. Hence every integer in ${1, 2, \dots, p-1}$ is coprime to $p$.
$$\boxed{\phi(p) = p - 1}$$
For a prime power: $$\phi(p^k) = p^k - p^{k-1} = p^{k-1}(p-1)$$
Why It Matters
- Simplifies computation: For a prime, the totient is trivially $p-1$.
- Multiplicative building block: Since $\phi$ is multiplicative for coprime factors, any $n$ factored into primes gives $$\phi(n) = n \prod_{p \mid n}\left(1 - \frac{1}{p}\right)$$
- RSA cryptography: With $n = p \times q$ (product of two primes), $$\phi(n) = (p-1)(q-1)$$ which is the basis for choosing the encryption/decryption exponents.
Example
$\phi(7)$: since $7$ is prime, $\phi(7) = 7 - 1 = 6$. Check: ${1,2,3,4,5,6}$ are all coprime to $7$. Count $= 6$. ✓
Part 2: GCD of 12 and 16 by Euclidean Algorithm
Rule: $\gcd(a,b) = \gcd(b,, a \bmod b)$ until the remainder is $0$; the last non-zero remainder is the GCD.
Take $a = 16,\ b = 12$:
| Step | Computation | Remainder |
|---|---|---|
| 1 | $16 = 1 \times 12 + 4$ | $4$ |
| 2 | $12 = 3 \times 4 + 0$ | $0$ |
Since the remainder is now $0$, the last non-zero remainder is $4$.
$$\boxed{\gcd(12, 16) = 4}$$
Verification
- $12 = 4 \times 3$ ✓
- $16 = 4 \times 4$ ✓
- $4$ is the largest common divisor. ✓
Diffie-Helman Key Exchange
2081 · 10 marks
Why do we need discrete logarithm? Illustrate with an example. Consider a Diffie-Hellman scheme with a common prime p = 13 between user A and user B. Suppose public key of A is 10 and public key of B is 8. Now determine their private keys and shared secret key. Select any valid primitive root of 13.[10]
2080 · 5 marks
How does meet in middle attack work in Diffie Helman key exchange protocol? Explain. [5]
2076 · 5 marks
Define discrete logarithm. Explain the procedure of sharing the secret key in Diffie Hellman. [5]
RSA Algorithm
Elgamal Cryptographic System
2079 · 10 marks
Show that Z 5 is a field. John publishes the ElGamal public key (q, α, YA) =(101, 2, 14). Jane desired to send the secret message CSIT to John. Using the equivalence A = 0, B=1, ..., Z=25, encrypt the message using John’s public key. Use a random number k = 4.[10]
2076 · 10 marks
State Fermat’s theorem with an example. Given the prime number p=29 and its primitive root g=8, private key sender with X=9 and random integer K=11, encrypt the message m=13 using ElGamal cryptosystem.[10]
Make Unit 3 stick
Practice CSC327 with flashcards & quizzes