From 514c0e36ba55beb47015c7154bf2abdf1724bdef Mon Sep 17 00:00:00 2001 From: Bill Buchanan Date: Thu, 25 Feb 2021 18:17:48 +0000 Subject: [PATCH] Update README.MD --- unit05_key_exchange/lab/README.MD | 48 ++++++++++++++----------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/unit05_key_exchange/lab/README.MD b/unit05_key_exchange/lab/README.MD index 7d51ea5..b29d53d 100644 --- a/unit05_key_exchange/lab/README.MD +++ b/unit05_key_exchange/lab/README.MD @@ -178,26 +178,23 @@ What happens to this sequence if we use g=3? We can determine the values of g which will work for a given prime number with the following: ```python import sys -import random p=11 def getG(p): for x in range (1,p): - rand = x - exp=1 - next = rand % p + rand = x + exp=1 + next = rand % p - while (next <> 1 ): - next = (next*rand) % p - exp = exp+1 - + while (next != 1 ): + next = (next*rand) % p + exp = exp+1 + if (exp==p-1): + print (rand) - if (exp==p-1): - print rand - -print getG(p) +print (getG(p)) ``` Run the program and determine the possible g values for these prime numbers: @@ -218,7 +215,6 @@ On the Internet, find a large prime number, and determine the values of g that a We can write a Python program to implement this key exchange. Enter and run the following program: ```python import random -import base64 import hashlib import sys @@ -232,25 +228,25 @@ b=random.randint(10,20) A = (g**a) % p B = (g**b) % p -print 'g: ',g,' (a shared value), n: ',p, ' (a prime number)' +print ('g: ',g,' (a shared value), n: ',p, ' (a prime number)') -print '\nAlice calculates:' -print 'a (Alice random): ',a -print 'Alice value (A): ',A,' (g^a) mod p' +print ('\nAlice calculates:') +print ('a (Alice random): ',a) +print ('Alice value (A): ',A,' (g^a) mod p') -print '\nBob calculates:' -print 'b (Bob random): ',b -print 'Bob value (B): ',B,' (g^b) mod p' +print ('\nBob calculates:') +print ('b (Bob random): ',b) +print ('Bob value (B): ',B,' (g^b) mod p') -print '\nAlice calculates:' +print ('\nAlice calculates:') keyA=(B**a) % p -print 'Key: ',keyA,' (B^a) mod p' -print 'Key: ',hashlib.sha256(str(keyA)).hexdigest() +print ('Key: ',keyA,' (B^a) mod p') +print ('Key: ',hashlib.sha256(str(keyA).encode()).hexdigest()) -print '\nBob calculates:' +print ('\nBob calculates:') keyB=(A**b) % p -print 'Key: ',keyB,' (A^b) mod p' -print 'Key: ',hashlib.sha256(str(keyB)).hexdigest() +print ('Key: ',keyB,' (A^b) mod p') +print ('Key: ',hashlib.sha256(str(keyB).encode()).hexdigest()) ``` Pick three different values for g and p, and make sure that the Diffie Hellman key exchange works.