我有一个维格纳密码的编码/解码编程问题。我只能使用列表、字典和循环。 编辑:我添加了我的解密。GetCharList()只获取一个包含字母表的列表。我不知道是什么错,它使decrypt的输出不是原始消息。在
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)
另一种方法是使用}来消除处理字母的一些复杂性,而不是自己去破解字母表。至少考虑使用
ord
和{itertools.cycle
和itertools.izip
来构造加密/解密对的列表。我可以这样解决:我不知道维格纳应该怎么工作。但是我很确定
num
为零。在相关问题 更多 >
编程相关推荐