Chapter ? - Code Cracker
- import secret2 as cipher
- #import secret3 as cipher
- import cryptoLib
- import string, time
- def main():
- msg = cryptoLib.getMessage()
- crack(msg)
- def getLikelihood(message, key, words):
- testPlaintext = cipher.decrypt(message, key)
-
- realWord = 0
- notRealWord = 0
- for word in testPlaintext.split():
- if word in words:
- realWord += 1
- else:
- notRealWord += 1
-
- return (realWord / float(realWord + notRealWord)) * 100.0
- def getWordsFromDictionaryFile():
- # Load the words from the dictionary file.
- dictionaryFile = open('dictionary.txt')
- words = dictionaryFile.readlines()
- dictionaryFile.close()
-
- # Remove the newline character at the end of each word.
- for i in range(len(words)):
- words[i] = words[i].strip()
-
- return words
-
- def crack(message):
- maxLikelihood = 0.0
- maxTestKey = 1
-
- words = getWordsFromDictionaryFile()
-
- # Replace any non-letter and non-apostrophe characters with spaces, so
- # that we can match words in the dictionary. Also make all letters
- # lowercase.
- testMessage = ''
- for letter in message:
- if letter not in string.letters and letter != "'":
- testMessage += ' '
- else:
- testMessage += letter.lower()
-
- # Do a test with a few keys to determine how long it will take to
- # go through all of the keys.
- startTime = time.time()
- for i in range(3):
- getLikelihood(testMessage, 1, words)
- endTime = time.time()
- averageTime = (endTime - startTime) / 3.0
- totalTime = averageTime * cipher.MAX_KEY_SIZE
- probableTime = totalTime / 2
-
- print
- print 'Average time to attempt one key: %.1f seconds' % (averageTime)
- print 'Total time to attempt all keys: %.1f seconds' % (totalTime)
- print 'Most likely time to find the key: %.1f seconds' % (probableTime)
- print
-
-
- for testKey in range(1, cipher.MAX_KEY_SIZE + 1):
- print 'Testing key: %s...' % (testKey),
-
- likelihood = getLikelihood(testMessage, testKey, words)
-
- if likelihood > maxLikelihood:
- maxLikelihood = likelihood
- maxTestKey = testKey
-
- print '%.1f%%' % (likelihood)
-
- if likelihood > 95.0:
- break
-
- print 'Most probable key: %s (%.1f%%)' % (maxTestKey, maxLikelihood)
- print 'Decrypted text with this key:'
- print cipher.decrypt(message, maxTestKey)
- # TODO: This is an additional function to crack the vigenere cipher
- def getKeyFromKeyWord(keyWord):
- key = 0
- for i in range(len(keyWord)):
- #print keyWord[i],
- if keyWord[i] == keyWord[i].upper():
- key += (ord(keyWord[i]) - ord('A')+1) * (52 ** i)
- #print (ord(keyWord[i]) - ord('A')+1) * (52 ** i)
- else:
- key += (ord(keyWord[i]) - ord('a')+1 + 26) * (52 ** i)
- #print (ord(keyWord[i]) - ord('a')+1 + 26) * (52 ** i)
- return key
- def getKeyWordFromKey(key):
- pass
-
- import sys
- if __name__ == '__main__':
- main()
- def getMode():
- mode = ''
- while (mode not in 'encrypt e decrypt d'.split()):
- print 'Do you wish to encrypt or decrypt a message?'
- mode = raw_input()
- mode = mode.lower()
- if (mode not in 'encrypt e decrypt d'.split()):
- print 'Enter either "encrypt" or "e" or "decrypt" or "d".'
- return mode
- def getMessage():
- print 'Enter your message:'
- message = raw_input()
-
- return message
- def getKey(maxKeySize):
- key = 0
- while (not (key >= 1 and key <= maxKeySize)):
- print 'Enter the key number (1-%s)' % (maxKeySize)
- key = int(raw_input())
- return key
Things Covered In This Chapter: