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.

248 Chapter 9 ■ Mastering Cryptography Using Python

■■

Encrypt the message using ECC

■■

Implement the Elliptic Curve Diffie-Hellman exchange

Constructing a Plaintext Communications Application

Our first task is to revisit the construction of a threaded server with unencrypted

traffic, which was introduced in Chapter 7. In addition to building a server and

client Python file, we will create a helper file here that will support the coding

additions we will implement as we are building our Python solution.

The architecture for the application includes a server file (crypto _ server _ a.

py), a client file (crypto _ client _ a.py), and a helper file that will be used by

both the client and server since they share many of the same components. One

side encrypts, and the other one decrypts. For the sake of keeping this example

simple, we will only do one-way communication. The same techniques used in

this chapter can be used between any applications that transmit data over UDP.

Creating a Server

Our first step is to create an application that will use UDP sockets to accept

packets from other programs. For our example we can use the localhost so that

you can use the client application to send plaintext messages to the server. The

server will then display the text as received.

Here’s the Python code:

# Message Receiver - crypto_chat_server.py

import hashlib, random, os, time

from binascii import hexlify

from socket import *

import Chapter9.ch9_crypto_chat as ct

def get_dh_sharedsecret():

return

def get_dh_sharedkey():

return

def decrypt(ciphertext, usePKI, useDH, serverSecret):

#msg = ct.decrypt(ciphertext, usePKI, useDH, serverSecret)

try:

msg = ct.decrypt(ciphertext, usePKI, useDH, serverSecret)

except:

msg = ciphertext

return msg

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

Saved successfully!

Ooh no, something went wrong!