Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: get_b7_key.py
- # Author: Jeoi Reqi
- """
- This script provides options to encode and decode strings using a BASE-7 cipher.
- [Encode Path]
- User Input --> ASCII --> BASE-7 --> Display Encoded Key
- Expected Output:
- Options:
- 1. Encode
- 2. Decode
- Choose an option (1 or 2): 1
- Enter your string: hello world!
- User Input: hello world!
- Encoded Key : 206 203 213 213 216 44 230 216 222 213 202 45
- [Decode Path]
- User Provides Encoded Key --> BASE-7 --> ASCII --> Decoded Original Text
- Expected Output:
- Options:
- 1. Encode
- 2. Decode
- Choose an option (1 or 2): 2
- Enter the encoded key: 206 203 213 213 216 44 230 216 222 213 202 45
- Decoded Text : hello world!
- """
- def base_7_encoder(num_key):
- """Encode a numerical key to BASE-7."""
- if num_key == 0:
- return '0'
- result = ''
- while num_key > 0:
- remainder = num_key % 7
- result = str(remainder) + result
- num_key //= 7
- return result
- def base_7_decoder(base_7_key):
- """Decode BASE-7 to a numerical key."""
- num_key = 0
- for digit in base_7_key:
- num_key = num_key * 7 + int(digit)
- return num_key
- def handle_encode():
- """Handle the encoding process."""
- user_input = input("Enter your string: ")
- ascii_values = [ord(char) for char in user_input]
- base_7_encoded_key = ' '.join([base_7_encoder(value) for value in ascii_values])
- print('\nUser Input: ', user_input, '\n')
- print('Encoded Key : ', base_7_encoded_key, '\n')
- def handle_decode():
- """Handle the decoding process."""
- encoded_key = input("Enter the encoded key: ")
- ascii_values = [base_7_decoder(value) for value in encoded_key.split()]
- decoded_text = ''.join([chr(value) for value in ascii_values])
- print('\nDecoded Text : ', decoded_text)
- # Main script
- print("Options:")
- print("1. Encode")
- print("2. Decode")
- choice = input("Choose an option (1 or 2): ")
- if choice == '1':
- handle_encode()
- elif choice == '2':
- handle_decode()
- else:
- print("Invalid choice. Please choose 1 or 2.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement