the mathematics of rsa encryption

38
THE MATHEMATICS OF RSA ENCRYPTION Casco Bay .NET User Group Nov. 2016

Upload: nathan-f-dunn

Post on 09-Feb-2017

73 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: The Mathematics of RSA Encryption

THE MATHEMATICS OF RSA ENCRYPTION

Casco Bay .NET User GroupNov. 2016

Page 2: The Mathematics of RSA Encryption

Goals Make encryption less mysterious

Page 3: The Mathematics of RSA Encryption

Outline What is RSA Encryption? Math Refresher How does it work? Math Deep Dive Where do keys come from? Why does it work? Why is it secure?

Page 4: The Mathematics of RSA Encryption

What is RSA Encryption? Public Key Cryptography Scheme

Page 5: The Mathematics of RSA Encryption
Page 6: The Mathematics of RSA Encryption
Page 7: The Mathematics of RSA Encryption
Page 8: The Mathematics of RSA Encryption

Math Refresher - Modulus % “The Remainder” operator 546 % 31 = 19 546 = 17*31 + 19

Page 9: The Mathematics of RSA Encryption

Math Refresher - Primes A prime can only be divided by 1 and itself Every number can be factored into a list of

primes 360 = 2 * 2 * 2 * 3 * 3 * 5 11 = 11

Two numbers are coprime if they have no common prime factors 6 = 2 * 3, 35 = 5 * 7, so 6 and 35 are coprime 26 = 2 * 13, 4 = 2 * 2, so 26 and 4 are not

coprime

Page 10: The Mathematics of RSA Encryption

How Does it Work? Public key: (e, n) Private key: (d, n) Message: M Encrypted Message: EM = M**e % n Decrypted Message: DM = EM**d % n

Page 11: The Mathematics of RSA Encryption

Example Public key: (e, n) = (3593, 150349) Private key: (d, n) = (957, 150349) Message: M = 90001 Encrypted Message: EM = M**e % n

EM = 90001**3593 % 150349 131425

Decrypted Message: DM = EM**d % n DM = 131425**957 % 150349 90001

Page 12: The Mathematics of RSA Encryption

Example Public key: (e, n) = (3593, 150349) Private key: (d, n) = (957, 150349) Message: M = 22621 Encrypted Message: EM = M**e % n

EM = 22621**3593 % 150349 62033

Decrypted Message: DM = EM**d % n DM = 62033**957 % 150349 22621

Page 13: The Mathematics of RSA Encryption

Why did that work?

Page 14: The Mathematics of RSA Encryption

Why did that work? Math

Page 15: The Mathematics of RSA Encryption

Why did that work? Math

Cleverly chosen keys

Page 16: The Mathematics of RSA Encryption

Why did that work? Math

Cleverly chosen keys Euler’s Theorem

Page 17: The Mathematics of RSA Encryption

φ, the totient function φ(n) is called the totient of n

Number of integers less than n, coprime with n n = 15 = 5 * 3 φ(15) = Number of integers coprime with 15

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 1, 2, 4, 7, 8, 11, 13, 14

φ(15) = 8

Page 18: The Mathematics of RSA Encryption

Calculating φ(n) φ(15) = 8

Notice: φ(5 * 3) = (5-1) * (3-1) n = p * q

n is the product of 2 different primes, p and q

There are p multiples of q There are q multiples of p 0 is counted twice φ(n) = p*q – p – q + 1 = (p-1) * (q-1)

Page 19: The Mathematics of RSA Encryption

Euler’s Theorem x**φ(n) % n = 1, where x is coprime with

n Euler’s Theorem predicts x**8 % 15 = 1

For x coprime with 150 1 2 3 4 5 6 7 8 9 10

11

12

13

14

0 1 1 6 1 10

6 1 1 6 10

1 6 1 10 1 2 3 4 5 6 7 8 9 1

011

12

13

14

0 1 1 6 1 10

6 1 1 6 10

1 6 1 11 2 4 7 8 11 13 141 1 1 1 1 1 1 1

Page 20: The Mathematics of RSA Encryption

Proof of Euler’s Theorem

X 1 2 4 7 8 11

13

14

1 1 2 4 7 8 11

13

14

2 2 4 8 14

1 7 11

13

4 4 8 1 13

2 14

7 11

7 7 14

13

4 11

2 1 8

8 8 1 2 11

4 13

14

7

11

11

7 14

2 13

1 8 4

13

13

11

7 1 14

8 4 2

14

14

13

11

8 7 4 2 1

Page 21: The Mathematics of RSA Encryption

Proof of Euler’s Theorem

X 1 2 4 7 8 11

13

14

1 1 2 4 7 8 11

13

14

2 2 4 8 14

1 7 11

13

4 4 8 1 13

2 14

7 11

7 7 14

13

4 11

2 1 8

8 8 1 2 11

4 13

14

7

11

11

7 14

2 13

1 8 4

13

13

