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.

134 Chapter 4 ■ Cryptographic Math and Frequency Analysis

def getTextOnly(text):

# Strip out all non-alphabet characters

# There are a number of ways to do this but this is the simplest to

understand

modifiedText = str(text.strip())

modifiedText = modifiedText.replace(" ", "")

modifiedText = " ".join(modifiedText.split())

modifiedText = modifiedText.lower()

return modifiedText

def getEncryptedData():

encryptedFilePath = "https://raw.githubusercontent.com/noidentity29/

AppliedCryptoPython/master/encryptedmoby.txt"

response = urllib.request.urlopen(encryptedFilePath, context=ssl._

create_unverified_context())

readText = response.read()

readText = readText.decode('utf-8')

textOnly = getTextOnly(readText)

return textOnly

def getFitnessScore(message, longerwords):

score = 0.0

for word in longerwords:

wordWeight = message.count(word)

if wordWeight>0:

score += wordWeight * 50 * len(word)

return score

def getDictionary():

commonWordsPath = "https://raw.githubusercontent.com/noidentity29/

AppliedCryptoPython/master/common_en_words.txt"

ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# create URL request

response = urllib.request.urlopen(commonWordsPath, context=ssl._

create_unverified_context())

readText = response.read()

# the file is in a binary format, decode

fileOfWords = readText.decode('utf-8')

# create an array for each word

words = fileOfWords.split()

# filter out the shorter words

longerwords = list(filter(lambda x: len(x)> 2, words))

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

Saved successfully!

Ooh no, something went wrong!