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 263

serverDH.generateSharedKey(key)

#serverDH.displayParameters()

#serverDH.displayShared()

return (serverDH.sharedSecret)

def get_dh_sharedkey():

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

serverDH.generateSharedKey(key)

serverDH.getSharedKey()

serverDH.generateSharedKey(key)

#serverDH.displayParameters()

#serverDH.displayShared()

private_key = serverDH.key

return private_key

In the main method, you will also need to update the examination of the

results so that you can set the correct Boolean operators:

# check to see if the user typed a special command such as addPKI or

addDH

result = crypto.check_server_command(plaintext)

if result == 10: # encryption has been disabled so no message

plaintext = b'PKI Encryption disabled!'

elif result == 11: # encryption enabled

plaintext = b'PKI Encryption enabled!'

elif result == 20: # dh enabled

clientKey = plaintext

plaintext = b'Diffie-Hellman disabled!'

elif result == 21: # encryption enabled

plaintext = b'Diffie-Hellman enabled!'

# messages are received encoded so you must decode the message for

processing

msg = str(plaintext, 'utf-8')

# process any client special commands

if result == 0:

# no encryption

break

if result == 10:

# turn off the use of PKI

useClientPKI = False;

if result == 11:

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

Saved successfully!

Ooh no, something went wrong!