在Python中解密替换密码会为某些字母生成正确的替换,但不会为其他字母生成正确的替换

在Python中解密替换密码会为某些字母生成正确的替换,但不会为其他字母生成正确的替换,python,cryptography,Python,Cryptography,为了帮助我学习Python,我一直在编写一个小脚本,在Python挑战中解密一个简单的替换密码。如果你想避免潜在的破坏者,请停在这里 密文向后旋转两步,密文输入如下: thread = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.ky

为了帮助我学习Python,我一直在编写一个小脚本,在Python挑战中解密一个简单的替换密码。如果你想避免潜在的破坏者,请停在这里

密文向后旋转两步,密文输入如下:

thread = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
我使用的脚本如下,带有注释

cipher = "abcdefghijklmnopqrstuvwxyz"

for letter in thread:
  if letter in cipher and cipher.index(letter) <= 23:    # if the character is in the alphabet and is between a-x...
    thread = thread.replace(letter, cipher[cipher.index(letter) + 2], 1)    # replace that letter with the one two letters after it, and save

  elif letter in cipher and cipher.index(letter) > 23:   # but if the character is y or z...
    thread = thread.replace(letter, cipher[cipher.index(letter) - 24], 1)    # replace those with a and b, respectively

print(thread)
让我印象深刻的是,第一个字母(g)被错误地转换了8步。但是,第二个字母(f)经过2步正确转置。其他不正确的字母似乎被不同数量的字母转置。我回去把代码拆开,检查各个部分是否工作:

print(cipher[cipher.index("g") + 2]) # this bit is functional and converts letters a-x by a shift of 2. more specifically, it does CORRECTLY convert g to i.

print(cipher[cipher.index("y") - 24]) # this is functional too and converts y and z

我不确定这个脚本有什么问题。任何帮助都将不胜感激。

好的,根据您的代码,我就是这样解决的:

thread = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
cipher = "abcdefghijklmnopqrstuvwxyz"
thread_decrypted = ''  ## I used an empty string to append the rotated letters ##

for letter in thread:
    if letter in cipher : 
        if cipher.index(letter) <= 23 : 
            thread_decrypted += cipher[ cipher.index(letter) + 2 ]  
        else :  
            thread_decrypted += cipher[ cipher.index(letter) -24 ]  
    else : 
        thread_decrypted += letter

print(thread_decrypted)  
这基本上是一行中的for循环(列表理解)

代码的问题在于使用了
replace()

每次迭代时,原始字符串都会发生变化
(“a”变成“c”,然后变成“e”,等等),
这就是为什么你会犯错误


我希望这有帮助,祝你学习顺利

我设法解密了你的
线程
,不过我使用了一种稍微不同的方法。如果你真的卡住了,我可以贴出来。提示凯撒密码的公式是字母表[(字母+旋转)%26]谢谢。我不知道解决这类密码的正式公式。这很有帮助。不过,知道我的代码有什么问题还是很好的。谢谢你的回复。我的印象是replace()将系统地遍历线程的每个字母,因为它位于for循环中。显然情况并非如此。所以我的问题是:for循环是否不系统地遍历列表或线程?它是随机的吗?您的逻辑似乎是正确的(检查
线程的每个字母并替换第一个出现的字母),但您忽略了一个细节。您可以系统地循环(索引)字母,但这不会影响
replace
。假设您到达第10个字母,如果
replace
在第4个字母上找到匹配项,它将替换第4个字母,而不是第10个字母。也许你可以把
thread
变成一个列表,修改它的元素,然后
join
把它变成一个字符串,但我认为你不能使用
replace
简言之,问题就在这里:
thread=thread.replace
,想想看。
thread = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
cipher = "abcdefghijklmnopqrstuvwxyz"
thread_decrypted = ''  ## I used an empty string to append the rotated letters ##

for letter in thread:
    if letter in cipher : 
        if cipher.index(letter) <= 23 : 
            thread_decrypted += cipher[ cipher.index(letter) + 2 ]  
        else :  
            thread_decrypted += cipher[ cipher.index(letter) -24 ]  
    else : 
        thread_decrypted += letter

print(thread_decrypted)  
thread = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
cipher = "abcdefghijklmnopqrstuvwxyz"  
thread_decrypted = ''.join( cipher[ ( cipher.index(l) + 2 ) % 26 ] if l in cipher else l for l in thread )
print(thread_decrypted)