11

7 1 14

8 4 2

14

14

13

11

8 7 4 2 1

Page 22: The Mathematics of RSA Encryption

Proof of Euler’s Theorem Consider the product of each number in the

first row 1*2*4*7*8*11*13*14 % 15

What if we multiply this value by 7**8? 7**8 * (1*2*4*7*8*11*13*14) % 15

(7*1)*(7*2)*(7*4)*(7*7)*(7*8)*(7*11)*(7*13)*(7*14) % 15

7*14*13*4*11*2*1*8 % 15 1*2*4*7*8*11*13*14 % 15

It didn’t change the value, so 7**8 % 15 = 1

Page 23: The Mathematics of RSA Encryption

Key Generation How did we get our keys from the

example?

Public key: (e, n) = (3593, 150349) Private key: (d, n) = (957, 150349)

Page 24: The Mathematics of RSA Encryption

Key Generation p, q = 251, 599 n = p * q

150349 e = 3593 φ(n) = (p-1) * (q-1)

149500 d*e % φ(n) = 1 solve for d

d*3593 % 149500 = 1 d = 957 is the only solution

Page 25: The Mathematics of RSA Encryption

What makes those keys work? EM = M**e % n DM = EM**d % n

DM = (M**e % n)**d % n DM = M**(e * d) % n

Page 26: The Mathematics of RSA Encryption

Why does it work? DM = M**(e*d) % n

e*d % φ(n) = 1 e*d = 1 + k*φ(n) DM = M**(1 + k*φ(n)) % n = (M**1) * (M**φ(n))**k % n = M * (1**k) % n = M

Page 27: The Mathematics of RSA Encryption

Why is it secure? Can we get the private key from the

public key? e*d % φ(n) = 1 3593*d % φ(150349) = 1

Page 28: The Mathematics of RSA Encryption

Why is it secure? Can we get the private key from the

public key? e*d % φ(n) = 1 3593*d % φ(150349) = 1

No, because factoring appears to be difficult

Page 29: The Mathematics of RSA Encryption

Why is it secure? Can we get the private key from the public

key? e*d % φ(n) = 1 3593*d % φ(150349) = 1

No, because factoring appears to be difficult RSA-200

27997833911221327870829467638722601621070446786955428537560009929326128400107609345671052955360856061822351910951365788637105954482006576775098580557613579098734950144178863178946295187237869221823983

663 bits, 2 years real time, 75 years CPU time

Page 30: The Mathematics of RSA Encryption

Why is it secure? Can we solve for M given the encrypted

message? EM = M**e % n 131435 = M**3593 % 150349

Page 31: The Mathematics of RSA Encryption

Why is it secure? Can we solve for M given the encrypted

message? EM = M**e % n 131435 = M**3593 % 150349

No, because taking the eth root (The RSA Problem) appears to be difficult

Page 32: The Mathematics of RSA Encryption

Why is it secure? Can we solve for d given a decrypted

(authenticated) message? EM = M**d % n 131435 = 90001**d % 150349

Page 33: The Mathematics of RSA Encryption

Why is it secure? Can we solve for d given a decrypted

(authenticated) message? EM = M**d % n 131435 = 90001**d % 150349

No, because the discrete logarithm appears to be difficult

Page 34: The Mathematics of RSA Encryption

Why is it secure? Can we take a guess at M, given the

encrypted message? EM = M**e % n 131435 = M**3593 % 150349

Page 35: The Mathematics of RSA Encryption

Why is it secure? Can we take a guess at M, given the

encrypted message? EM = M**e % n 131435 = M**3593 % 150349 90001**3593 % 150329 = 131435 !!

Page 36: The Mathematics of RSA Encryption

Why is it secure? Can we take a guess at M, given the

encrypted message? EM = M**e % n 131435 = M**3593 % 150349 90001**3593 % 150329 = 131435 !!

Yes, that is a “chosen plaintext attack”, and that is why you must pad your messages

Page 37: The Mathematics of RSA Encryption

More References Video demonstrating Public Key

Cryptography https://www.youtube.com/watch?v=

GSIDS_lvRv4 Wikipedia Page

https://en.wikipedia.org/wiki/RSA_(cryptosystem)

Page 38: The Mathematics of RSA Encryption

Appendix: Properties of % A + B % n = (A % n) + (B % n) % n

517 + 878 % 10 = 7 + 8 % 10 A * B % n = (A % n) * (B % n) % n

318 * 73 % 10 = 8 * 3 % 10 A ** B % n = (A % n) ** B % n ≠ (A % n) ** (B %

n) 93 ** 57 % 10 = 3 ** 57 % 10 ≠ 3 ** 7 % 10

A ** B % n = A ** (B % phi(n)) % n (For A and n coprime) 93 ** 57 % 10 = 93 ** (57 % 4) % 10

A % n = A’ => A = A’ + k*n 382 % 10 = 2 => 382 = 2 + 38*10