def 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() most_common_letters_m = [sorted_letters[:1], sorted_letters[1:7], sorted_letters[7:10], sorted_letters[10:15], sorted_letters[15:20], sorted_letters[20:26]] # high 8 middle 8 low 10 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'] mf = [f1, f2, f3, f4, f5] mp = [[j for j in range(len(mf[i]))] for i in range(len(mf))] flag = True while(flag): key = {} 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] decrypted_text = decrypt(cipher_text, key) if is_plaintext(decrypted_text, word1, word2 ,word3): flag=False return key 这里每个key不包含全部的26组对应关系,请修改以将他们补齐
时间: 2024-01-30 20:04:26 浏览: 69
可以将代码修改为:
```
import itertools
import random
def 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()
most_common_letters_m = [sorted_letters[:1], sorted_letters[1:7], sorted_letters[7:10], sorted_letters[10:15], sorted_letters[15:20], sorted_letters[20:26]]
# high 8 middle 8 low 10
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']
mf = [f1, f2, f3, f4, f5]
mp = [[j for j in range(len(mf[i]))] for i in range(len(mf))]
flag = True
while flag:
key = {}
row_permutations = [itertools.permutations(row) if len(row) > 1 else row for row in mp]
matrix_permutations = itertools.product(*row_permutations)
for permutation in matrix_permutations:
for i in range(len(permutation)):
if len(permutation[i]) == 1:
key[most_common_letters_m[i][0]] = mf[i][permutation[i][0]]
else:
for j in range(len(permutation[i])):
key[most_common_letters_m[i][permutation[i][j]]] = mf[i][j]
# 补齐未配对的字母
remaining_letters = set('abcdefghijklmnopqrstuvwxyz') - set(key.keys())
while len(remaining_letters) > 0:
letter = random.sample(remaining_letters, 1)[0]
value = random.sample(set('abcdefghijklmnopqrstuvwxyz') - set(key.values()), 1)[0]
key[letter] = value
remaining_letters = set('abcdefghijklmnopqrstuvwxyz') - set(key.keys())
decrypted_text = decrypt(cipher_text, key)
if is_plaintext(decrypted_text, word1, word2, word3):
flag = False
break
return key
```
这样,我们在生成 key 的过程中,对于每个位置,如果对应的单词只包含一个字母,则直接将该字母和对应的字母配对,否则使用 permutations() 函数生成所有可能的排列,然后将排列中的字母按照顺序与 mf 中的字母配对。然后,我们使用随机字母补齐未配对的字母,直到 key 包含 26 对对应关系。最后,我们检查解密后的明文是否满足给定的单词列表,如果满足,则返回 key。
需要注意的是,这里的随机字母选择算法并不完美,如果 mf 中的字母和已配对的字母集合中的字母的交集不为空,可能会导致算法产生死循环。如果需要更安全和更合理的随机字母选择算法,可以参考 Python 标准库中的 secrets 模块。
阅读全文