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 4 ■ Cryptographic Math and Frequency Analysis 137

defaultScore = getFitnessScore(decrypted, dictionary)

for a in range(1,answerLen):

testKey.append(keyPos[a])

decrypted = decryptIndex(testKey,encrypted)

testScore = getFitnessScore(decrypted, dictionary)

if testScore > defaultScore:

answerIndex = a

defaultKey = testKey

keys.append(keyPos[answerIndex])

fullKey = ""

for i in range(len(keys)):

fullKey = fullKey + chr(keys[i] + 65)

fullKey = ""

for i in range(len(keys)):

fullKey = fullKey + chr(keys[i] + 65)

return (fullKey)

myDictionary = getDictionary()

cipherText = getEncryptedData()

freqScore = getLetterFreqs(cipherText)

fitScore = getFitnessScore(cipherText, myDictionary)

keyLength = getKeyLength(cipherText)

decryptKey = getKey(cipherText, keyLength, myDictionary)

print ()

print ("The frequency score for this file is: ", freqScore)

print ()

print ("The fitness score for this file is: ", fitScore)

print()

print ("The key length for this file is: ", keyLength)

print ()

print ("The decryption key for this file is: ", decryptKey)

print ()

Figure 4.11 shows the output of an encrypted Moby Dick file and uses frequency

analysis to find the frequency, fitness score, and key length to decrypt the file.

Figure 4.11: Vigenere_crypto.py

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

Saved successfully!

Ooh no, something went wrong!