Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: num_key_alpha_freq_cipher_full.py
- """
- This script automatically encodes and decodes a user input string using a letter frequency & a BASE-7 cipher.
- [Encode Path]
- User Input --> Letter Freequency --> Numerical Key
- User Input --> ASCII --> BASE-7 --> Display Encoded Key
- [Decode Path]
- Numerical Key --> Encoded BASE-7 Key --> Encoded ASCII --> DECODED
- Example Output:
- Enter your string: hello world!
- User Input: hello world!
- abcdefghijklmnopqrstuvwxyz
- --------------------------
- Numerical Key: 00011001000300200100001000
- Encoded BASE-7 Key: 111264533620221435234501460
- Encoded ASCII: 206 203 213 213 216 44 230 216 222 213 202 45
- Decoded Numerical Key: 11001000300200100001000
- Decoded In Plain Text: hello world!
- """
- def base_7_encoder(num_key):
- 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):
- num_key = 0
- for digit in base_7_key:
- num_key = num_key * 7 + int(digit)
- return num_key
- def text_to_base_7(text):
- base_7_result = ''
- for char in text:
- char_value = ord(char)
- base_7_result += base_7_encoder(char_value) + ' '
- return base_7_result.strip()
- def base_7_text_decoder(base_7_text):
- decoded_text = ''
- for base_7_digit in base_7_text.split():
- char_value = base_7_decoder(base_7_digit)
- decoded_text += chr(char_value)
- return decoded_text
- # Global Variable Alphabet
- alphabet = 'abcdefghijklmnopqrstuvwxyz'
- def count_letters(input_string):
- letter_count = {char: 0 for char in alphabet}
- for char in input_string:
- if char.isalpha():
- letter_count[char.lower()] += 1
- count_string = ''.join(str(letter_count[char]) for char in alphabet)
- return count_string
- # Get User Input
- user_input = input("Enter your string: ")
- num_key = count_letters(user_input)
- # Convert numerical key to base-7
- base_7_encoded_key = base_7_encoder(int(num_key, 10))
- # Convert text to base-7
- base_7_encoded_text = text_to_base_7(user_input)
- # Output Results
- print('\nUser Input: ', user_input, '\n')
- print('\t\t\t' + alphabet, '\n\t\t\t--------------------------')
- print('Numerical Key: ', num_key)
- print('\nEncoded BASE-7 Key: ', base_7_encoded_key)
- print('Encoded ASCII: ', base_7_encoded_text)
- # Decode base-7 key
- decoded_num_key = base_7_decoder(base_7_encoded_key)
- # Decode base-7 text
- decoded_text = base_7_text_decoder(base_7_encoded_text)
- # Output Decoded Results
- print('\nDecoded Numerical Key: ', decoded_num_key)
- print('Decoded In Plain Text: ', decoded_text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement