我对编码非常陌生,我正在编写的 Vigenere Cipher 加密的方式不是很正确。
它可以循环显示前几个字母,但不会继续。
我确定它与alphabet_position(key[char % len(key)])
区域有关。
前3个加密字母正确,但其余字母关闭。
import string
"""Creating a function that uses a string to encrypt a message"""
def alphabet_position(letter):
char = ''
if letter.isalpha():
if letter == letter.lower():
num = (ord(letter) - 97) % 26
return num
if letter == letter.upper():
num = (ord(letter) - 65) % 26
return num
elif letter != letter.isalpha():
char += letter
return char
def rotate_character(char,rot):
character = ''
if char.isalpha() == False:
char += character
return char
else:
rot = alphabet_position(char) + rot
if rot >= 26:
rot -= 26
if char == char.upper():
rot += 65
if char == char.lower():
rot += 97
return chr(rot)
def encrypt_key(text,key):
char = 0
mess = ''
for letter in range (len(text)):
letter = text[letter]
pos = alphabet_position(key[char%len(key)])
mess += rotate_character(letter,pos)
char += 1
return mess
def main():
print(encrypt_key("The crow flies at midnight!","boom"))
if __name__ == "__main__":
main()