Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: simple_music_cipher.py
- # Author: Jeoi Reqi
- # Original Key File: https://pastebin.com/sHMQDfhM
- """
- !!NOTICE!!
- THIS IS A CRUDE BRUTE FORCE DECODER AND IS HIGHLY
- LIMITED IN FUNCTIONALITY & INPUT LENGTHS. THIS WILL
- MOST LIKELY GIVE BAD RESULTS UNLESS COMMON WORDS OF
- SHORT LENGTHS. I WILL BE UPDATING THIS SCRIPT SHORTLY.
- ANY ADVICE TO HOW TO PROPERLY DECODE A STRING USING
- A KEY WHERE MULTIPLE VALUES ARE GIVEN FOR EACH LETTER.
- NEXT ATTEMPT WILL INVOLVE INDEXING THE KEY VALUES AND
- THEN CYCLING THEM RECURSIVELY AND OUTPUT ALL VARIABLE
- POSSIBILITIES TO THE TERMINAL FOR THE USER TO SELECT.
- This script demonstrates a simple music cipher, allowing users to encode and decode a short message using a specified key for notes A-G.
- The decoding utilizes the NLTK words dataset and brute force methods to generate possible decodings.
- Requirements:
- - Python 3
- - NLTK (Natural Language Toolkit) library: Install it using 'pip install nltk'
- - NLTK words dataset: If not present, uncomment 'nltk.download('words')' and run the script. Comment it out once downloaded.
- Encoding Example:
- message_to_encode = "hello world" #Short strings w/ common words work the best in this version.
- encoded_message = encode(message_to_encode)
- print(f"Encoded: {encoded_message}")
- Decoding Example:
- print("Possible Decodings:")
- decode(encoded_message)
- Usage:
- 1. Run the script and enter the message to encode.
- 2. The encoded message will be displayed, and possible decodings will be generated.
- 3. Optionally, save the decoding results to a file.
- Note: Numbers in the message are ignored during encoding and decoding.
- For more information on the cipher and its cryptanalysis, refer to the references included in the script.
- """
- import nltk
- import os
- # Uncomment the line below if NLTK words dataset is not present on your system
- # nltk.download('words')
- def encode(message):
- key = {
- 'a': 'a', 'b': 'b', 'c': 'c',
- 'd': 'd', 'e': 'e', 'f': 'f',
- 'g': 'g', 'h': 'a', 'i': 'b',
- 'j': 'c', 'k': 'd', 'l': 'e',
- 'm': 'f', 'n': 'g', 'o': 'a',
- 'p': 'b', 'q': 'c', 'r': 'd',
- 's': 'e', 't': 'f', 'u': 'g',
- 'v': 'a', 'w': 'b', 'x': 'c',
- 'y': 'd', 'z': 'e',
- ' ': ' ' # add space character to the key
- }
- encoded_message = ''.join([key.get(char.lower(), char) if char.isalpha() else char for char in message])
- return encoded_message
- def decode(encoded_message):
- reverse_key = {
- 'a': 'ahov', 'b': 'bipw', 'c': 'cjqx',
- 'd': 'dkry', 'e': 'elsz', 'f': 'fmt',
- 'g': 'gnu'
- }
- english_words = set(nltk.corpus.words.words())
- def generate_possible_decodings(decoded_prefix, remaining_encoded, all_combinations):
- if not remaining_encoded:
- if decoded_prefix.lower() in english_words:
- all_combinations.append(decoded_prefix.strip())
- return
- current_char = remaining_encoded[0]
- possibilities = reverse_key.get(current_char, [current_char])
- for possibility in possibilities:
- new_decoded_prefix = decoded_prefix + possibility
- generate_possible_decodings(new_decoded_prefix, remaining_encoded[1:], all_combinations)
- def decode_message(remaining_words, decoded_sentence, all_combinations):
- if not remaining_words:
- all_combinations.append(decoded_sentence.strip())
- return
- current_word = remaining_words[0]
- word_combinations = []
- generate_possible_decodings('', current_word, word_combinations)
- for word_combination in word_combinations:
- new_decoded_sentence = decoded_sentence + word_combination + ' '
- decode_message(remaining_words[1:], new_decoded_sentence, all_combinations)
- def print_combinations(combinations):
- for combination in combinations:
- print(f"\t\t{combination}")
- words = encoded_message.split()
- all_combinations = []
- decode_message(words, '', all_combinations)
- print_combinations(all_combinations)
- return all_combinations
- def save_to_file(combinations):
- filename = 'decoding_results.txt'
- with open(filename, 'w') as file:
- for combination in combinations:
- file.write(f"{combination}\n")
- print(f"Results saved to {filename}")
- # Example usage:
- print("\n:: [SIMPLE MUSIC ENCODER/DECODER] ::\n\n")
- user_message = input("\tEnter the message to encode: ")
- encoded_message = encode(user_message)
- print(f"\n\tEncoded: {encoded_message}\n")
- print("\nPossible Decodings:\n")
- decoded_combinations = decode(encoded_message)
- save_option = input("\nDo you want to save the decoding results to a file?\n\n1: Yes\n2: No\n\nWhat is your choice (1 Or 2?) ")
- if save_option == '1':
- save_to_file(decoded_combinations)
- else:
- print("\nProgram Ending. Thank you!\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement