Advertisement
Python253

kryptos_c1_c2_decoder

Mar 1st, 2024
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.06 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Filename: kryptos_c1_c2_decoder.py
  3. # Author: Jeoi Reqi
  4. # Decoder for The Kryptos Ciphers C1 & C2
  5.  
  6.  
  7. # Function for the vigenere cryptography
  8. def vigenere_decoder(ciphertext, key, alphabet):
  9.     """
  10.    Decrypt a Vigenere cipher using the specified key and alphabet.
  11.  
  12.    Parameters:
  13.    ciphertext (str): The text to decrypt.
  14.    key (str): The key to use for decryption.
  15.    alphabet (str): The alphabet to use for decryption.
  16.  
  17.    Returns:
  18.    str: The decrypted plaintext.
  19.    """
  20.     plaintext = ''
  21.     key_index = 0
  22.     key_length = len(key)
  23.     ciphertext_length = len(ciphertext)
  24.  
  25.     for i in range(ciphertext_length):
  26.         if ciphertext[i] not in alphabet:
  27.             plaintext += ciphertext[i]
  28.             continue
  29.  
  30.         # Calculate the shift
  31.         shift = alphabet.index(key[key_index])
  32.         key_index = (key_index + 1) % key_length
  33.  
  34.         # Decrypt the character
  35.         ciphertext_char = ciphertext[i]
  36.         ciphertext_pos = alphabet.find(ciphertext_char)
  37.         plaintext_pos = (ciphertext_pos - shift) % len(alphabet)
  38.         plaintext += alphabet[plaintext_pos]
  39.  
  40.     return plaintext
  41.  
  42. # Test the combined function with the given ciphertexts and keys
  43. ciphertext1 = "EMUFPHZLRFAXYUSDJKZLDKRNSHGNFIVJYQTQUXQBQVYUVLLTREVJYQTMKYRDMFD"
  44. key1 = "PALIMPSEST"
  45. alphabet1 = "KRYPTOSABCDEFGHIJLMNQUVWXZ"
  46. plaintext1 = vigenere_decoder(ciphertext1, key1, alphabet1)
  47. print("Cipher 1:")
  48. print(plaintext1)
  49. print("\n\n")
  50.  
  51. ciphertext2 = "VFPJUDEEHZWETZYVGWHKKQETGFQJNCEGGWHKKDQMCPFQZDQMMIAGPFXHQRLGTIMVMZJANQLVKQEDAGDVFRPJUNGEUNAQZGZLECGYUXUEENJTBJLBQCRTBJDFHRRYIZETKZEMVDUFKSJHKFWHKUWQLSZFTIHHDDDUVH?DWKBFUFPWNTDFIYCUQZEREEVLDKFEZMOQQJLTTUGSYQPFEUNLAVIDXFLGGTEZ?FKZBSFDQVGOGIPUFXHHDRKFFHQNTGPUAECNUVPDJMQCLQUMUNEDFQELZZVRRGKFFVOEEXBDMVPNFQXEZLGREDNQFMPNZGLFLPMRJQYALMGNUVPDXVKPDQUMEBEDMHDAFMJGZNUPLGEWJLLAETG"
  52. key2 = "ABSCISSA"
  53. alphabet2 = "KRYPTOSABCDEFGHIJLMNQUVWXZ"
  54. plaintext2 = vigenere_decoder(ciphertext2, key2, alphabet2)
  55. print("Cipher 2:")
  56.  
  57. # Print the decrypted plaintext
  58. print(plaintext2)
  59. print("\n\n")
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement