问题描述
我正在处理单字母的加密文本,我试图根据每个字母的出现频率猜测原始文本。虽然逐步替换后,当我遇到下面的字母m是我的代码时遇到了错误
`cipher_text = "iyzzfwpmzcyawcixvwxvwciywpfavcwryvvm ywqafrwciywbfs fwhzymvywuywmpmaywcimcwciywrmvbmamgywimvwuyyswcfcmzzewgxvbfkyaygwsfwrfaywbfrrjsxbmcxfsvwvxsbywciyewmaywcwgms yafjvwqfawjvwuewciywpmewciywrfvcwhfhjzmawzyccyavwxswys zxviwmaywycmfxswviagzj"
frequency_letters = {}
for line in cipher_text:
for char in line:
if char not in frequency_letters:
frequency_letters[char]=1
else:
frequency_letters[char] += 1
attempt = cipher_text.replace("z","\033[31ml\033[0m")
attempt = attempt.replace("i","\033[31mh\033[0m")
attempt = attempt.replace("c","\033[31mt\033[0m")
attempt = attempt.replace("y","\033[31me\033[0m")
attempt = attempt.replace("w","\033[31m''\033[0m")
attempt = attempt.replace("f","\033[31mo\033[0m")
attempt = attempt.replace("p","\033[31mw\033[0m")
attempt = attempt.replace("a","\033[31mr\033[0m")
attempt = attempt.replace("x","\033[31mi\033[0m")
attempt = attempt.replace("v","\033[31ms\033[0m")
#below is the line that gives an error substituting m to a
#attempt = attempt.replace("m","\033[31ma\033[0m")
print(attempt)
`
When it comes to substituting m to a i get these`[31[31mah[0[31ma[31[31mae[0[31ma[31[31mal[0[31ma[31[31mal[0[31ma[31[31mao[0[31ma[31[31ma''[0[31ma[31[31maw[0[31maa[31[31mal[0[31ma[31[31mat[0[31ma[31[31mae[0[31ma[31[31mar[0[31ma[31[31ma''[0[31ma[31[31mat[0[31ma[31[31mah[0[31ma[31[31mai[0[31ma[31[31mas[0[31ma[31[31ma''[0[31ma[31[31mai[0[31ma[31[31mas[0[31ma[31[31ma''[0[31ma[31[31mat[0[31ma[31[31mah[0[31ma[31[31mae[0[31ma[31[31ma''[0[31ma[31[31maw[0[31ma[31[31mao[0[31ma[31[31mar[0[31ma[31[31mas[0[31ma[31[31mat[0[31ma[31[31ma''[0[31mar[31[31mae[0[31ma[31[31mas[0[31ma[31[31mas[0[31maa [31[31mae[0[31ma[31[31ma''[0[31maq[31[31mar[0[31ma[31[31mao[0[31mar[31[31ma''`
解决方法
我认为最好的解决方案是使用字典并遍历文本
text = "iyzzfwpmzcyawcixvwxvwciywpfavcwryvvm ywqafrwciywbfs fwhzymvywuywmpmaywcimcwciywrmvbmamgywimvwuyyswcfcmzzewgxvbfkyaygwsfwrfaywbfrrjsxbmcxfsvwvxsbywciyewmaywcwgms yafjvwqfawjvwuewciywpmewciywrfvcwhfhjzmawzyccyavwxswys zxviwmaywycmfxswviagzj"
d = {'z': 'l','i': 'h','c': 't','y': 'e','w': "''",'f': 'o','p': 'w','a': 'r','x': 'i','v': 's','m': 'a'}
text_new = ''
for i in range(len(text)):
try:
text_new += d[text[i]].upper()
except:
text_new += text[i]
print(text_new)
我也破译了您的文字:https://imgur.com/sgWX9K8
,至少使用函数为文本着色也许会更方便。像这样:
cipher_text = "iyzzfwpmzcyawcixvwxvwciywpfavcwryvvm ywqafrwciywbfs fwhzymvywuywmpmaywcimcwciywrmvbmamgywimvwuyyswcfcmzzewgxvbfkyaygwsfwrfaywbfrrjsxbmcxfsvwvxsbywciyewmaywcwgms yafjvwqfawjvwuewciywpmewciywrfvcwhfhjzmawzyccyavwxswys zxviwmaywycmfxswviagzj"
frequency_letters = {}
for line in cipher_text:
for char in line:
if char not in frequency_letters:
frequency_letters[char]=1
else:
frequency_letters[char] += 1
def c(character):
return "\x1b[31m" + character + "\x1b[0m"
attempt = cipher_text.replace("z",c("l"))
attempt = attempt.replace("i",c("h"))
attempt = attempt.replace("c",c("t"))
attempt = attempt.replace("y",c("e"))
attempt = attempt.replace("w",c("''"))
attempt = attempt.replace("f",c("o"))
attempt = attempt.replace("p",c("w"))
attempt = attempt.replace("a",c("r"))
attempt = attempt.replace("x",c("i"))
attempt = attempt.replace("v",c("s"))
attempt = attempt.replace("mm",c("a"))
print(attempt)
或者:
cipher_text = "iyzzfwpmzcyawcixvwxvwciywpfavcwryvvm ywqafrwciywbfs fwhzymvywuywmpmaywcimcwciywrmvbmamgywimvwuyyswcfcmzzewgxvbfkyaygwsfwrfaywbfrrjsxbmcxfsvwvxsbywciyewmaywcwgms yafjvwqfawjvwuewciywpmewciywrfvcwhfhjzmawzyccyavwxswys zxviwmaywycmfxswviagzj"
frequency_letters = {}
for line in cipher_text:
for char in line:
if char not in frequency_letters:
frequency_letters[char]=1
else:
frequency_letters[char] += 1
changes = [
("z","l"),("i","h"),("c","t"),("y","e"),("w","''"),("f","o"),("p","w"),("a","r"),("x","i"),("v","s"),("mm","a")
]
for what,to in changes:
cipher_text = cipher_text.replace(what,"\x1b[31m" + to + "\x1b[0m")
print(cipher_text)