我正在尝试在 python 中创建 vigenere 密码,但似乎存在问题。这是我的加密代码:
def encryption():
plaintext=input("Please enter the message you wish to encode.")
#This allows the user to enter the message they wish to encrypt.
keyword=input("Please enter your keyword, preferably shorter than the plaintext.")
#This allows the user to enter a keyword.
encoded=""
#This creates a string for the user to input the encrypted message to.
while len(keyword)<len(plaintext):
#This begins a while loop based on the fact that the length of the keyword is shorter than the length of the plaintext.
keyword+=keyword
#This repeats the keyword.
if len(keyword)>len(plaintext):
#This sees if the string length of the keyword is now longer than the string length of the plaintext.
newkey=keyword[:len(plaintext)]
#This cuts the string length of the keyword
for c in range(len(plaintext)):
char=ord(plaintext[c])
temp=ord(keyword[c])
newchar=char+temp
if newchar>ord("Z"):
newchar-=26
newnewchar=chr(newchar)
encoded+=newnewchar
print(encoded)
我似乎找不到它的问题,但是当我输入明文“hello”和关键字“hi”时,它会出现以下符号:¶´º»½。我认为 for 循环中的添加可能太过分了。