guess_key1(cipher_text, words): letter_frequency = get_letter_frequency(cipher_text.lower()) sorted_letters = sorted(letter_frequency.keys(), key=lambda x: letter_frequency[x], reverse=True) # paixu most_common_letters_m = [sorted_letters[:8], sorted_letters[8:16], sorted_letters[-10:]] # high 8 middle 8 low 10 f1 = ['e'] # >10% f2 = ['a', 'i', 'r', 't', 'o', 'n'] f3 = ['s', 'l', 'c'] f4 = ['u', 'p', 'm', 'd', 'h'] f5 = ['g', 'b', 'y', 'f', 'v'] f6 = ['w', 'k', 'x', 'z', 'q', 'j'] # <1% mf = [f1, f2, f3, f4, f5, f6] mp = [[iter(range(len(mf[i]))) for j in range(len(mf[i]))] for i in range(len(mf))] for i in range(len(mf)): for j in range(len(mf[i])): mp[i].append(None) flag = True while(flag): key = {} for i in range(len(mf)): mp[i] = itertools.permutations(range(len(mf[i]))) flag=False for i in range(6): for j in range(len(mf[i])): index = next(mp[i][j]) key.update({most_common_letters_m[i][index]: mf[i][j]}) decrypted_text = decrypt(cipher_text, key) if is_plaintext(decrypted_text, words): flag=False return key 我的想法是,每次取mp的一种排列,得到一个key,检验不通过再取mp的下一种排列,如何修改?
时间: 2024-03-12 08:45:09 浏览: 76
key_DES.rar_blowfish cipher
你可以在`while`循环内部使用一个`for`循环来遍历`mp`中的所有排列,如果某个排列解密后能通过检验,则直接返回`key`,否则继续遍历下一个排列。修改后的代码如下:
```
guess_key1(cipher_text, words):
letter_frequency = get_letter_frequency(cipher_text.lower())
sorted_letters = sorted(letter_frequency.keys(), key=lambda x: letter_frequency[x], reverse=True)
most_common_letters_m = [sorted_letters[:8], sorted_letters[8:16], sorted_letters[-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']
f6 = ['w', 'k', 'x', 'z', 'q', 'j']
mf = [f1, f2, f3, f4, f5, f6]
mp = [[iter(range(len(mf[i]))) for j in range(len(mf[i]))] for i in range(len(mf))]
for i in range(len(mf)):
for j in range(len(mf[i])):
mp[i].append(None)
flag = True
while(flag):
for i in range(len(mf)):
for j in range(len(mf[i])):
index = next(mp[i][j])
key = {}
for k in range(len(most_common_letters_m)):
key[most_common_letters_m[k][index]] = mf[k][j]
decrypted_text = decrypt(cipher_text, key)
if is_plaintext(decrypted_text, words):
return key
flag = False
```
阅读全文