我得到的錯誤字符串索引超出範圍內的加密函數我不知道如何得到腐爛重複文本。該代碼僅在兩個輸入具有相同長度時才起作用。如果可以,我想讓alphabet_position和rotate_character功能保持一致。我的vigenere密碼加密函數有什麼問題?
alpha_lower_list = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
alpha_upper_list = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
def alphabet_position(letter):
if letter in alpha_upper_list:
return alpha_upper_list.index(letter)
else:
return alpha_lower_list.index(letter)
def rotate_character(char, rot):
rotated_letter = ''
if char.isalpha():
rotate = alphabet_position(char) + rot
if rotate < 26:
if char in alpha_upper_list:
rotated_letter = alpha_upper_list[rotate]
return(rotated_letter)
else:
rotated_letter = alpha_lower_list[rotate]
return(rotated_letter)
else:
if char in alpha_upper_list:
rotated_letter = alpha_upper_list[rotate % 26]
return(rotated_letter)
else:
rotated_letter = alpha_lower_list[rotate % 26]
return(rotated_letter)
else:
return(char)
def encrypt(text, rot):
lis = []
for i in range(len(text)):
lis.append(rotate_character(text[i], alphabet_position(rot[i])))
return (''.join(lis))
def main():
user_text = input("Type a message: ")
rotate_by = input("Rotate by: ")
print(encrypt(user_text, rotate_by))
if __name__ == '__main__':
main()
謝謝你,我只需要弄清楚如何返回此: 'Feqwgba <3 fnvta effo ox0xiv syfvbxf' insted的 'Feqwgba <3 asgdm zuvc zf0xka plppiey' 的 – shiquann
能您請提供明文和密鑰,在該密碼下加密是有效的輸出?這將緩解跟蹤bug ... –
這是輸入vigenere.encrypt('航行<3通過br0ken港口','NeilYoung') – shiquann