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.

126 Chapter 4 ■ Cryptographic Math and Frequency Analysis

Here's the example code to read in the words using the .split() method

to break them into a list of words. Using the following Python code, you will

download a file from the Internet called common_en_words.txt, which contains

10,000 common words in the English language. The output will show each of

these words on their own line:

import urllib.request, ssl

# URL to Common English Words

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

AppliedCryptoPython/master/common_en_words.txt"

# 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()

# print word list

for word in words:

print (word)

# filter out the shorter words

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

# print out longer words

for word in longerwords:

print (word)

Determining the Frequency

The trick to decrypting historical ciphers using cryptanalysis relies on being

able to determine the frequency of letters regardless of their encryption state.

In the next example, you will see how to examine the frequency of letters

used in text. The online provided file, ch4_encrypted.txt, is encrypted with

a computer-generated key that represents the letters of the alphabet. From our

previous lessons, you learned that the most common letters include E, T, A, O,

I, N. Armed with this, you can start examining the frequency of letters in text

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

Saved successfully!

Ooh no, something went wrong!