CS285 · Annotated SlidesChapter 4 — Number Theory and Cryptography
Chapter 4 · Annotated slides

Number Theory and Cryptography

Divisibility, primes and the Fundamental Theorem of Arithmetic, GCD and LCM, the division algorithm, mod and congruences, then classical ciphers (Caesar, shift, affine), public-key cryptography with RSA, and the Diffie–Hellman key exchange.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up
Slide 1: Slide 1
Exam weight Low

    4.1 Divisibility and Modular Arithmetic 4.3 Primes and Greatest Common Divisor 4.6 Cryptography

    2
    Exam weight Low
      Slide 3: The Integers & Division
      Exam weight Medium
      1. a | b ("a divides b") when b = ac for some integer c (a ≠ 0).
      🎯 How this can come up · 1 question
      True / False

      3 | 12 and 12 | 3.

      Slide 4: Divisors Examples
      Exam weight Low
        Slide 5: Divisors Examples
        Exam weight Low
          Slide 6: Facts: The Divides Relation
          Exam weight Low
          1. If a | b and a | c then a | (b + c); if a | b then a | bc; if a | b and b | c then a | c.
          Slide 7: Composite & Prime Numbers
          Exam weight Low
          1. Prime: integer > 1 whose only positive divisors are 1 and itself. 1 is NOT prime.
          Slide 8: Fundamental Theorem of Arithmetic
          Exam weight Low
          1. Every integer > 1 is a unique product of primes (Fundamental Theorem of Arithmetic).
          Slide 9: Theorem
          Exam weight High
          1. If n is composite, it has a prime divisor ≤ √n. So test primes up to √n only.
          🎯 How this can come up · 1 question
          MCQ

          To show 101 is prime it is enough to test divisibility by primes up to:

          Slide 10: Prime Factorization Technique
          Exam weight Low

            Prime Factorization Examples

            Exercise 1: Show that 100 is composite?

            Exercise 2: Show that 101 is prime?

            Exercise 3: Find the prime factors of 7007?

            11
            Exam weight Low

              12
              Exam weight Low
                Slide 13: Primary Testing Example
                Exam weight Low
                  Slide 14: Greatest Common Division (GCD)
                  Exam weight Low
                  1. gcd(a, b): largest integer dividing both. Relatively prime when gcd = 1.
                  Slide 15: 2 Ways to find GCD
                  Exam weight High
                  1. gcd via prime factorizations: take the MIN power of each prime. lcm: take the MAX power.
                  2. gcd(a, b)·lcm(a, b) = a·b.
                  🎯 How this can come up · 2 questions
                  MCQ

                  gcd(120, 500) =

                  MCQ

                  If gcd(a, b) = 6 and ab = 360, lcm(a, b) =

                  Slide 16: Slide 16
                  Exam weight Low

                    17
                    Exam weight Low
                      Slide 18: Greatest Common Division
                      Exam weight Low

                        19
                        Exam weight Low
                          Slide 20: Greatest Common Division Relatively Prime
                          Exam weight Low
                          1. Pairwise relatively prime: every pair has gcd 1.
                          Slide 21: Greatest Common Division Relatively Prime
                          Exam weight Low
                            Slide 22: Least Common Multiple
                            Exam weight Low
                              Slide 23: Slide 23
                              Exam weight Low
                                Slide 24: Division
                                Exam weight Low
                                  Slide 25: The Division “Algorithm”
                                  Exam weight High
                                  1. Division algorithm: a = dq + r with 0 ≤ r < d; q = a div d, r = a mod d.
                                  Exam trapThe remainder is never negative: −11 div 3 = −4, −11 mod 3 = 1 (−11 = 3·(−4) + 1).
                                  🎯 How this can come up · 1 question
                                  MCQ

                                  −11 mod 3 =

                                  Slide 26: Examples
                                  Exam weight Low
                                    Slide 27: Solutions
                                    Exam weight Low
                                      Slide 28: mod Function Start Here
                                      Exam weight Low
                                        Slide 29: mod Function
                                        Exam weight Low

                                          Question

                                          ·What time it will be (on a 24-hour clock) 50 hours from 11 am?

                                          ·After 24 hours it will be 11 am ·After another 24 hours it will be 11 am

                                          • This is after 48 hours

                                          ·After 2 more hours it will be 13 (1 pm)!

                                          • This is after 50 hours

                                          ·Is there another way?

                                          30
                                          Exam weight Low

                                            Question

                                            ·What time it will be (on a 24-hour clock) 50 hours from 11 am?

                                            · Calculate (11 + 50) mod 24?

                                            ·We can also use mod Function to convert time from 24-hour clock to 12-hour

                                            clock

                                            ·Calculate 13 mod 12?

                                            ·Calculate 23 mod 12?

                                            31
                                            Exam weight Low
                                              Slide 32: Modular Congruence
                                              Exam weight High
                                              1. a ≡ b (mod m) when m | (a − b), i.e. a mod m = b mod m.
                                              🎯 How this can come up · 1 question
                                              True / False

                                              17 ≡ 5 (mod 6).

                                              Slide 33: Examples
                                              Exam weight Low
                                                Slide 34: Useful Congruence Theorems
                                                Exam weight Low
                                                1. If a ≡ b and c ≡ d (mod m) then a + c ≡ b + d and ac ≡ bd (mod m).
                                                Slide 35: Ex.:
                                                Exam weight Low

                                                  4.6 Cryptography

                                                  Slide 36
                                                  36
                                                  Exam weight Low

                                                    Section Summary

                                                    Classical Cryptography Cryptosystems Public Key Cryptography RSA Cryptosystem Cryptographic Protocols Primitive Roots and Discrete Logarithms

                                                    37
                                                    Exam weight Low

                                                      Why Cryptography

                                                      Cryptography is a science that applies complex mathematics and

                                                      logic to design strong encryption methods.

                                                      Cryptography is also an art.

                                                      Cryptography allows people to keep confidence in the electronic

                                                      world.

                                                      People can do their business on electric channel without worrying of

                                                      deceit and deception.

                                                      38
                                                      Exam weight Low

                                                        Symmetric Encryption (single-key encryption)

                                                        39
                                                        Exam weight Low

                                                          Symmetric Encryption

                                                          The universal technique for providing confidentiality also referred to as single-key encryption Algorithms: DES, Triple DES, and AES Two requirements for secure use:

                                                          • need a strong encryption algorithm
                                                          • sender and receiver must have obtained copies of the secret key in a secure fashion
                                                            • and must keep the key secure
                                                          Symmetric Encryption
                                                          40
                                                          Exam weight Low
                                                          1. Symmetric encryption: the same secret key encrypts and decrypts.
                                                          Slide 41: Symmetric Encryption
                                                          Exam weight Low
                                                            Slide 42: Caesar Cipher
                                                            Exam weight High
                                                            1. Caesar cipher: f(p) = (p + 3) mod 26; decrypt with (c − 3) mod 26.
                                                            🎯 How this can come up · 1 question
                                                            MCQ

                                                            Caesar (shift 3) encrypts "HI" as:

                                                            Slide 43: Caesar cipher
                                                            Exam weight Low
                                                              Slide 44: Caesar Cipher
                                                              Exam weight Low
                                                                Slide 45: Caesar Cipher
                                                                Exam weight Low
                                                                  Slide 46: Shift Cipher
                                                                  Exam weight Low
                                                                  1. Shift cipher: f(p) = (p + k) mod 26, f⁻¹(c) = (c − k) mod 26.
                                                                  Slide 47: Shift Cipher
                                                                  Exam weight Low
                                                                    Slide 48: Shift Cipher
                                                                    Exam weight Low
                                                                      Slide 49: Shift Cipher
                                                                      Exam weight Low
                                                                        Slide 50: Affine Ciphers
                                                                        Exam weight Medium
                                                                        1. Affine cipher: f(p) = (ap + b) mod 26 with gcd(a, 26) = 1 so it can be inverted.
                                                                        🎯 How this can come up · 1 question
                                                                        MCQ

                                                                        In an affine cipher f(p) = (ap + b) mod 26, a must satisfy:

                                                                        Slide 51: Cryptanalysis of Affine Ciphers
                                                                        Exam weight Low
                                                                        1. Frequency analysis breaks shift and affine ciphers (E, T, A are the most common letters).
                                                                        Slide 52: Cryptosystems
                                                                        Exam weight Low
                                                                          Slide 53: Cryptosystems
                                                                          Exam weight Low

                                                                            Public Key Cryptography

                                                                            54
                                                                            Exam weight Low

                                                                              Public Key Cryptography

                                                                              All classical ciphers, including shift and affine ciphers, are private key cryptosystems. Knowing the encryption key allows one to quickly determine the decryption key.

                                                                              All parties who wish to communicate using a private key cryptosystem must share the key and keep it a secret.

                                                                              In public key cryptosystems, first invented in the 1970

                                                                              s, knowing how to

                                                                              encrypt a message does not help one to decrypt the message. Therefore, everyone can have a publicly known encryption key. The only key that needs to be kept secret is the decryption key.

                                                                              55
                                                                              Exam weight Low
                                                                              1. Public key: anyone can encrypt with the public key; only the private key decrypts.
                                                                              Slide 56: Clifford Cocks
                                                                              Exam weight Low

                                                                                The RSA Cryptosystem

                                                                                The RSA algorithm involves four steps: key generation, key distribution, encryption and decryption.

                                                                                The public encryption key is (n,e ), where n = pq, the modulus is the product of two large primes p and q ,say with 200 digits each, and an exponent e that is relatively prime to (p

                                                                                q

                                                                                The two large primes can be quickly found using probabilistic primarily

                                                                                −1)(−1).

                                                                                tests, discussed earlier.

                                                                                But

                                                                                n = pq, with approximately 400 digits, cannot be factored in a reasonable

                                                                                length of time.

                                                                                57
                                                                                Exam weight High
                                                                                1. RSA: n = pq, choose e with gcd(e, (p−1)(q−1)) = 1, d = e⁻¹ mod (p−1)(q−1).
                                                                                Slide 58: Slide 58
                                                                                Exam weight Low
                                                                                  Slide 59: Slide 59
                                                                                  Exam weight Low

                                                                                    The RSA Cryptosystem

                                                                                    Step-1: Select two prime numbers p and q where p ≠ q.

                                                                                    Step-2: Calculate n = p * q.

                                                                                    Step-3: Calculate n) = (p-1) * (q-1).

                                                                                    Ф(

                                                                                    Step -4: Select e such that, e is relatively prime to n), i.e. (e, n)) = 1 and 1 < e < n)

                                                                                    Ф(

                                                                                    Ф(

                                                                                    Ф(

                                                                                    Step-5: Calculate d = e -1 mod n) or ed = 1 mod n).

                                                                                    Ф(

                                                                                    Ф(

                                                                                    Step-6: Public key = {e, n}, private key = {d, n}.

                                                                                    Step-7: Find out cipher text using the formula, C = P

                                                                                    e mod n where, P < n where C = Cipher text, P = Plain text, e = Encryption key and n=block size.

                                                                                    Step-8: P = C

                                                                                    d mod n. Plain text P can be obtain using the given formula. where, d =decryption key

                                                                                    60
                                                                                    Exam weight Low

                                                                                      RSA algorithm explanation -step by step example

                                                                                      Step 1: Select two prime numbers p and q where p ≠ q.

                                                                                      • Example, Two prime numbers p = 13, q = 11.

                                                                                      Step – 2: Calculate n = p * q.

                                                                                      Example, n = p * q = 13 * 11 = 143.

                                                                                      Step – 3: Calculate Ф(n) = (p

                                                                                      -1) * (q-1).

                                                                                      Example, Ф(n) = (13 –

                                                                                      1. * (11 – 1) = 12 * 10 = 120.

                                                                                      Step – 4: Select e such that,

                                                                                      1. coprime with n and Ф(n), i.e. (e, Ф(n)) = 1 and

                                                                                      2- 1 < e < Ф(n).

                                                                                      Example, Select e = 13, gcd (13, 120) = 1.

                                                                                      61
                                                                                      Exam weight Low

                                                                                        Continue...

                                                                                        Step – 5: Calculate d = e -1 mod n) or e * d = 1 mod n)

                                                                                        Ф(

                                                                                        Ф(

                                                                                        Example, Finding d: e * d mod n) = 1

                                                                                        Ф(

                                                                                        13 * d mod 120 = 1 (How to find: d *e = 1 mod n) d = ((n) * i) + 1) / e

                                                                                        Ф(

                                                                                        Ф(

                                                                                        d = (120 + 1) / 13 = 9.30 (i = 1) d = (240 + 1) / 13 = 18.53 (i = 2)

                                                                                        ∵

                                                                                        d = (360 + 1) / 13 = 27.76 (i = 3)

                                                                                        ∵

                                                                                        d = (480 + 1) / 13 = 37 (i = 4))

                                                                                        ∵

                                                                                        ∵

                                                                                        62
                                                                                        Exam weight Low

                                                                                          Continue...

                                                                                          Step – 6: Public key = {e, n}, private key = {d, n}.

                                                                                          Example, Public key = {13, 143} and private key = {37, 143}.

                                                                                          Step – 7: Find out cipher text C using the formula, C = P e mod n where, P < n.

                                                                                          Encryption Example, Plain text P = 13. (Where, P < n) C = P e mod n = 13 13 mod 143 = 52.

                                                                                          Step – 8: P = C

                                                                                          d mod n. Plain text P can be obtain using the given formula.

                                                                                          Decryption Example, Cipher text C = 52 P = C d mod n = 52 37 mod 143 = 13.

                                                                                          63
                                                                                          Exam weight Low

                                                                                            Example

                                                                                            Encrypt (C = P

                                                                                            e mod n) the message B using the RSA cryptosystem with key

                                                                                            (5,14):

                                                                                            5 mod14= 32 mod 14 = 4

                                                                                            2 4 D

                                                                                            Decrypt (P = C

                                                                                            d mod n) the cipher text D using the RSA cryptosystem with key

                                                                                            (11,14):

                                                                                            4 mod 14 =4194304 mod 14 ≈2

                                                                                            11

                                                                                            2 B

                                                                                            64
                                                                                            Exam weight Low
                                                                                              Slide 65: RSA Encryption
                                                                                              Exam weight Low
                                                                                              1. Encrypt: C = Mᵉ mod n.
                                                                                              Slide 66: RSA Encryption- example
                                                                                              Exam weight Low
                                                                                                Slide 67: RSA Decryption
                                                                                                Exam weight High
                                                                                                1. Decrypt: M = Cᵈ mod n.
                                                                                                🎯 How this can come up · 2 questions
                                                                                                Written

                                                                                                RSA with p = 3, q = 11, e = 3. Find n, φ, and d.

                                                                                                MCQ

                                                                                                In RSA, which key is kept secret?

                                                                                                RSA Decryption- example

                                                                                                Example: The message 0981 0461

                                                                                                is received. What is the decrypted message if it was

                                                                                                encrypted using the RSA cipher from the previous example.

                                                                                                Solution: The message was encrypted with

                                                                                                n = 43 59 = 2537 and exponent 13. An inverse

                                                                                                of 13 modulo 42 58 = 2436 (

                                                                                                exercise in Section 4.4) is d

                                                                                                = 937.

                                                                                                C M C 937 mod 2537.

                                                                                                ∙

                                                                                                • To decrypt a block, = ∙
                                                                                                • Since 0981 937 mod 2537 = 0704 and 0461 mod 2537 = 1115, the decrypted message is 0704 937 1115. Translating back to English letters, the message is HELP.
                                                                                                • Here is a link to RSA calculator: https://www.cs.drexel.edu/~jpopyack/IntroCS/HW/RSAWorksheet.html
                                                                                                68
                                                                                                Exam weight Low
                                                                                                  Slide 69: Slide 69
                                                                                                  Exam weight Low

                                                                                                    70
                                                                                                    Exam weight Low

                                                                                                      71
                                                                                                      Exam weight Low

                                                                                                        72
                                                                                                        Exam weight Low

                                                                                                          73
                                                                                                          Exam weight Low

                                                                                                            74
                                                                                                            Exam weight Low

                                                                                                              Diffie-Hellman

                                                                                                              The Diffie Hellman algorithm widely known as Key exchange algorithm or key agreement algorithm developed by Whitefield Diffie and Martin Hellman in 1976. The purpose of the algorithm is to enable two users to securely exchange a key that can be used for subsequent encryption of messages. The algorithm itself is limited to the exchange of secret values.

                                                                                                              Step-1: Select q (prime number) and is primitive root of q)

                                                                                                              α (α

                                                                                                              Step-2: User A Key Generation: select X, X < q

                                                                                                              A A

                                                                                                              Calculate public key Y, Y = X mod q

                                                                                                              α

                                                                                                              A A A

                                                                                                              Y Shared with user B

                                                                                                              A

                                                                                                              Step-3: User B Key Generation: select X, X < q

                                                                                                              B B

                                                                                                              Calculate public key Y, Y = α

                                                                                                              X mod q

                                                                                                              B B B

                                                                                                              Y shared with user A

                                                                                                              B

                                                                                                              Step-4: Calculation of secret key by user A: K = (Y) X mod q

                                                                                                              B A

                                                                                                              Step-5: Calculation of secret key by user B: K = (Y) X mod q

                                                                                                              A B

                                                                                                              75
                                                                                                              Exam weight High
                                                                                                              1. Diffie–Hellman: agree on prime p and base g; Alice sends gᵃ mod p, Bob sends gᵇ mod p; both compute the shared key gᵃᵇ mod p.
                                                                                                              🎯 How this can come up · 1 question
                                                                                                              MCQ

                                                                                                              p = 23, g = 5, a = 6, b = 15. Alice sends:

                                                                                                              Slide 76: Diffie Helman Step by Step
                                                                                                              Exam weight Low

                                                                                                                Diffie Helman Step by Step- Cont

                                                                                                                Step – 4: Calculation of secret key by user A:

                                                                                                                K = (Y) X mod q

                                                                                                                B A

                                                                                                                Example, Here Y = 4, X = 6

                                                                                                                B

                                                                                                                A

                                                                                                                Calculate K = (Y)

                                                                                                                X mod q 4 mod 7 1

                                                                                                                B A

                                                                                                                Step – 5: Calculation of secret key by user B:

                                                                                                                ⇒

                                                                                                                ⇒

                                                                                                                K = (Y)

                                                                                                                X mod q

                                                                                                                A B

                                                                                                                Example, Here Y = 1, X = 4

                                                                                                                A

                                                                                                                B

                                                                                                                Calculate K = (Y) X mod q 1 4 mod 7 1

                                                                                                                A B

                                                                                                                ⇒

                                                                                                                ⇒

                                                                                                                77
                                                                                                                Exam weight Low
                                                                                                                  Slide 78: Slide 78
                                                                                                                  Exam weight Low

                                                                                                                    79
                                                                                                                    Exam weight Low

                                                                                                                      80
                                                                                                                      Exam weight Low

                                                                                                                        81
                                                                                                                        Exam weight Low
                                                                                                                          Slide 82: Cryptographic Protocols: Key Exchange
                                                                                                                          Exam weight Low
                                                                                                                            Slide 83: Cryptographic Protocols: Digital Signatures
                                                                                                                            Exam weight Low
                                                                                                                              Slide 84: Cryptographic Protocols: Digital Signatures
                                                                                                                              Exam weight Low