def find_best_key(key, cipherText): ''' finds the best scoring key by modifying the key one letter at a time and calculating the log score :param key: :param cipherText: :return: ''' bestKey = key bestScore = float(calculate_log_score(Vignere.decrypt(cipherText, key), 4)) score = -1 for index in range(0, len(key)): for letter in range(0, 26): key = iterate_key(key, index, letter) score = float(calculate_log_score(Vignere.decrypt(cipherText, key), 4)) if score > bestScore: bestScore = score bestKey = key print ("%s\t%s\t%s" % (key, score, Vignere.decrypt(cipherText,key))) key = bestKey return bestKey
def find_best_key(key, cipherText): ''' finds the best scoring key by modifying the key one letter at a time and calculating the log score :param key: :param cipherText: :return: ''' bestKey = key bestScore = float(calculate_log_score(Vignere.decrypt(cipherText, key), 4)) score = -1 for index in range(0, len(key)): for letter in range(0, 26): key = iterate_key(key, index, letter) score = float( calculate_log_score(Vignere.decrypt(cipherText, key), 4)) if score > bestScore: bestScore = score bestKey = key print("%s\t%s\t%s" % (key, score, Vignere.decrypt(cipherText, key))) key = bestKey return bestKey
__author__ = 'jon-bassi' import sys import IndexOfCoincidence import Vignere import ChiSquaredStatistic if len(sys.argv) != 2: print 'error executing HW1_1.py\nusage: python HW1_1.py [text]' sys.exit(0) # find ic of different periods, target is > 0.06 # cipher text is hogvkiougtbtwlittwkopovsvebsvkiougtbtwlittwkjbd # vptnvffuntshtarptymjwzirappljmhhqvsubwlzzygvtyitarptyiougxiuydtgzhhvvmumshwkzgstfmekvmpkswdgbilvjljmglmjfqwioiivknulvvfemioiemojtywdsajtwmtcgluysdsumfbieugmvalvxkjduetukatymvkqzhvqvgvptytjwwldyeevquhlulwpkt text = sys.argv[1].lower() IndexOfCoincidence.list_all_ic(text) keyLength = int(raw_input('key length: ')) periodicList = [''] * keyLength for idx in range(len(text)): periodicList[idx % keyLength] += text[idx] key = '' for string in periodicList: key += ChiSquaredStatistic.list_all_cs(string) print ('key: %s\ndecrypted text: %s' % (key, Vignere.decrypt(text, key)))
score = float( calculate_log_score(Vignere.decrypt(cipherText, key), 4)) if score > bestScore: bestScore = score bestKey = key print("%s\t%s\t%s" % (key, score, Vignere.decrypt(cipherText, key))) key = bestKey return bestKey if __name__ == "__main__": if len(sys.argv) != 3: print 'error executing NGramScore.py\nusage: python NGramScore.py [text] [key size]' sys.exit(0) text = sys.argv[1] keySize = int(sys.argv[2]) key = '' # could make this a random key for i in range(0, keySize): key += 'a' while True: bestKey = find_best_key(key, text) print('%s %s' % (bestKey, Vignere.decrypt(text, bestKey))) input = raw_input('press enter to try again or type x to exit: ') if input == 'x': break key = bestKey
for index in range(0, len(key)): for letter in range(0, 26): key = iterate_key(key, index, letter) score = float(calculate_log_score(Vignere.decrypt(cipherText, key), 4)) if score > bestScore: bestScore = score bestKey = key print ("%s\t%s\t%s" % (key, score, Vignere.decrypt(cipherText,key))) key = bestKey return bestKey if __name__ == "__main__": if len(sys.argv) != 3: print 'error executing NGramScore.py\nusage: python NGramScore.py [text] [key size]' sys.exit(0) text = sys.argv[1] keySize = int(sys.argv[2]) key = '' # could make this a random key for i in range(0, keySize): key += 'a' while True: bestKey = find_best_key(key, text) print('%s %s' % (bestKey, Vignere.decrypt(text, bestKey))) input = raw_input('press enter to try again or type x to exit: ') if input == 'x': break key = bestKey
__author__ = 'jon-bassi' import sys import IndexOfCoincidence import Vignere import ChiSquaredStatistic if len(sys.argv) != 2: print 'error executing HW1_1.py\nusage: python HW1_1.py [text]' sys.exit(0) # find ic of different periods, target is > 0.06 # cipher text is hogvkiougtbtwlittwkopovsvebsvkiougtbtwlittwkjbd # vptnvffuntshtarptymjwzirappljmhhqvsubwlzzygvtyitarptyiougxiuydtgzhhvvmumshwkzgstfmekvmpkswdgbilvjljmglmjfqwioiivknulvvfemioiemojtywdsajtwmtcgluysdsumfbieugmvalvxkjduetukatymvkqzhvqvgvptytjwwldyeevquhlulwpkt text = sys.argv[1].lower() IndexOfCoincidence.list_all_ic(text) keyLength = int(raw_input('key length: ')) periodicList = [''] * keyLength for idx in range(len(text)): periodicList[idx % keyLength] += text[idx] key = '' for string in periodicList: key += ChiSquaredStatistic.list_all_cs(string) print('key: %s\ndecrypted text: %s' % (key, Vignere.decrypt(text, key)))