Advertisement
Python253

kryptos_c1_decoder

Mar 1st, 2024
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Filename: kryptos_c1_decoder.py
  3. # Author: Jeoi Reqi
  4. # Decoder for The Kryptos Ciphers C1
  5.  
  6. # Define the alphabet and key
  7. ALPHABET = "KRYPTOSABCDEFGHIJLMNQUVWXZ"
  8. KEY = "PALIMPSEST"
  9.  
  10.  
  11. # Function for the vigenere cryptography
  12. def vigenere_decoder(ciphertext, key, alphabet):
  13.     """
  14.    Decrypt a Vigenere cipher using the specified key and alphabet.
  15.  
  16.    Parameters:
  17.    ciphertext (str): The text to decrypt.
  18.    key (str): The key to use for decryption.
  19.    alphabet (str): The alphabet to use for decryption.
  20.  
  21.    Returns:
  22.    str: The decrypted plaintext.
  23.    """
  24.     # Convert the key and ciphertext to uppercase
  25.     key = key.upper()
  26.     ciphertext = ciphertext.upper()
  27.  
  28.     # Calculate the length of the key and ciphertext
  29.     key_length = len(key)
  30.     ciphertext_length = len(ciphertext)
  31.  
  32.     # Generate the repeated key
  33.     repeated_key = ""
  34.     for i in range(ciphertext_length):
  35.         repeated_key += key[i % key_length]
  36.  
  37.     # Decrypt the ciphertext using the repeated key
  38.     plaintext = ""
  39.     for i in range(ciphertext_length):
  40.         ciphertext_char = ciphertext[i]
  41.         key_char = repeated_key[i]
  42.         # Find the position of the key and ciphertext characters in the alphabet
  43.         key_pos = alphabet.find(key_char)
  44.         ciphertext_pos = alphabet.find(ciphertext_char)
  45.         # Subtract the key position from the ciphertext position to get the plaintext position
  46.         plaintext_pos = (ciphertext_pos - key_pos) % len(alphabet)
  47.         # Get the plaintext character from the alphabet
  48.         plaintext_char = alphabet[plaintext_pos]
  49.         plaintext += plaintext_char
  50.  
  51.     return plaintext
  52.  
  53. # Decrypt the ciphertext using the given key and alphabet
  54. ciphertext = "EMUFPHZLRFAXYUSDJKZLDKRNSHGNFIVJYQTQUXQBQVYUVLLTREVJYQTMKYRDMFD"
  55. key = "PALIMPSEST"
  56. alphabet = "KRYPTOSABCDEFGHIJLMNQUVWXZ"
  57. plaintext = vigenere_decoder(ciphertext, key, alphabet)
  58.  
  59. # Print the decrypted plaintext
  60. print(plaintext)
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement