目前在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 len in(keyword):loop? – ergonaut
我懷疑你想'拉鍊'... – jonrsharpe
@idjaw不是這個問題的重複,無論如何。像這樣的Vigenere密碼的重點是模塊算術,即使OP還沒有意識到它。 – BlivetWidget