CS Lab 11. Today's Objectives. Prime Number Generation Implement Diffie-Hellman Key Exchange Implement RSA Encryption

Similar documents
Overview. Public Key Algorithms I

Public-key encipherment concept

Public Key Algorithms

Outline. Public Key Cryptography. Applications of Public Key Crypto. Applications (Cont d)

CSCI 454/554 Computer and Network Security. Topic 5.2 Public Key Cryptography

Public Key Algorithms

Outline. CSCI 454/554 Computer and Network Security. Introduction. Topic 5.2 Public Key Cryptography. 1. Introduction 2. RSA

What did we talk about last time? Public key cryptography A little number theory

Dr. Jinyuan (Stella) Sun Dept. of Electrical Engineering and Computer Science University of Tennessee Fall 2010

Public Key Cryptography and the RSA Cryptosystem

Kurose & Ross, Chapters (5 th ed.)

Uzzah and the Ark of the Covenant

Key Exchange. Secure Software Systems

Cryptography and Network Security. Sixth Edition by William Stallings

ח'/סיון/תשע "א. RSA: getting ready. Public Key Cryptography. Public key cryptography. Public key encryption algorithms

CSC 474/574 Information Systems Security

Introduction to Cryptography and Security Mechanisms. Abdul Hameed

A nice outline of the RSA algorithm and implementation can be found at:

An overview and Cryptographic Challenges of RSA Bhawana

Chapter 9 Public Key Cryptography. WANG YANG

Introduction to Cryptography and Security Mechanisms: Unit 5. Public-Key Encryption

Chapter 9. Public Key Cryptography, RSA And Key Management

Distributed Systems. 26. Cryptographic Systems: An Introduction. Paul Krzyzanowski. Rutgers University. Fall 2015

CS 161 Computer Security

