Advertisement
Python253

num_key_alpha_freq_cipher_full

Mar 5th, 2024
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: num_key_alpha_freq_cipher_full.py
  4.  
  5.  
  6. """
  7. This script automatically encodes and decodes a user input string using a letter frequency & a BASE-7 cipher.
  8.  
  9. [Encode Path]
  10. User Input --> Letter Freequency --> Numerical Key
  11. User Input --> ASCII --> BASE-7 --> Display Encoded Key
  12.  
  13. [Decode Path]
  14. Numerical Key --> Encoded BASE-7 Key --> Encoded ASCII --> DECODED
  15.  
  16. Example Output:
  17. Enter your string: hello world!
  18.  
  19. User Input:             hello world!
  20.  
  21.                        abcdefghijklmnopqrstuvwxyz
  22.                        --------------------------
  23. Numerical Key:          00011001000300200100001000
  24.  
  25. Encoded BASE-7 Key:     111264533620221435234501460
  26. Encoded ASCII:          206 203 213 213 216 44 230 216 222 213 202 45
  27.  
  28. Decoded Numerical Key:  11001000300200100001000
  29. Decoded In Plain Text:  hello world!
  30. """
  31.  
  32. def base_7_encoder(num_key):
  33.     if num_key == 0:
  34.         return '0'
  35.  
  36.     result = ''
  37.     while num_key > 0:
  38.         remainder = num_key % 7
  39.         result = str(remainder) + result
  40.         num_key //= 7
  41.  
  42.     return result
  43.  
  44. def base_7_decoder(base_7_key):
  45.     num_key = 0
  46.     for digit in base_7_key:
  47.         num_key = num_key * 7 + int(digit)
  48.     return num_key
  49.  
  50. def text_to_base_7(text):
  51.     base_7_result = ''
  52.     for char in text:
  53.         char_value = ord(char)
  54.         base_7_result += base_7_encoder(char_value) + ' '
  55.  
  56.     return base_7_result.strip()
  57.  
  58. def base_7_text_decoder(base_7_text):
  59.     decoded_text = ''
  60.     for base_7_digit in base_7_text.split():
  61.         char_value = base_7_decoder(base_7_digit)
  62.         decoded_text += chr(char_value)
  63.     return decoded_text
  64.  
  65. # Global Variable Alphabet
  66. alphabet = 'abcdefghijklmnopqrstuvwxyz'
  67.  
  68. def count_letters(input_string):
  69.     letter_count = {char: 0 for char in alphabet}
  70.  
  71.     for char in input_string:
  72.         if char.isalpha():
  73.             letter_count[char.lower()] += 1
  74.  
  75.     count_string = ''.join(str(letter_count[char]) for char in alphabet)
  76.     return count_string
  77.  
  78. # Get User Input
  79. user_input = input("Enter your string: ")
  80. num_key = count_letters(user_input)
  81.  
  82. # Convert numerical key to base-7
  83. base_7_encoded_key = base_7_encoder(int(num_key, 10))
  84.  
  85. # Convert text to base-7
  86. base_7_encoded_text = text_to_base_7(user_input)
  87.  
  88. # Output Results
  89. print('\nUser Input:            ', user_input, '\n')
  90. print('\t\t\t' + alphabet, '\n\t\t\t--------------------------')
  91. print('Numerical Key:         ', num_key)
  92. print('\nEncoded BASE-7 Key:    ', base_7_encoded_key)
  93. print('Encoded ASCII:         ', base_7_encoded_text)
  94.  
  95. # Decode base-7 key
  96. decoded_num_key = base_7_decoder(base_7_encoded_key)
  97.  
  98. # Decode base-7 text
  99. decoded_text = base_7_text_decoder(base_7_encoded_text)
  100.  
  101. # Output Decoded Results
  102. print('\nDecoded Numerical Key: ', decoded_num_key)
  103. print('Decoded In Plain Text: ', decoded_text)
  104.  
  105.  
  106.  
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement