3 Asymmetric Ciphers

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.

Asymmetric Ciphers: notes

Number Theory: Prime Numbers, Fermat's Theorem, Euler's Theorem, Primility Testing, Miller-Rabin Algorithm, Extended Euclidean Theorem, Discrete Logarithms

A prime number is an integer greater than 1 divisible only by 1 and itself (2, 3, 5, 7, 11, ...); public-key cryptography depends on being able to generate large primes and, separately, on the difficulty of factoring the product of two large primes. Primality testing checks whether a given number is prime. Trial division becomes impractical for the several-hundred-digit numbers used in RSA, so practical systems use a probabilistic test, most commonly Miller-Rabin: to test $n$, write $n - 1 = 2^k m$ with $m$ odd, pick a random witness $a$ with $1 < a < n-1$, and compute $b = a^m \bmod n$. If $b \equiv 1 \pmod n$, report probably prime. Otherwise, for $i = 0$ to $k-1$: if $b \equiv -1 \pmod n$, report probably prime and stop; else square $b \bmod n$ and continue. If the loop finishes without hitting $-1$, report composite. Testing $n = 17$ with witness $a = 5$: $n - 1 = 16 = 2^4 \times 1$, so $k=4$, $m=1$, and $b = 5^1 \bmod 17 = 5 \ne 1$. Looping: at $i=0$, $b=5 \ne 16$, so $b \leftarrow 5^2 \bmod 17 = 8$; at $i=1$, $b=8 \ne 16$, so $b \leftarrow 8^2 \bmod 17 = 64 \bmod 17 = 13$; at $i=2$, $b=13 \ne 16$, so $b \leftarrow 13^2 \bmod 17 = 169 \bmod 17 = 16$; at $i=3$, $b = 16 = n-1$, so the test reports 17 is (probably) prime, correctly, since 17 is in fact prime.

Fermat's little theorem states that if $p$ is prime and $a$ is a positive integer not divisible by $p$, then $a^{p-1} \equiv 1 \pmod p$; equivalently, without the coprimality restriction, $a^p \equiv a \pmod p$. For $a=3$, $p=5$: $3^4 = 81 \equiv 1 \pmod 5$, and $3^5 = 243 \equiv 3 \pmod 5$, matching both forms. Euler's totient function $\phi(n)$ counts the positive integers less than $n$ that are coprime to $n$. For $n=10$, the integers coprime to 10 among ${1, \ldots, 9}$ are ${1,3,7,9}$, so $\phi(10)=4$. When $n$ is prime, every integer below it is coprime to it, so $\phi(n) = n-1$ (for example $\phi(7) = 6$). When $n = pq$ for distinct primes $p, q$, $\phi(n) = (p-1)(q-1)$; for $n = 15 = 3 \times 5$, $\phi(15) = (3-1)(5-1) = 8$. Euler's theorem generalizes Fermat's: if $a$ and $n$ are coprime, $a^{\phi(n)} \equiv 1 \pmod n$. For $a=3$, $n=10$, $\phi(10)=4$: $3^4 = 81 \equiv 1 \pmod{10}$, and $3^5 \equiv 3 \pmod{10}$, exactly mirroring Fermat's theorem with $\phi(n)$ standing in for $p-1$.

An integer $a$ is a primitive root of a prime $p$ if the powers $a^1, a^2, \ldots, a^{p-1} \bmod p$ are all distinct, and therefore run through every nonzero residue $1, \ldots, p-1$ exactly once. Checking whether $2$ is a primitive root of $5$: $2^1 \bmod 5 = 2$, $2^2 \bmod 5 = 4$, $2^3 \bmod 5 = 3$, $2^4 \bmod 5 = 1$; these four values ${2,4,3,1}$ are all distinct, so $2$ is a primitive root of $5$. Given a primitive root $a$ of $p$, the discrete logarithm of $b$ base $a$ modulo $p$, written $\mathrm{dlog}_{a,p}(b)$, is the unique exponent $i$ with $b \equiv a^i \pmod p$. Since $3$ is a primitive root of $7$ (its powers $3^1, \ldots, 3^6 \bmod 7 = 3, 2, 6, 4, 5, 1$ are all distinct), and $3^3 \bmod 7 = 27 \bmod 7 = 6$, the discrete logarithm of $6$ base $3$ modulo $7$ is $3$. Computing a discrete logarithm is believed to be computationally hard for a well-chosen large prime, which is exactly the hardness assumption Diffie-Hellman and ElGamal below are built on. The extended Euclidean algorithm, covered in Unit 2 for finding $\gcd(a,b) = sa+tb$, is the standard tool for computing the modular inverses ($d = e^{-1} \bmod \phi(n)$ in RSA, or $k^{-1} \bmod q$ in DSA) that these public-key schemes need throughout their key generation and signing steps.

Public Key Cryptosystems and their Applications

Public-key cryptography uses two mathematically linked but distinct keys, a public key, freely distributed, and a private key, kept secret by its owner, rather than one key shared by both parties. A valid public-key cryptosystem must satisfy three computational conditions: encrypting or decrypting with the appropriate key must be computationally easy; deriving the private key from the public key must be computationally infeasible; and recovering the private key even from a chosen-plaintext attack (feeding chosen messages through the public encryption function and observing the output) must likewise be infeasible. Two applications follow directly from having two separate keys. Encryption for confidentiality: content encrypted with the recipient's public key can be decrypted only with the recipient's private key, so only the intended recipient can recover it, and any tampering during transit is caught because decryption also verifies that the recovered content matches what was originally encrypted. Digital signatures for authentication and integrity: content signed with the signer's private key can be verified by anyone holding the signer's public key; since only the signer could have produced a signature that verifies correctly, the recipient gets both authentication (confidence the signer really produced it) and integrity (any change to the signed content, however small, makes verification fail).

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 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$.

  1. Alice picks private $X_A$, sends $Y_A = g^{X_A} \bmod p$.
  2. Bob picks private $X_B$, sends $Y_B = g^{X_B} \bmod p$.
  3. 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:

  1. Alice sends $Y_A$; Darth intercepts it.
  2. Darth sends $Y_{D1}$ to Bob.
  3. Bob sends $Y_B$; Darth intercepts it.
  4. Darth sends $Y_{D2}$ to Alice.

Resulting keys:

PartyKey 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.

Open this answer on its own page →

Number Theory

2082 · 5 marks

State Fermat's theorem with example. What is the implication of discrete logarithm? [5]

Statement: If p is a prime number and a is a positive integer such that gcd(a, p) = 1 (i.e., a is not divisible by p), then: $$a^{p-1} \equiv 1 \pmod{p}$$ An alternative (and equally important) form of Fermat's theorem is: $$a^p \equiv a \pmod{p}$$ This holds for any integer a (even if p divides ...

Open this answer on its own page →

2080 · 5 marks

How Miller Rabin test is used for primality testing? Show whether the number 561 passes the test. [5]

  • Number to test: $n = 561$ - Test base (chosen): $a = 2$ Miller-Rabin is a probabilistic primality test based on the fact that for a prime $n$, the only square roots of $1 \pmod n$ are $\pm 1$. Algorithm: 1. Write $n - 1 = 2^k \cdot m$ where $m$ is odd. 2. Pick a base $a$ with $1 < a < n-1$. 3. ...
Open this answer on its own page →

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 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)}}$$

Open this answer on its own page →

2078 · 5 marks

Find the value of $7^{2019} \mod 13$ using Fermat's Little theorem. Define Euler totient function with an example. [5]

  • Expression to evaluate: $7^{2019} \bmod 13$ - Base $a = 7$, modulus $p = 13$ (prime), exponent $= 2019$ - Task: define Euler totient function with an example --- Fermat's Little Theorem: If $p$ is prime and $\gcd(a,p)=1$, then $$a^{p-1} \equiv 1 \pmod{p}$$ Step 1: Check condition: $13$ is prime...
Open this answer on its own page →

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 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

  1. Simplifies computation: For a prime, the totient is trivially $p-1$.
  2. 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)$$
  3. 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$:

StepComputationRemainder
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. ✓
Open this answer on its own page →

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]

  • Common prime: $p = 13$ - Public key of A: $YA = 10$ - Public key of B: $YB = 8$ - Task: find private keys $XA, XB$ and shared secret $K$, using any valid primitive root of 13. --- Definition. Given a prime $p$, a primitive root $g$, and a value $y$, the discrete logarithm problem is to find $x$...
Open this answer on its own page →

2080 · 5 marks

How does meet in middle attack work in Diffie Helman key exchange protocol? Explain. [5]

In the Diffie-Hellman (DH) protocol, two parties (Alice and Bob) agree on public parameters: - A large prime q - A primitive root a of q Each party generates a private key and computes a public key, then exchanges public keys to derive a shared secret. --- Note: In the context of Diffie-Hellman, ...

Open this answer on its own page →

2076 · 5 marks

Define discrete logarithm. Explain the procedure of sharing the secret key in Diffie Hellman. [5]

For a prime number P and a primitive root a, if: $$a^i \equiv b \pmod{P}$$ Then i is called the discrete logarithm of the number b for the base a mod P, and is denoted as: $$dloga(b) = i$$ The difficulty of computing the discrete logarithm (given b, a, and P, finding i) forms the mathematical fou...

Open this answer on its own page →

RSA Algorithm

2080

Discrete Logarithms and RSA Cryptosystem

  • Primes: $p = 13$, $q = 7$ - Plaintext: $M = \text{'hi'}$ - Encoding: $a=0, b=1, \ldots, z=25$ All required data is present. --- Given a prime $p$, a primitive root (generator) $g$, and a value $y$, the discrete logarithm is the integer $x$ such that: $$g^x \bmod p = y$$ Properties: - It is the ...
Open this answer on its own page →

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]

--- Field problem: - Set: $\mathbb{Z}5 = {0,1,2,3,4}$ under addition and multiplication mod 5. ElGamal problem: - Public key: $(q, \alpha, YA) = (101, 2, 14)$ - Message: "CSIT" - Encoding: $A=0, B=1, \dots, Z=25$ - Random number: $k = 4$ All required data present. --- A field requires: closure,...

Open this answer on its own page →

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]

If $p$ is a prime number and $a$ is an integer with $\gcd(a, p) = 1$, then: $$a^{p-1} \equiv 1 \pmod{p}$$ Let $p = 7$, $a = 3$: $$3^{6} \bmod 7$$ - $3^2 = 9 \equiv 2 \pmod 7$ - $3^3 = 27 \equiv 6 \pmod 7$ - $3^6 = (3^3)^2 = 6^2 = 36 \equiv 1 \pmod 7$ Thus $3^6 \equiv 1 \pmod 7$, confirming the th...

Open this answer on its own page →