[python] Шифр Виженера
Viewer
- def encrypt_vigenere(plaintext: str, keyword: str) -> str:
- """
- Encrypts plaintext using a Vigenere cipher.
- >>> encrypt_vigenere("PYTHON", "A")
- 'PYTHON'
- >>> encrypt_vigenere("python", "a")
- 'python'
- >>> encrypt_vigenere("ATTACKATDAWN", "LEMON")
- 'LXFOPVEFRNHR'
- """
- ciphertext = ""
- for index, element in enumerate(plaintext):
- shift_alpha = keyword[index % len(keyword)]
- shift = ord((shift_alpha).lower()) - ord("a")
- if element.isalpha() and shift != 0:
- char_index = ord(element)
- if ord("a") <= char_index <= ord("z"):
- char_index = char_index + shift
- if char_index > ord("z"):
- char_index = char_index - 26
- cipher_char = chr(char_index)
- ciphertext += cipher_char
- elif ord("A") <= char_index <= ord("Z"):
- char_index = char_index + shift
- if char_index > ord("Z"):
- char_index = char_index - 26
- cipher_char = chr(char_index)
- ciphertext += cipher_char
- else:
- ciphertext += element
- return ciphertext
Editor
You can edit this paste and save as new: