Advertisement
Python253

simple_music_cipher_crude_brute

Mar 5th, 2024 (edited)
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.94 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: simple_music_cipher.py
  4. # Author: Jeoi Reqi
  5. # Original Key File:   https://pastebin.com/sHMQDfhM
  6.  
  7. """
  8. !!NOTICE!!
  9.  
  10. THIS IS A CRUDE BRUTE FORCE DECODER AND IS HIGHLY
  11. LIMITED IN FUNCTIONALITY & INPUT LENGTHS. THIS WILL
  12. MOST LIKELY GIVE BAD RESULTS UNLESS COMMON WORDS OF
  13. SHORT LENGTHS. I WILL BE UPDATING THIS SCRIPT SHORTLY.
  14. ANY ADVICE TO HOW TO PROPERLY DECODE A STRING USING
  15. A KEY WHERE MULTIPLE VALUES ARE GIVEN FOR EACH LETTER.
  16. NEXT ATTEMPT WILL INVOLVE INDEXING THE KEY VALUES AND
  17. THEN CYCLING THEM RECURSIVELY AND OUTPUT ALL VARIABLE
  18. POSSIBILITIES TO THE TERMINAL FOR THE USER TO SELECT.
  19.  
  20.  
  21. This script demonstrates a simple music cipher, allowing users to encode and decode a short message using a specified key for notes A-G.
  22. The decoding utilizes the NLTK words dataset and brute force methods to generate possible decodings.
  23.  
  24. Requirements:
  25. - Python 3
  26. - NLTK (Natural Language Toolkit) library: Install it using 'pip install nltk'
  27. - NLTK words dataset: If not present, uncomment 'nltk.download('words')' and run the script. Comment it out once downloaded.
  28.  
  29. Encoding Example:
  30.    message_to_encode = "hello world"  #Short strings w/ common words work the best in this version.
  31.    encoded_message = encode(message_to_encode)
  32.    print(f"Encoded: {encoded_message}")
  33.  
  34. Decoding Example:
  35.    print("Possible Decodings:")
  36.    decode(encoded_message)
  37.  
  38. Usage:
  39. 1. Run the script and enter the message to encode.
  40. 2. The encoded message will be displayed, and possible decodings will be generated.
  41. 3. Optionally, save the decoding results to a file.
  42.  
  43. Note: Numbers in the message are ignored during encoding and decoding.
  44.  
  45. For more information on the cipher and its cryptanalysis, refer to the references included in the script.
  46.  
  47. """
  48.  
  49. import nltk
  50. import os
  51.  
  52. # Uncomment the line below if NLTK words dataset is not present on your system
  53. # nltk.download('words')
  54.  
  55. def encode(message):
  56.     key = {
  57.         'a': 'a', 'b': 'b', 'c': 'c',
  58.         'd': 'd', 'e': 'e', 'f': 'f',
  59.         'g': 'g', 'h': 'a', 'i': 'b',
  60.         'j': 'c', 'k': 'd', 'l': 'e',
  61.         'm': 'f', 'n': 'g', 'o': 'a',
  62.         'p': 'b', 'q': 'c', 'r': 'd',
  63.         's': 'e', 't': 'f', 'u': 'g',
  64.         'v': 'a', 'w': 'b', 'x': 'c',
  65.         'y': 'd', 'z': 'e',
  66.         ' ': ' '  # add space character to the key
  67.     }
  68.  
  69.     encoded_message = ''.join([key.get(char.lower(), char) if char.isalpha() else char for char in message])
  70.     return encoded_message
  71.  
  72. def decode(encoded_message):
  73.     reverse_key = {
  74.         'a': 'ahov', 'b': 'bipw', 'c': 'cjqx',
  75.         'd': 'dkry', 'e': 'elsz', 'f': 'fmt',
  76.         'g': 'gnu'
  77.     }
  78.  
  79.     english_words = set(nltk.corpus.words.words())
  80.  
  81.     def generate_possible_decodings(decoded_prefix, remaining_encoded, all_combinations):
  82.         if not remaining_encoded:
  83.             if decoded_prefix.lower() in english_words:
  84.                 all_combinations.append(decoded_prefix.strip())
  85.             return
  86.  
  87.         current_char = remaining_encoded[0]
  88.         possibilities = reverse_key.get(current_char, [current_char])
  89.  
  90.         for possibility in possibilities:
  91.             new_decoded_prefix = decoded_prefix + possibility
  92.             generate_possible_decodings(new_decoded_prefix, remaining_encoded[1:], all_combinations)
  93.  
  94.     def decode_message(remaining_words, decoded_sentence, all_combinations):
  95.         if not remaining_words:
  96.             all_combinations.append(decoded_sentence.strip())
  97.             return
  98.  
  99.         current_word = remaining_words[0]
  100.         word_combinations = []
  101.  
  102.         generate_possible_decodings('', current_word, word_combinations)
  103.  
  104.         for word_combination in word_combinations:
  105.             new_decoded_sentence = decoded_sentence + word_combination + ' '
  106.             decode_message(remaining_words[1:], new_decoded_sentence, all_combinations)
  107.  
  108.     def print_combinations(combinations):
  109.         for combination in combinations:
  110.             print(f"\t\t{combination}")
  111.  
  112.     words = encoded_message.split()
  113.     all_combinations = []
  114.  
  115.     decode_message(words, '', all_combinations)
  116.     print_combinations(all_combinations)
  117.  
  118.     return all_combinations
  119.  
  120. def save_to_file(combinations):
  121.     filename = 'decoding_results.txt'
  122.     with open(filename, 'w') as file:
  123.         for combination in combinations:
  124.             file.write(f"{combination}\n")
  125.     print(f"Results saved to {filename}")
  126.  
  127. # Example usage:
  128. print("\n:: [SIMPLE MUSIC ENCODER/DECODER] ::\n\n")
  129. user_message = input("\tEnter the message to encode: ")
  130. encoded_message = encode(user_message)
  131. print(f"\n\tEncoded: {encoded_message}\n")
  132.  
  133. print("\nPossible Decodings:\n")
  134. decoded_combinations = decode(encoded_message)
  135.  
  136. 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?) ")
  137. if save_option == '1':
  138.     save_to_file(decoded_combinations)
  139. else:
  140.     print("\nProgram Ending. Thank you!\n")
  141.  
  142.  
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement