我在试图弄清楚如何让我的解密功能工作时遇到了很多麻烦。它适用于味精可被 3 整除的理想情况,但在那之后我完全迷失了。我对我必须做什么有一个模糊的想法,因为我能够让它与两个导轨一起工作,但是三个导轨有更多的可能性。我很迷茫:(所有这些打印语句也只是为了帮助我弄清楚发生了什么。
import sys
def main():
plaintext="abcdefgh"
print(threeRailEncrypt(plaintext))
print(threeRailDecrypt(threeRailEncrypt(plaintext)))
def threeRailEncrypt(plaintext):
ciphertext=""
rail1=""
rail2=""
rail3=""
for i in range(len(plaintext)):
if i%3 == 0:
rail1=rail1+plaintext[i]
elif i%3 == 1:
rail2=rail2+plaintext[i]
else:
rail3=rail3+plaintext[i]
ciphertext=rail1+rail2+rail3
return(ciphertext)
def threeRailDecrypt(msg):
if len(msg)%3==0:
third=len(msg)//3
print(third)
rail1=msg[:third]
rail2=msg[third:third*2]
rail3=msg[third*2:]
print(rail1,rail2,rail3)
dm=""
for i in range(third):
dm=dm+rail1[i]
dm=dm+rail2[i]
dm=dm+rail3[i]
else:
third=(len(msg)//3)+1
print(third)
rail1=msg[:third]
rail2=msg[third:third*2]
rail3=msg[third*2:]
print(rail1,rail2,rail3)
dm=""
for i in range(third):
dm=dm+rail1[i]
print(dm)
dm=dm+rail2[i]
print(dm)
dm=dm+rail3[i]
print(dm)
if len(rail2)>len(rail3):
dm=dm+rail2[-1]
return(dm)
main()