Atalanttore

OneTimePad

Jun 7th, 2020
937
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import string
  2.  
  3. ALPHABET = string.ascii_uppercase
  4.  
  5.  
  6. def convert_to_alphabet_indices(word):
  7.     alphabet_indices = list()
  8.     for character in list(word):
  9.         character_index = ALPHABET.index(character)
  10.         alphabet_indices.append(character_index)
  11.     return alphabet_indices
  12.  
  13.  
  14. def convert_to_alphabet_letters(indices):
  15.     alphabet_letters = list()
  16.     for index in indices:
  17.         alphabet_letters.append(ALPHABET[index])
  18.     return ''.join(alphabet_letters)
  19.  
  20.  
  21. def sum_list_values(first_list, second_list):
  22.     result = list()
  23.     for x, y in zip(first_list, second_list):
  24.         total = x + y
  25.         alphabet_length = len(ALPHABET)
  26.         if total >= alphabet_length:
  27.             total %= alphabet_length
  28.         result.append(total)
  29.     return result
  30.  
  31.  
  32. def subtract_list_values(first_list, second_list):
  33.     return [x - y for x, y in zip(first_list, second_list)]
  34.  
  35.  
  36. plaintext = "GEHEIMTEXT"
  37. one_time_pad_text = "PADUGSNQHG"
  38.  
  39. plaintext_indices = convert_to_alphabet_indices(plaintext)
  40. print("Klartext-Indizes:", plaintext_indices)
  41.  
  42. one_time_pad_text_indices = convert_to_alphabet_indices(one_time_pad_text)
  43. print("OTP-Indizes:", one_time_pad_text_indices)
  44.  
  45. cyphertext_indices = sum_list_values(plaintext_indices, one_time_pad_text_indices)
  46. print("Geheimtext-Indizes:", cyphertext_indices)
  47.  
  48. cyphertext = convert_to_alphabet_letters(cyphertext_indices)
  49. print("Geheimtext:", cyphertext)
  50.  
  51. difference_cyphertext_indices_and_otp_indices = subtract_list_values(cyphertext_indices, one_time_pad_text_indices)
  52. print("Differenz zwischen Geheimtext-Indizes und OTP-Indizes:", difference_cyphertext_indices_and_otp_indices)
  53.  
  54. encrypted_text = convert_to_alphabet_letters(difference_cyphertext_indices_and_otp_indices)
  55. print("Entschlüsselter Text:", encrypted_text)
RAW Paste Data