07.07.2023 Views

Implementing-cryptography-using-python

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 ■ Mastering Cryptography Using Python 267

except:

return 0

# generate Diffie-Hellman certificates for client

def gen_client_DH():

clientDH = dh.DiffieHellman(2,17,1024)

privateKey = str(clientDH.privateKey).encode()

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

fd.write(privateKey)

fd.close()

publicKey = str(clientDH.publicKey).encode()

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

fd.write(publicKey)

fd.close()

clientDHSet = clientDH

return clientDH

# generate Diffie-Hellman certificates for server

def gen_server_DH():

svrDH = dh.DiffieHellman(2,17,1024)

privateKey = str(svrDH.privateKey).encode()

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

fd.write(privateKey)

fd.close()

publicKey = str(svrDH.publicKey).encode()

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

fd.write(publicKey)

fd.close()

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

serverDHSet = svrDH

return svrDH

# encrypt using Diffie-Hellman - ECC

def encrypt_dh(plaintext, dhSecret):

# encrypt using the shared secret from Client (Private) and Server

(Public)

ciphertext = encrypt_AES_GCM(plaintext,dhSecret)

ciphertext = ciphertext.encode()

#reverse = decrypt_AES_GCM(ciphertext, dhSecret)

return ciphertext

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

Saved successfully!

Ooh no, something went wrong!