03 CS285 Number Theory and Cryptography Chap4 Updated
03 CS285 Number Theory and Cryptography Chap4 Updated
Cryptography
CHA PT ER 4
S EC T ION 4 . 1 , 4 . 3 , 4 . 6
4.1 Divisibility and Modular Arithmetic
4.3 Primes and Greatest Common Divisor
4.6 Cryptography
The Integers & Division
divide_
n
n
intafford
Divisors Examples
11 774 7
false
14 1 17 True
24 1 24 True
false
24 0 0 True
4
Divisors Examples
5
Facts: The Divides Relation
1131213 18 3 1218
3121 3 18 73 30
26 726.8
2624248
48 816
Composite & Prime Numbers
9itself
49
Fundamental Theorem of Arithmetic
15522.1
2
ifL
Prime Factorization Technique any
int
important
2 n
3 n 10
5 n
Prime Factorization Examples
Exercise 1: Show that 100 is composite?
I
me
If a
to
IEEE a
E
217007 317007
E.gg kid I E in 7007721
7 7007 507007 is a
11
12
Primary Testing Example
13
Greatest Common Division (GCD)
2int
14
2 Ways to find GCD
method 1
metritis number
I fi i
36 2
36 22
3
god 24136
16
2
120
60 2
so 2 23 5 3
15 5
3 3 august
smog
l l l
go 2 50
2 533
17
420 2 98 2
Greatest Common Division
I
24
Did 2 7 14
30
18
17 17
I l
ged 17,22 1
E
F
we have 1
Gt 1 is not 19
Prime number
Greatest Common Division
Relatively Prime
gcdcn tt ll
gd33.71
¾ ¾ sina
sn 223 12
¾ gcd 2425
atively
Prime
20
Greatest Common Division
Relatively Prime
21
Least Common Multiple
100k the biggest c f
p
Eg
6k
we look
math
c
23
Division
The Division
“Algorithm”
Examples
Solutions
mod Function Start Here
4
find reminder
9
17
U
6
28
ive
mod Function
Question
·What time it will be (on a 24-hour clock) 50 hours from 11 am?
0 EE
· Calculate (11 + 50) mod 24?
·We can also use mod Function to convert time from 24-hour clock to 12-hour
clock
modems
i
20
Examples
crf or Q 17 5most
17mod615mod6
6 55
5
6 17 5 6 12 2
24 14 mod 6
yes they're congruent
24mod6 14mod6
6 FI or
no
Te Titstop
here
Useful Congruence Theorems
Ex.:
0
farity
4.6 Cryptography
Td g
Section Summary
Classical Cryptography
Cryptosystems
Public Key Cryptography
RSA Cryptosystem
Cryptographic Protocols
Primitive Roots and Discrete Logarithms
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.
Symmetric Encryption (single-key encryption)
Symmetric Encryption
The universal technique for providing confidentiality
also referred to as single-key encryption
Algorithms: DES, Triple DES, and AES
we_
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
TEiii.tn
HEI
F.e
en.pe woot
city equation
t.tt
Caesar Cipher
Julius Caesar created secret messages by shifting each letter three letters
forward in the alphabet (sending the last three letters to the first three
letters.)
AIDEF
For example, the letter B is replaced by E and the letter X is replaced by A.
This process of making a message secret is an example of encryption.
Here is how the encryption process works:
◦ Replace each letter by an integer from Z26, that is an integer from 0 to 25
representing one less than its position in the alphabet.
t.net
is
◦ The encryption function is f(p) = (p + 3) mod 26. It replaces each integer p in
the set {0,1,2,…,25} by f(p) in the set {0,1,2,…,25} .
AB PE
◦ Replace each integer p by the letter with the position p + 1 in the alphabet.
Caesar cipher
Caesar Cipher
etm
orExample: Encrypt the message “MEET YOU IN THE PARK” using the Caesar cipher.
Solution: 12 4 4 19 24 14 20 8 13 19 7 4 15 0 17 10.
Now replace each of these numbers p by f(p) = (p + 3) mod 26.
15 7 7 22 1 17 23 11 16 22 10 7 18 3 20 13.
Translating the numbers back to letters produces the encrypted message
“PHHW BRX LQ WKH SDUN.”
f 12 26 19
12 3 mod26 15mod
Ei
zu 24 3 mod26 27mod26
in 14 3 mod 26 17mod 26 17
26 23
SC
Caesar Cipher
To recover the original message, use f−1(p) = (p−3) mod 26. So, each
letter in the coded message is shifted back three letters in the
alphabet, with the first three letters sent to the last three letters. This
process of recovering the original message from the encrypted
message is called decryption.
The Caesar cipher is one of a family of ciphers called shift ciphers. Letters
can be shifted by an integer k, with 3 being just one possibility. The
encryption function is
f(p) = (p + k) mod 26
and the decryption function is
f−1(p) = (p−k) mod 26
The integer k is called a key.
Shift Cipher 018,45 s
Example 1: Encrypt the message “STOP GLOBAL WARMING” using the shift cipher with
k = 11.
apply encrption function f p et k mod26 11mod26
iieiiiiiiiii.is
DEZA
Shift Cipher
Example 1: Encrypt the message “STOP GLOBAL WARMING” using the shift cipher with
k = 11.
Solution: Replace each letter with the corresponding element of Z26.
18 19 14 15 6 11 14 1 0 11 22 0 17 12 8 13 6.
Apply the shift f(p) = (p + 11) mod 26, yielding
3 4 25 0 17 22 25 12 11 22 7 11 2 23 19 24 17.
Translating the numbers back to letters produces the ciphertext
“DEZA RWZMLW HLCXTYR.”
Shift Cipher
Example 2: Decrypt the message “LEWLYPLUJL PZ H NYLHA ALHJOLY” that was
encrypted using the shift cipher with k = 7.
Shift Cipher cypher text if
Example 2: Decrypt the message “LEWLYPLUJL PZ H NYLHA ALHJOLY” that was
encrypted using the shift cipher with k = 7.
Solution: Replace each letter with the corresponding element of Z26.
11 4 22 11 24 15 11 20 9 11 15 25 7 13 24 11 7 0 0 11 7 9 14 11 24.
Shift each of the numbers by −k = −7 modulo 26, yielding f p p 7 mod 26
0 Eretria
4 23 15 4 17 8 4 13 2 4 8 18 0 6 17 4 0 19 19 4 0 2 7 4 17.
Translating the numbers back to letters produces the decrypted message
4g “EXPERIENCE IS A GREAT TEACHER.”
mymoiiii
◦ Find the frequency of the letters in the ciphertext.
◦ Hypothesize that the most frequent letter is produced by encrypting E.
◦ If the value of the shift from E to the most frequent letter is k, shift the ciphertext by −k and see if it makes
sense.
x.im
◦ If not, try T as a hypothesis and continue.
Example: We intercepted the message “ZNK KGXRE HOXJ MKZY ZNK CUXS” that we know was
produced by a shift cipher. Let’s try to cryptanalyze.
ii onsia
Solution: The most common letter in the ciphertext is K. So perhaps the letters were shifted by 6
since this would then map E to K. Shifting the entire message by −6 gives us “THE EARLY BIRD
GETS THE WORM.”
Cryptosystems
Definition: A cryptosystem is a five-tuple (P,C,K,E,D), where
◦ P is the set of plainntext strings,
◦ C is the set of ciphertext strings,
◦ K is the keyspace (set of all possible keys),
◦ E is the set of encryption functions, and
◦ D is the set of decryption functions.
The encryption function in E corresponding to the key k is denoted by Ek and the
decryption function in D that decrypts cipher text encrypted using Ek is denoted
by Dk. Therefore:
Dk(Ek(p)) = p, for all plaintext strings p.
Cryptosystems
Example: Describe the family of shift ciphers as a cryptosystem.
Solution: Assume the messages are strings consisting of elements in Z26.
◦ P is the set of strings of elements in Z26,
◦ C is the set of strings of elements in Z26,
◦ K = Z26,
◦ E consists of functions of the form
Ek (p) = (p + k) mod 26 , and
◦ D is the same as E where Dk (p) = (p − k) mod 26 .
Public Key Cryptography
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 1970s, 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.
Clifford Cocks
The RSA Cryptosystem (Born 1950)
A public key cryptosystem, now known as the RSA system was introduced in
1976 by three researchers at MIT.
Leonard
Ronald Rivest Adi Shamir Adelman
(Born 1948) (Born 1952) (Born 1945)
It is now known that the method was discovered earlier by Clifford Cocks,
working secretly for the UK government.
The RSA Cryptosystem
The RSA algorithm involves four steps: key generation, key distribution,
encryption and decryption.
penigtion irivener
The public encryption key is (n,e), where n = pq, the modulus is the product of
re each,
two large primes p and q ,say with 200 digits s s crossinum
and an exponent e that is
relatively prime to (p−1)(q −1).
The two large primes can be quickly found using probabilistic primarily
tests, discussed earlier.
But n = pq, with approximately 400 digits, cannot be factored in a reasonable
length of time.
P x q
The RSA Cryptosystem
Step-1: Select two prime numbers p and q where p ≠ q.
7
Step-2: Calculate n = p * q. shouting
Step-3: Calculate Ф(n) = (p-1) * (q-1).
sina.se
pox
Step
-4: Select e such that, e is relatively prime to Ф(n), i.e. (e, Ф(n)) = 1 and 1 < e < Ф(n) e 2 ged e 12
accrupt
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,
cighent pl.in
C = Pe mod n where, P < n where C = Cipher text, P = Plain text, e = Encryption key and n=block size.
Step-8: P = Cd mod n. Plain text P can be obtain using the given formula. where, d =decryption key
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.
66
Example
Encrypt (C = Pe
12
mod n ) the message B using the RSA cryptosystem with key
e n
(5,14):
c phoan25 mod14= 32 mod 14 = 4 D
4D
Decrypt (P = Cd mod n ) the cipher text D using the RSA cryptosystem with key
d n
(11,14): p c mod n
411 mod 14 =4194304 mod 14 ≈2
2 B
RSA Encryption
To encrypt a message using RSA using a key (n,e) :
1. Translate the plaintext message M into sequences of two digit integers
representing the letters. Use 00 for A, 01 for B, … 25 for Z.
2. Concatenate the two digit integers into strings of digits.
3. Divide this string into equally sized blocks of 2N digits (N = number of
letters in the block) where 2N is the largest even number 2525…25 with
2N digits that does not exceed n.
4. The plaintext message M is now a sequence of integers m1,m2,…,mk.
5. Each block (an integer) is encrypted using the function C = Me mod n.
RSA Encryption- example
Example: Encrypt the message STOP using the RSA cryptosystem with key(2537,13).
◦ 2537 = 43∙ 59,
◦ p = 43 and q = 59 are primes and gcd(e,(p−1)(q −1)) = gcd(13, 42∙ 58) = 1.
is iii iii.it
I
I iiiiiiiiiiii 72
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 XA, XA < q
Calculate public key YA, YA = αXA mod q
YA Shared with user B
Step-3: User B Key Generation: select XB, XB < q
Calculate public key YB, YB = αXB mod q
YB shared with user A
Step-4: Calculation of secret key by user A: K = (YB)XA mod q
Step-5: Calculation of secret key by user B: K = (YA)XB mod q
78
Diffie Helman Step by Step
Step – 1: Select q (prime number) and α (α is primitive root of q)
Example, here q = 7, α = 17.
Step – 2: User A Key Generation: Select XA < q,
calculate public key YA and shared with user B: YA = αXA mod q
Example, Here XA = 6,
Calculate YA = αXA mod q ⇒176 mod 7 ⇒ 1
Step – 3: User B Key Generation: Select XB < q,
calculate public key YB and shared with user A: YB = αXB mod q
Example, Here XB = 4,
Calculate YB = αXB mod q ⇒174 mod 7 ⇒ 4
Diffie Helman Step by Step- Cont
Step – 4: Calculation of secret key by user A:
K = (YB) XA mod q
Example, Here YB = 4, XA = 6
Calculate K = (YB) XA mod q ⇒46 mod 7 ⇒ 1
Step – 5: Calculation of secret key by user B:
K = (YA) XB mod q
Example, Here YA = 1, XB = 4
Calculate K = (YA) XB mod q ⇒14 mod 7 ⇒ 1
81
Cryptographic Protocols: Key Exchange
Cryptographic protocols are exchanges of messages carried out by two or more parties to achieve a particular security goal.
Key exchange is a protocol by which two parties can exchange a secret key over an insecure channel without having any past
shared secret information. Here the Diffe-Hellman key agreement protocol is described by example.
1. Suppose that Alice and Bob want to share a common key.
2. Alice and Bob agree to use a prime p and a primitive root a of p.
3. Alice chooses a secret integer k1 and sends ak1 mod p to Bob.
4. Bob chooses a secret integer k2 and sends ak2 mod p to Alice.
5. Alice computes (ak2)k1 mod p.
6. Bob computes (ak1)k2 mod p.
At the end of the protocol, Alice and Bob have their shared key
(ak2)k1 mod p = (ak1)k2 mod p.
To find the secret information from the public information would require the adversary to find k1 and k2 from ak1 mod p and
ak2 mod p respectively. This is an instance of the discrete logarithm problem, considered to be computationally infeasible
when p and a are sufficiently large.
Cryptographic Protocols: Digital Signatures
Adding a digital signature to a message is a way of ensuring the recipient that the message
came from the purported sender.
Suppose that Alice’s RSA public key is (n,e) and her private key is d. Alice encrypts a plain text
message x using E(n,e) (x)= xd mod n. She decrypts a ciphertext message y using D(n,e) (y)= yd mod
n.
Alice wants to send a message M so that everyone who receives the message knows that it
came from her.
1. She translates the message to numerical equivalents and splits into blocks m1, m2 , m3,….
just as in RSA encryption.
2. She then applies her decryption function D(n,e) to the blocks and sends the results to all
intended recipients.
3. The recipients apply Alice’s encryption function and the result is the original plain text
since E(n,e) (D(n,e) (x))= x.
Everyone who receives the message can then be certain that it came from Alice.
Cryptographic Protocols: Digital Signatures
Example: Suppose Alice’s RSA cryptosystem is the same as in the earlier example with key(2537,13),
2537 = 43∙ 59, p = 43 and q = 59 are primes and gcd(e,(p−1)(q −1)) = gcd(13, 42∙ 58) = 1.
Her decryption key is d = 937.
She wants to send the message “MEET AT NOON” to her friends so that they can be certain that
the message is from her.
Solution: Alice translates the message into blocks of digits 1204 0419 0019 1314 1413.
1. She then applies her decryption transformation D(2537,13) (x)= x937 mod 2537 to each block.
2. She finds (using her laptop, programming skills, and knowledge of discrete mathematics) that 1204937
mod 2537 = 817, 419937 mod 2537 = 555 , 19937 mod 2537 = 1310, 1314937 mod 2537 = 2173, and
1413937 mod 2537 = 1026.
3. She sends 0817 0555 1310 2173 1026.
When one of her friends receive the message, they apply Alice’s encryption transformation E(2537,13)
to each block. They then obtain the original message which they translate back to English letters.