guess_key2(cipher_text, word1, word2 ,word3): letter_frequency = get_letter_frequency(cipher_text.lower()) excluded_letters = [letter for letter in letter_frequency.keys() if letter_frequency[letter] == 0] sorted_letters = sorted([letter for letter in letter_frequency.keys() if letter_frequency[letter] > 0], key=lambda x: letter_frequency[x], reverse=True) print("Excluded letters:", excluded_letters) print() #if len(excluded_letters)<5 most_common_letters_m = [sorted_letters[15:21] ,sorted_letters[10:15] ,sorted_letters[7:10], sorted_letters[1:7]] #反向 #f1 = ['e'] f2 = ['a', 'i', 'r', 't', 'o', 'n'] f3 = ['s', 'l', 'c'] f4 = ['u', 'p', 'm', 'd', 'h'] f5 = ['g', 'b', 'y', 'f', 'v', 'w'] f6 = ['k', 'x', 'z', 'q', 'j'] mf = [f5, f4, f3, f2] mp = [[j for j in range(len(mf[i]))] for i in range(len(mf))] flag = True while(flag): key = { sorted_letters[0]: 'e'} row_permutations = [itertools.permutations(row) for row in mp] matrix_permutations = itertools.product(*row_permutations) for permutation in matrix_permutations: for i in range(len(mp)): for j in range(len(mp[i])): key[most_common_letters_m[i][permutation[i][j]]] = mf[i][j] if len(key) < len (sorted_letters): for i in range (len(sorted_letters)-len(key)): key.update({sorted_letters[len(key)+i-1]:f6[i]}) decrypted_text = decrypt(cipher_text, key) if is_plaintext(decrypted_text, word1, word2 ,word3): flag=False return key时间复杂度是否过高?
时间: 2024-03-14 21:49:04 浏览: 78
key_DES.rar_blowfish cipher
这段代码的时间复杂度非常高,因为它使用了多重嵌套的循环和迭代器,这会导致执行时间随着输入数据的增加而指数级增加。具体来说,这段代码的时间复杂度是O(N^M),其中N是字母表中字母的数量,M是最常见的字母组合的数量。如果输入的密文很长,这个时间复杂度可能会变得非常大。因此,这段代码应该尽可能地避免重复计算,以及尝试减少嵌套循环的数量。
阅读全文