RSA (material drawn from Avi Kak Lecture 12, Lecture Notes on "Computer and Network Security" Used in asymmetric crypto.

Public-Key Cryptography. Professor Yanmin Gong Week 3: Sep. 7

Public Key Cryptography and RSA

Algorithms (III) Yijia Chen Shanghai Jiaotong University

Modern cryptography 2. CSCI 470: Web Science Keith Vertanen

Lecture 6: Overview of Public-Key Cryptography and RSA

CS 161 Computer Security

Algorithms (III) Yijia Chen Shanghai Jiaotong University

CRYPTOGRAPHY Thursday, April 24,

Computer Security. 08. Cryptography Part II. Paul Krzyzanowski. Rutgers University. Spring 2018

Algorithms (III) Yu Yu. Shanghai Jiaotong University

Chapter 8 Security. Computer Networking: A Top Down Approach. 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012

Some Stuff About Crypto

CPSC 467b: Cryptography and Computer Security

Cryptography III. Public-Key Cryptography Digital Signatures. 2/1/18 Cryptography III

Number Theory and RSA Public-Key Encryption

Encryption. INST 346, Section 0201 April 3, 2018

CS61A Lecture #39: Cryptography

Public Key Algorithms

10.1 Introduction 10.2 Asymmetric-Key Cryptography Asymmetric-Key Cryptography 10.3 RSA Cryptosystem

CS 6324: Information Security More Info on Key Establishment: RSA, DH & QKD

ASYMMETRIC CRYPTOGRAPHY

CS669 Network Security

Secrets & Lies, Knowledge & Trust. (Modern Cryptography) COS 116 4/20/2006 Instructor: Sanjeev Arora

Chapter 3 Public Key Cryptography

Cryptography III Want to make a billion dollars? Just factor this one number!

Cryptography (DES+RSA) by Amit Konar Dept. of Math and CS, UMSL

Applied Cryptography and Computer Security CSE 664 Spring 2018

Public Key Cryptography

Davenport University ITS Lunch and Learn February 2, 2012 Sneden Center Meeting Hall Presented by: Scott Radtke

CS 556 Spring 2017 Project 3 Study of Cryptographic Techniques

Part VI. Public-key cryptography

Cryptography Symmetric Cryptography Asymmetric Cryptography Internet Communication. Telling Secrets. Secret Writing Through the Ages.

Introduction. CSE 5351: Introduction to cryptography Reading assignment: Chapter 1 of Katz & Lindell

Module: Cryptographic Protocols. Professor Patrick McDaniel Spring CMPSC443 - Introduction to Computer and Network Security

Public Key Encryption. Modified by: Dr. Ramzi Saifan

Public Key Cryptography, OpenPGP, and Enigmail. 31/5/ Geek Girls Carrffots GVA

Lecture 30. Cryptography. Symmetric Key Cryptography. Key Exchange. Advanced Encryption Standard (AES) DES. Security April 11, 2005

Cryptography & Key Exchange Protocols. Faculty of Computer Science & Engineering HCMC University of Technology

Crypto CS 485/ECE 440/CS 585 Fall 2017

RSA (algorithm) History

Activity Guide - Public Key Cryptography

Cryptography Worksheet

1.264 Lecture 28. Cryptography: Asymmetric keys

CS 161 Computer Security

Lecture 3 Algorithms with numbers (cont.)

Making and Breaking Ciphers

CS573 Data Privacy and Security. Cryptographic Primitives and Secure Multiparty Computation. Li Xiong

CSE 3461/5461: Introduction to Computer Networking and Internet Technologies. Network Security. Presentation L

ASYMMETRIC (PUBLIC-KEY) ENCRYPTION. Mihir Bellare UCSD 1

ASYMMETRIC (PUBLIC-KEY) ENCRYPTION. Mihir Bellare UCSD 1

Blum-Blum-Shub cryptosystem and generator. Blum-Blum-Shub cryptosystem and generator

Key Exchange. References: Applied Cryptography, Bruce Schneier Cryptography and Network Securiy, Willian Stallings

Computer Security 3/23/18

Introduction. Cambridge University Press Mathematics of Public Key Cryptography Steven D. Galbraith Excerpt More information

CPSC 467: Cryptography and Computer Security

ICT 6541 Applied Cryptography. Hossen Asiful Mustafa

Smalltalk 3/30/15. The Mathematics of Bitcoin Brian Heinold

Lecture 2 Applied Cryptography (Part 2)

1. Diffie-Hellman Key Exchange

CSE 127: Computer Security Cryptography. Kirill Levchenko

Senior Math Circles Cryptography and Number Theory Week 1

Channel Coding and Cryptography Part II: Introduction to Cryptography

Secure Multiparty Computation

ISA 662 Internet Security Protocols. Outline. Prime Numbers (I) Beauty of Mathematics. Division (II) Division (I)

SECURITY IN NETWORKS

Other Uses of Cryptography. Cryptography Goals. Basic Problem and Terminology. Other Uses of Cryptography. What Can Go Wrong? Why Do We Need a Key?

Crypto-systems all around us ATM machines Remote logins using SSH Web browsers (https invokes Secure Socket Layer (SSL))

Quantum Encryption Keys

Lecture 9a: Secure Sockets Layer (SSL) March, 2004

Lecture 20 Public key Crypto. Stephen Checkoway University of Illinois at Chicago CS 487 Fall 2017 Slides from Miller and Bailey s ECE 422

Encryption 2. Tom Chothia Computer Security: Lecture 3

Introduction to cryptography

TECHNISCHE UNIVERSITEIT EINDHOVEN Faculty of Mathematics and Computer Science Exam Cryptology, Tuesday 31 October 2017

Great Theoretical Ideas in Computer Science. Lecture 27: Cryptography

ENCRYPTION USING LESTER HILL CIPHER ALGORITHM

Transcription:

CS 105 - Lab 11 Today's Objectives Prime Number Generation Implement Dfie-Hellman Key Exchange Implement RSA Encryption Part 1: Dfie-Hellman Key Exchange In class you learned about the Dfie-Hellman-Merkle key exchange protocol, and how it can be used to let 2 people come up with a shared secret key without actually sending the key itself over the network. The process can be summarized with paint: Image credit: Dr. Allen, Furman University Computer Science What you're going to be doing is creating a Python version of the key exchange following the same principles outlined in your class handout. We'll be following the following algorithm:

Alice part 1 Choose prime p Choose prime q Choose secret value a Mix the "common paint" p and q with "secret color" a to get mixture A = (q ** a) % p Send p, q, and A to Bob. Bob's part With A, p, and q: Choose a secret value b Compute B = (q ** b) % p Computer Bob's K value K B as (A ** b) % p Send B back to Alice. Alice Part 2 Compute Alice's K value K A = (B ** a) % p The numbers a, b, and K are never transmitted, Alice and Bob have just enough information to independently compute K, and an eavesdropper doesn't know a or b. We'll start out with prime number generation so that we can choose p and q. Remember that prime numbers only have two factors: 1 and themselves. Suppose we want to find out a number, n, is prime. If n doesn't divide evenly into anything less than it besides 1, it should be prime. Start up IDLE and create a new function, is_prime(n), which returns True the given number n is prime and returns False n isn't prime. Test your function with the code below. # finish this function! primes = [] for i in range(1, 100): is_prime(i): primes.append(i) print(primes) To make sure you're doing it right, change the print(primes) statement to print(primes[8:12]) and write down the 4 numbers you get. We'll check 'em when we check you off! Now that we have a prime number generator, let's extend it. Write a function, get_random_prime(limit), which will generate a list of primes from 1 to limit and return a random prime from the list. Look up the random.choice() function to learn about how to get a random item from a list, or look at an example below: >>> import random >>> seq = [1,2,3,4,5] >>> random.choice(seq) 1

>>> random.choice(seq) 1 >>> random.choice(seq) 3 >>> random.choice(seq) 5 Hint: the code provided in the first exercise is great at generating the list of primes. You might want to adapt it to work for you. import random # what you wrote last time def get_random_prime(limit): # finish this function! print(get_random_prime(100)) print(get_random_prime(100)) Run your program and write down the two numbers that it gives you. Of course, they'll be random numbers but as long as they're both primes from 1 to 100 you're good. Alright, you're ready to finish the program! import random # same as last time def get_random_prime(limit): # same as last time # Generate 2 random primes p = q = print('primes: p:', p, 'q:', q) # Input a secret integer a a = # Compute A per the algorithm A = print('a is', A) # Input a secret integer b

b = # Compute B per the algorithm B = print('b is', B) # Compute Bob's K KB = # Compute Alice's K KA = print('k-values:') print('alice:', KA) print('bob: ', KB) KA == KB: print('the shared secrets match!') print("the shared secrets don't match!") Part 2: the RSA cryptosystem While symmetric key cryptography (everyone has the same key) is fun and easy. It proposes a lot of issues when it comes with dealing with confidentiality when dealing with people you have never met or are in able to safely and easily transfer a symmetric key to. For instance, I want to buy a book from Amazon.com. All my private information including credit card information needs to be secure or I will not do business with you. Also, I do not have time to go met someone to transfer a symmetric key. Finally, It s very very hard for Amazon to come up with unique, nonreusable symmetric keys for every transaction. So what do we do? Asymmetric cryptography. Simply put, asymmetric cryptography allows two dferent parties to create private and public numbers and transfer information confidentially over the Internet securely. Have you ever noticed the padlock symbol on browsers when you are on certain web sites or how some website URLs start with HTTPS rather than HTTP? That indicates that your website connection is using SSL which implements asymmetric cryptography and even that algorithm you are about to attempt to code in Python. The most famous and popular asymmetric cryptographic scheme is RSA. What follows is a straightforward mathematical description of the mechanics of RSA encryption and decryption. 1. Alice picks two giant prime numbers, p and q. The primes should be enormous, but for simplicity we assume that Alice chooses p = 17, q = 11. She must keep these numbers secret. 2. Alice multiplies them together to get another number, N. In this case N = 187. She now picks another number e, and in this case she chooses e = 7. (e and (p - 1) x (q - 1) should be relatively prime, but this is a technicality.) 3. Alice can now publish e and N in something akin to a telephone directory. Since these two numbers are necessary for encryption, they must be available to anybody who might want to encrypt a message to Alice. Together these numbers are called the public key. (As well as being part of Alice s public key, e could also be part of everybody else s public key. However, everybody must have a dferent value of N, which depends on their choice of p and q.) 4. To encrypt a message, the message must first be converted into a number, M. For example, a word is changed into ASCII binary digits, and the binary digits can be considered as a decimal number. M is then encrypted to give

the ciphertext, C, according to the formula C = M e (mod N) 5. Imagine that Bob wants to send Alice a simple kiss: just the letter X. In ASCII this is represented by 1011000, which is equivalent to 88 in decimal. So, M = 88. 6. To encrypt this message, Bob begins by looking up Alice s public key, and discovers that N = 187 and e = 7. This provides him with the encryption formula required to encrypt messages to Alice. With M = 88, the formula gives C = 88 7 (mod 187) 7. Working this out directly on a calculator is not straightforward, because the display cannot cope with such large numbers. However, there is a neat trick for calculating exponentials in modular arithmetic. We know that, since 7 = 4 + 2 + 1, 88 7 (mod 187) = [88 4 (mod 187) x 88 2 (mod 187) x 88 1 (mod 187)] (mod 187) 88 1 = 88 = 88 (mod 187) 88 2 = 7,744 = 77 (mod 187) 88 4 = 59,969,536 = 132 (mod 187) 88 7 = 88 4 x 88 2 x 88 1 = 88 x 77 x 132 = 894,432 = 11 (mod 187) Bob now sends the ciphertext, C = 11, to Alice. 8. We know that exponentials in modular arithmetic are one-way functions, so it is very dficult to work backward from C = 11 and recover the original message, M. Hence, Eve cannot decipher the message. 9. However, Alice can decipher the message because she has some special information: she knows the values of p and q. She calculates a special number, d, the decryption key, otherwise known as her private key. The number d is calculated according to the following formula e x d = 1 (mod (p-1) x (q-1)) 7 x d = 1(mod 16 x 10) 7 x d = 1 (mod 160) d = 23 (Deducing the value of d is not straightforward, but a technique known as Euclid s algorithm allows Alice to find d quickly and easily.) 10. To decrypt the message, Alice must simply use the following formula, M = C d mod(n) M = C d mod(187) M = 11 23 mod(187) M = [11 1 mod (187) x 11 2 mod (187) x 11 4 mod (187) x 11 16 mod (187)] (mod 187) M = 11 x 121 x 55 x 154 (mod 187)

M = 88 = X in ASCII Rivest, Shamir, and Adleman had created a special one-way function, one that could be reversed only by someone with access to privileged information, namely the values of p and q. Each function can be personalized by choosing p and q, which multiply together to give N. The function allows everybody to encrypt messages to a particular person by using that person s choice of N, but only the intended recipient can decrypt the message because the recipient is the only person who knows p and q, and hence the only person who knows the decryption key, d. Finish the Python representation of the algorithm below! # rsa.py - Let's practice the RSA encryption algorithm. import random # We need to write a helper function that allows us to determine two # integers are relatively prime. In other words, the only positive # integer that divides both a and b is 1. def relatively_prime(a, b): # First, determine the lesser of a and b and put it in a variable lesser = lesser = # For all i from 2 up to the lesser, see this number i divides # both a and b. If so, we have found that the numbers a and b are # not relatively prime. found_common_divisor = False i = 2 while i <= lesser: found_common_divisor = True break i += 1 found_common_divisor return False return True == True: # 1. Choose secret and distinct primes p and q. We assume they are prime! # paste this in from before def get_random_prime(limit): # paste this in from before p = get_random_prime(100) q = get_random_prime(100) # 2. Let N = pq and let M = (p-1)(q-1). N = M = # 3. Choose public encryption key e, which is a number < M and

# relatively prime to M. # Let's show the user a list of valid candidates from which to choose. print("here is a list of possible values you can choose for e:\n") i = 1 while i < M: print(i) i += 1 e = int(input("which value would you like for public encryption key e? ")) # 4. Message is x. Sender needs to transmit y = x^e mod N. x = int(input("enter numerical message x: ")) y = print("the message you send is y = ", y) # 5. Choose private decryption key d, where ed mod M = 1. # Let's show the user a list of possible choices for d. print("here is a list of possible values you can choose for d:\n") L = [] i = 1 while i < M: print(i) i += 1 d = int(input("which value would you like for private decryption key d? ")) # 6. Recipient computes z = y^d mod N, which should equal original message x. z = print("recipient computes message z = ", z) x == z: print("good news! As expected, x and z match.") print("uh-oh! Something is wrong with the implementation. x!= z.") Run the program. You'll be prompted for an encryption key and a number to encrypt. Notice that x is too large, then the final result (z) is incorrect. Why? What is the largest value of x we should input?