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 257

The command being used in this example is addPKI:

# send the data packet for decryption

plaintext = decrypt(data, useClientPKI, useDHKey, serverSecret)

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

addDH

result = ct.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!'

# 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:

# turn on the use of PKI

useClientPKI = True;

# let the user know PKI is certs are found

print ("Client certificate found ...")

Modifying the Client

The primary modifications to the client file capture the user commands, such

as addPKI or addDH, and then configure the communications accordingly. Once

we know which encryption method the user wants (plain, PKI, ECC), we pass

the data, the type of encryption, and the required key or client secret. The client

file will pass the heavy lifting over to the helper file:

# 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;

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

Saved successfully!

Ooh no, something went wrong!