我遇到了一个解密问题。我有一个非常基本的密码,它只是字母表,偏移 1 个字母,如下所示:
A B
B C
C D
D E
to z to A
右栏是给我的字母,我需要把它们变成左边的字母。
我正在从文件中读取它,并将每一列保存到这样的列表中
#!/usr/bin/python
key = "key.txt"
encrypted = "encrypted.txt"
decrypted = "decrypted.txt"
encryptedList = []
decryptedList = []
with open(key, "r") as file:
for line in file:
currentLine = line.split()
currentDecrypted = currentLine[0]
currentEncrypted = currentLine[1]
decryptedList.append(currentEncrypted)
encryptedList.append(currentDecrypted)
file.close()
counter = 0
with open(encrypted, "r") as file:
for line in file:
currentLine = line
for letter in currentLine:
currentLetter = letter
for item in encryptedList:
if(item == currentLetter):
####here's where the problem starts####
####I've tried just printing counter, and I get mostly go
printencryptedList[counter-1]
counter = 0
break
counter += 1
我要解密的是一个看起来像这样的文本文件
FMMP
NZ OBNF JT KSMBB
KPIO TVDLT BQQ
GWWWWWWBMT PG DJJJJJH
我得到了 FMMP 的正确计数,(5、12、12、15)和计数器 -1,但后来我得到 39、25、40 等等。任何帮助将不胜感激,如果您需要更多信息,请告诉我。
我也欢迎有关更好/更简单方法的想法,但我也想要一个类似的解决方案,所以我可以弄清楚这里发生了什么。谢谢
好的,谢谢所有的答案和信息。我正在发布我最终所做的并且它有效。我确信它不像它应该的那样像pythonic,但我实现了人们提到的一些东西。谢谢(你的)信息。
import string
key = "key.txt"
encrypted = ""encrypted.txt"
decrypted = "decrypted.txt
encryptedString = ""
decryptedString = ""
keyDict = {}
with open(key, "r") as file:
for line in file:
currentLine = line.split()
currentDecrypted = currentLine[0]
currentEncrypted = currentLine[1]
keyDict[currentDecrypted] = currentEncrypted
with open(encrypted, "r") as file:
for line in file:
currentLine = line
for letter in currentLine:
currentLetter = letter
encryptedString += letter
for key in keyDict:
if(keyDict[key] == letter):
decryptedString += key
break
elif(letter == " "):
decryptedString += " "
break
elif(letter == "\n"):
decryptedString += "\n"
break
with open(decrypted, "a") as file:
file.write(decryptedString)