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 259

fd = open("client_public_key.pem", "wb")

fd.write(public_key)

fd.close()

# decrypt using RSA

def decrypt_rsa(ciphertext):

key = RSA.importKey(open('client_private_key.pem').read())

cipher = PKCS1_OAEP.new(key)

plaintext = cipher.decrypt(ciphertext)

return plaintext

# encrypt using RSA

def encrypt_rsa(message):

key = RSA.importKey(open('client_public_key.pem').read())

cipher = PKCS1_OAEP.new(key)

ciphertext = cipher.encrypt(message)

plaintext = decrypt_rsa(ciphertext)

return ciphertext

# check client commands

def check_client_command(data):

if data == b'addPKI':

gen_rsa_certs()

return 11

elif data == b'removePKI':

usePKI = False

return 10

return 1

# check server commands

def check_server_command(data):

if data == b'addPKI':

return 11

if data == b'removePKI':

useDH = False

return 10

return 1

Execution

The execution of the PKI solution will appear to work just as the original version

did. Once you type addPKI, the client sends the message to the server unencrypted

and then modifies the data to state “PKI Encryption enabled!” This

should pop up in the command window for the server, as shown in Figure 9.5.

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

Saved successfully!

Ooh no, something went wrong!