07.07.2023 Views

Implementing-cryptography-using-python

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9 ■ Mastering Cryptography Using Python 265

def get_dh_sharedkey():

key = int(open('server_public_dh_key.pem').read())

clientDH.generateSharedKey(key)

clientDH.getSharedKey()

clientDH.generateSharedKey(key)

private_key = clientDH.key

return private_key

As with the server file, the client file needs to address the results determined

by the message sent over UDP:

# determine if the user initiated a special command

result = ct.check_client_command(data)

# handle any custom commands

if data == b'exit':

break

if result == 0:

break

if result == 10:

sendUsingPrivate = False;

if result == 11:

sendUsingPrivate = True;

skipEncryption = True;

if result == 20:

sendUsingDH = False;

if result == 21:

sendUsingDH = True;

skipEncryption = True;

Prior to going into the loop, you will need to get the client secret by calling

the get _ dh _ sharedkey() method:

# no matter what, get the ECC shared key, only use it if the user

enables

clientSecret = get_dh_sharedkey()

print ("Welcome to Crypto-Chat! \n")

print (" Enable PKI: type 'addPKI'")

print (" Disable PKI: type 'removePKI'")

print (" Enable Diffie-Hellman: type 'addDH'")

print (" Disable Diffie-Hellman: type 'removeDH'")

print ()

We also make a number of changes to the client screen to show the user the

special commands. These are nothing more than just print() calls. The final

version should look similar to Figure 9.8.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!