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[:7], sorted_letters[8:15], sorted_letters[16:25]] # high 8 middle 8 low 10 f1 = ['e', 't', 'a', 'o', 'i', 'n', 'r', 's'] # >6% f2 = ['h', 'd', 'c', 'l', 'm', 'p', 'u', 'f'] # >2% f3 = ['g', 'w', 'y', 'b', 'v', 'k', 'j', 'x', 'q', 'z'] # <2% maf = [f1 ,f2 ,f3] flag = True while(flag): for i in range(3): for j in range(len(most_common_letters_m[i])): for k in range(len(most_common_letters_m[i])): key = {most_common_letters_m[j]: maf[i][0], most_common_letters_m[k]: maf[i][1]} decrypted_text = decrypt(cipher_text, key) if is_plaintext(decrypted_text, words): flag=False return key 这里中间类似j,k的循环我想进行多次,怎么修改
时间: 2024-01-05 16:03:38 浏览: 83
如果你想多次执行 j,k 的循环,可以将其封装成一个函数,然后在外层再嵌套一个循环,多次调用这个函数即可。例如:
```
def decrypt_with_key(cipher_text, key):
decrypted_text = decrypt(cipher_text, key)
if is_plaintext(decrypted_text, words):
return True, decrypted_text
else:
return False, ''
def try_decrypt(cipher_text, most_common_letters_m, maf):
for i in range(3):
for j in range(len(most_common_letters_m[i])):
for k in range(len(most_common_letters_m[i])):
key = {most_common_letters_m[j]: maf[i][0], most_common_letters_m[k]: maf[i][1]}
success, decrypted_text = decrypt_with_key(cipher_text, key)
if success:
return key, decrypted_text
return {}, ''
flag = True
while flag:
key, decrypted_text = try_decrypt(cipher_text, most_common_letters_m, maf)
if key:
flag = False
print(key, decrypted_text)
```
这样就可以多次尝试不同的解密密钥了。
阅读全文