Advertisement
Python253

get_b7_key

Mar 5th, 2024
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.15 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: get_b7_key.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. This script provides options to encode and decode strings using a BASE-7 cipher.
  8.  
  9. [Encode Path]
  10. User Input --> ASCII --> BASE-7 --> Display Encoded Key
  11. Expected Output:
  12.    Options:
  13.    1. Encode
  14.    2. Decode
  15.    Choose an option (1 or 2): 1
  16.    Enter your string: hello world!
  17.  
  18.    User Input:  hello world!
  19.    Encoded Key  :  206 203 213 213 216 44 230 216 222 213 202 45
  20.  
  21. [Decode Path]
  22. User Provides Encoded Key --> BASE-7 --> ASCII --> Decoded Original Text
  23. Expected Output:
  24.    Options:
  25.    1. Encode
  26.    2. Decode
  27.    Choose an option (1 or 2): 2
  28.    Enter the encoded key: 206 203 213 213 216 44 230 216 222 213 202 45
  29.  
  30.    Decoded Text :  hello world!
  31. """
  32.  
  33. def base_7_encoder(num_key):
  34.     """Encode a numerical key to BASE-7."""
  35.     if num_key == 0:
  36.         return '0'
  37.  
  38.     result = ''
  39.     while num_key > 0:
  40.         remainder = num_key % 7
  41.         result = str(remainder) + result
  42.         num_key //= 7
  43.  
  44.     return result
  45.  
  46. def base_7_decoder(base_7_key):
  47.     """Decode BASE-7 to a numerical key."""
  48.     num_key = 0
  49.     for digit in base_7_key:
  50.         num_key = num_key * 7 + int(digit)
  51.     return num_key
  52.  
  53. def handle_encode():
  54.     """Handle the encoding process."""
  55.     user_input = input("Enter your string: ")
  56.     ascii_values = [ord(char) for char in user_input]
  57.     base_7_encoded_key = ' '.join([base_7_encoder(value) for value in ascii_values])
  58.     print('\nUser Input: ', user_input, '\n')
  59.     print('Encoded Key  : ', base_7_encoded_key, '\n')
  60.  
  61. def handle_decode():
  62.     """Handle the decoding process."""
  63.     encoded_key = input("Enter the encoded key: ")
  64.     ascii_values = [base_7_decoder(value) for value in encoded_key.split()]
  65.     decoded_text = ''.join([chr(value) for value in ascii_values])
  66.     print('\nDecoded Text : ', decoded_text)
  67.  
  68. # Main script
  69. print("Options:")
  70. print("1. Encode")
  71. print("2. Decode")
  72.  
  73. choice = input("Choose an option (1 or 2): ")
  74.  
  75. if choice == '1':
  76.     handle_encode()
  77. elif choice == '2':
  78.     handle_decode()
  79. else:
  80.     print("Invalid choice. Please choose 1 or 2.")
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement