def decrypt(cipher_text, key): mapping_dict = str.maketrans(key) return cipher_text.translate(mapping_dict)
时间: 2024-03-14 09:49:31 浏览: 146
这段代码是一个简单的解密函数,接受密文和密钥作为参数,使用 str.maketrans() 函数创建一个映射表,然后使用 translate() 方法将密文中的字符根据映射表进行解密。
具体来说,str.maketrans() 函数接受两个参数,第一个参数是需要被替换的字符,第二个参数是替换的字符,这两个参数必须具有相同的长度。函数会返回一个映射表,用于将第一个参数中的字符替换为第二个参数中的字符。
translate() 方法接受一个映射表作为参数,将字符串中的字符根据映射表进行替换。因此,decrypt() 函数会将密文中的字符根据密钥进行解密,并返回解密后的明文。
需要注意的是,这段代码只适用于简单的替换密码,对于复杂的加密算法可能并不适用。
相关问题
def 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 = sorted_letters[:6] # first 6 for i in range(len(most_common_letters)): for j in range(i+1, len(most_common_letters)): for k in range(j+1, len(most_common_letters)): key = {most_common_letters[i]: 'e', most_common_letters[j]: 't', most_common_letters[k]: 'a'} # key = {'n': 'e', 'o': 't', 's': 'a'} .... decrypted_text = decrypt(cipher_text, key) if is_plaintext(decrypted_text, words): return key def decrypt(cipher_text, key): mapping_dict = str.maketrans(key) return cipher_text.translate(mapping_dict) def is_plaintext(text, words): words_found = 0 for word in text.split(): if word.lower() in words: words_found += 1 return (words_found / len(text.split())) > 0.001 其中key并没有替换掉所有字母,修改
可以尝试将key字典中没有替换的字母的值设置为出现频率最高的字母,即将如下代码:
```
key = {most_common_letters[i]: 'e', most_common_letters[j]: 't', most_common_letters[k]: 'a'}
```
修改为:
```
key = {most_common_letters[i]: 'e', most_common_letters[j]: 't', most_common_letters[k]: 'a'}
for letter in sorted_letters:
if letter not in key:
key[letter] = sorted_letters[0]
```
这样就可以保证key字典中所有字母都被替换了,并且未被替换的字母的值为出现频率最高的字母。
def guess_key3(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() f1 = ['a', 'i', 'r'] f2 = ['t', 'o', 'n'] f3 = ['s', 'l', 'c'] f4 = ['u', 'p', 'm', 'd', 'h'] f5 = ['g', 'b', 'y', 'f'] f6 = ['v', 'w','k'] f7 = ['x', 'z', 'q', 'j'] mf = [f6, f5, f4, f3, f2, f1] key = {sorted_letters[0]: 'e'} most_common_letters_m = [sorted_letters[19:22],sorted_letters[15:19] ,sorted_letters[10:15] ,sorted_letters[7:10] ,sorted_letters[4:7], sorted_letters[1:4]] c1 = 0.7 for i1 in range(5): lk1=len(key) key1 =check3(cipher_text, word1, word2, word3, most_common_letters_m, sorted_letters, mf, f7, lk1, key, c1, i1) key.update(key1) del mf[-1] del most_common_letters_m[-1] c1+=0.2 return key def check3(cipher_text, word1, word2, word3, most_common_letters_m, sorted_letters, mf, f7, key, lk1, c1, i1): mp = [[j for j in range(len(mf[i]))] for i in range(len(mf))] 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]:f7[i]}) decrypted_text = decrypt(cipher_text, key) k1 = is_plaintext3(decrypted_text, word1, word2 ,word3) #k2 = k1 if k1 > k2 else k2 if( k1 > c1): key1 = dict(list(key())[lk1:len(mp[i])+lk1]) return key1 def is_plaintext3(text, word1, word2 ,word3): words_found = 0 for word in text.split(): if word.lower() in word1: words_found += 10 if word.lower() in word2: words_found += 3 if word.lower() in word3: words_found += 1 return words_found / len(text.split()) def decrypt(cipher_text, key): mapping_dict = str.maketrans(key) return cipher_text.translate(mapping_dict)这段代码的问题
这段代码中的 `check3` 函数在定义时参数列表中包含了六个参数,但在调用时只传递了九个参数,因此会导致参数数量不匹配的错误。具体来说,`check3` 函数在调用时缺少了 `lk1`、`key`、`c1`、`i1` 这四个参数。你需要检查一下代码逻辑,确认这四个参数的值应该从哪里传递进去,并在调用时将它们添加到参数列表中。
阅读全文