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.

264 Chapter 9 ■ Mastering Cryptography Using Python

# turn on the use of PKI

useClientPKI = True;

# let the user know PKI certs are found

print ("Client certificate found ...")

if result == 20:

# turn off Diffie-Hellman

useDHKey = False;

if result == 21:

# turn on Diffie-Hellman

useDHKey = True;

print ("DH Key Exchange ...")

serverSecret = get_dh_sharedkey()

# if any encryption is used, change the message to 'secure' message

if useClientPKI == True or useDHKey == True:

print ("Received secured message: " + msg)

else:

print ("Received message: " + msg)

Modifying the Client File

The client will call the gen _ client _ DH() method to generate the certificates

when the application starts up. The certificates need to be in place prior to the

client requiring them, which takes place when the user types addDH on the client.

In addition to the Diffie-Hellman certificates that get created, we also need to

implement the Diffie-Hellman methods to examine the server’s public key and

use it to create a shared secret using the client’s private key. The same process

happens on the server.

The following code was added to the client file:

import Chapter9.ch9_crypto_chat as ct

clientDH = clientDH = ct.gen_client_DH()

clientSecret = 0;

def get_dh_sharedsecret():

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

clientDH.generateSharedKey(key)

clientDH.getSharedKey()

clientDH.generateSharedKey(key)

shared_key = clientDH.sharedSecret

return (shared_key)

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

Saved successfully!

Ooh no, something went wrong!