【发布时间】:2014-05-03 01:23:42
【问题描述】:
我在为 vigenere 密码进行编码/解码编程时遇到问题。我只应该使用列表、字典和循环。 编辑:我添加了我拥有的解密。 GetCharList() 只是获取一个包含字母表的列表。我不知道它使 decrpyt 的输出不是原始消息有什么问题。
def encryptVig(msg, keyword):
alphabet = getCharList() #Get char list is another function which creates a list containing a - z
key = keyword.upper()
keyIndex = 0
dicList = []
for symbol in msg:
num = alphabet.find(key[keyIndex])
if num != -1:
num += alphabet.find(key[keyIndex])
alphabet.find(key[keyIndex])
num%= len(alphabet)
if symbol.isupper():
dicList.append(alphabet[num])
elif symbol.islower():
dicList. append(alphabet[num].lower())
keyIndex += 1
if keyIndex == len(key):
keyIndex = 0
else:
dicList.append(symbol)
return " " .join(dicList)
def decryptVig(msg, keyword):
getCharList()
key = keyword.upper()
keyIndex = 0
dicList = []
for symbol in msg:
num = alphabet.find(key[keyIndex])
if num != -1:
num -= alphabet.find(key[keyIndex])
alphabet.find(key[keyIndex])
num%= len(alphabet)
if symbol.isupper():
dicList.append(alphabet[num])
elif symbol.islower():
dicList. append(alphabet[num].lower())
keyIndex -= 1
if keyIndex == len(key):
keyIndex = 0
else:
dicList.append(symbol)
return " " .join(dicList)
【问题讨论】:
-
您遇到了什么问题?
-
所以基本上我们无法运行你的东西,你有一些问题,但你没有告诉我们它是什么?!
-
当我编码时,它会产生一个输出,例如 Q O Q O O Q 当我使用解码函数对其进行解码时,它与 + 到 - 的功能基本相同,以反转密码,它不会解密消息。
-
如果问题出在
decrypt,那么您应该发布该函数。 -
发布了解密:\任何能引导我走向正确方向的帮助,谢谢!
标签: python list loops dictionary vigenere