[python] Шифр Виженера

Viewer

copydownloadembedprintName: Шифр Виженера
  1. def encrypt_vigenere(plaintext: str, keywordstr) -> str:
  2.     """
  3.     Encrypts plaintext using a Vigenere cipher.
  4.  
  5.     >>> encrypt_vigenere("PYTHON", "A")
  6.     'PYTHON'
  7.     >>> encrypt_vigenere("python", "a")
  8.     'python'
  9.     >>> encrypt_vigenere("ATTACKATDAWN", "LEMON")
  10.     'LXFOPVEFRNHR'
  11.     """
  12.     ciphertext = ""
  13.     for index, element in enumerate(plaintext):
  14.         shift_alpha = keyword[index % len(keyword)]
  15.         shift = ord((shift_alpha).lower()) - ord("a")
  16.         if element.isalpha() and shift != 0:
  17.             char_index = ord(element)
  18.             if ord("a") <= char_index <= ord("z"):
  19.                 char_index = char_index + shift
  20.                 if char_index > ord("z"):
  21.                     char_index = char_index - 26
  22.                 cipher_char = chr(char_index)
  23.                 ciphertext += cipher_char
  24.             elif ord("A") <= char_index <= ord("Z"):
  25.                 char_index = char_index + shift
  26.                 if char_index > ord("Z"):
  27.                     char_index = char_index - 26
  28.                 cipher_char = chr(char_index)
  29.                 ciphertext += cipher_char
  30.         else:
  31.             ciphertext += element
  32.     return ciphertext

Editor

You can edit this paste and save as new:


File Description
  • Шифр Виженера
  • Paste Code
  • 29 Mar-2024
  • 1.19 Kb
You can Share it: