问题是:
您的程序需要解码一个名为“encrypted.txt”的加密文本文件。编写它的人使用了“key.txt”中指定的密码。此密钥文件类似于以下内容:
A B B C C D D E E F F G G H H I I J J K K L L M M N N O O P P Q Q R R S S T T U U V V W W X X Y Y Z Z A
左栏代表明文字母,右栏代表对应的密文。
您的程序应该使用“key.txt”解码“encrypted.txt”文件并将明文写入“decrypted.txt”。
我有:
decrypt = ""
keyfile = open("key.txt", "r")
cipher = {}
for keyline in keyfile:
cipher[keyline.split()[0]] = keyline.split()[1]
keyfile.close()
encrypt = []
encryptedfile = open("encrypted.txt", "r")
readlines = encryptedfile.readlines()
str = ""
for line in readlines:
str = line
letter = list(str)
for i in range(len(letter)):
if letter[i]==",":
decrypt += ","
elif letter[i] == ".":
decrypt+= "."
elif letter[i] == "!":
decrypt += "!"
elif letter[i] == " ":
decrypt += " "
else:
found = False
count = 0
while (found == False):
if (letter[i] == keyline.split()[0][count]):
decrypt += keyline.split()[1][count]
found = True
count += 1
print decrypt
encryptedfile.close()
decryptedfile = open("decrypted.txt", "w")
decryptedfile.write(decrypt)
decryptedfile.close()
这是 Python 语言。输出确实生成了一个名为decrypted.txt 的文件,但文件中唯一的内容是A,这对我来说没有意义。对于问题,它应该输出更多,对吗?