【发布时间】:2015-11-04 14:46:12
【问题描述】:
目前正在用 Python 进行 Vigenere 密码,我和我班上的许多人都卡在一个方面。
将关键字转换为序数后,我们需要将这些数字添加到消息中以对其进行加密。这是我目前的代码。
Input = input('Enter your message: ')
key = input('Enter the one word key: ')
times = len(Input)//len(key)+1
encryptedKey = (times*key)[:len(Input)]
output = []
for character in Input:
number = ord(character) - 96
output.append(number)
outputKey = []
for character in encryptedKey:
numberKey = ord(character) - 96
outputKey.append(numberKey)
print(encryptedKey)
print(outputKey)
print(Input)
print(output)
因此,如果输入是'hello',而键是'bye',则关键字将变为'byeby'[2,25,5,2,25],而'hello' 将变为[8,5,12,12,15]。我想不出一种方法来添加第一个 2 和 8,25 和 5,等等。
我试过print(sum(output + outputKey)),但当然只是将所有数字相加,这意味着答案是111。
我还需要将它们转回字母,以便它以加密消息结束。
谢谢!
【问题讨论】:
-
为什么不 a: for i in len(keyword): 循环?
-
我怀疑你想要
zip... -
@idjaw 无论如何都不是那个问题的副本。像这样的 Vigenere 密码的重点是模运算,即使 OP 还没有意识到这一点。
标签: python encryption keyword vigenere