Guest User

substitutiondecyphertool

a guest
May 9th, 2025
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | Source Code | 0 0
  1. import os
  2.  
  3. # FUNCTIONS
  4. def apply_substitution(cyphered_text, substitution_table, highlighted_char):
  5.     """Applies a substitution table to a cyphered text, with colored output."""
  6.     converted_text = ""
  7.     for char in cyphered_text:
  8.         if char == highlighted_char:
  9.             converted_text += f"\033[0;33m{char}\033[0m"
  10.         elif char in substitution_table:
  11.             converted_text += f"\033[92m{substitution_table[char]}\033[0m"  # Green color
  12.         else:
  13.             converted_text += char
  14.     return converted_text
  15.  
  16. def apply_highlight(text, highlightedchar):
  17.     """highlights a character in a text"""
  18.     highlighted_text = ""
  19.     for char in text:
  20.         if char == highlightedchar:
  21.             highlighted_text += f"\033[0;33m{char}\033[0m"  
  22.         else:
  23.             highlighted_text += char
  24.     return highlighted_text
  25.  
  26. #PROGRAM
  27. cyphered_message = """
  28. 1234567/8393068/3019A8/560037A9C8112C\\4A1587E
  29. 92A95H450L/F
  30. C2AM34A9R23/923031A679
  31. 5H5609CT3/2319C77
  32. 3VA19CT3
  33. 6897
  34. 92CO9E
  35. """
  36. substitution_table = {}
  37. highlighted_char = ""
  38. last_action = ""
  39. while True:
  40.     os.system('cls')
  41.     print("\n---")
  42.     print("Cyphered Message:")
  43.     print(apply_highlight(cyphered_message, highlighted_char))
  44.     print("\n---")
  45.     print("Converted Message:")
  46.     converted_message = apply_substitution(cyphered_message, substitution_table, highlighted_char)
  47.     print(converted_message)
  48.     print("---")
  49.     print("\033[0;36m" + last_action + "\033[0m")
  50.     print("\nActions:")
  51.     print("add-> A [original] [substitute]")
  52.     print("remove-> R [original]")
  53.     print("count-> C [original]")
  54.     print("highlight-> H [original]")
  55.     print("clear-> CLR")
  56.     print("quit-> Q")
  57.     action = input("Enter an action (A[DD], R[EM], C[OUNT], H[IGHLIGHT], CLR, Q[UIT]): ").upper().split()
  58.    
  59.     if not action:
  60.         continue
  61.  
  62.     if action[0] == 'A' or action[0] == 'ADD':
  63.         if len(action) == 3:
  64.             original_char = action[1]
  65.             substitute_char = action[2]
  66.             if len(original_char) == 1 and len(substitute_char) == 1:
  67.                 substitution_table[original_char] = substitute_char
  68.                 last_action= f"Added substitution: '{original_char}' -> '{substitute_char}'"
  69.             else:
  70.                 last_action = "Invalid input. Both original and substitute must be single characters."
  71.         else:
  72.             last_action= "Usage: A[DD] [original_char] [substitute_char]"
  73.     elif action[0] == 'QUIT' or action[0] == 'Q':
  74.         print("Exiting.")
  75.         break
  76.     elif action[0] == 'CLR':
  77.         substitution_table.clear()
  78.         last_action = "Substitution table cleared."
  79.     elif action[0] == 'R' or action[0] == 'REM':
  80.         if len(action) == 2:
  81.             original_char = action[1]
  82.             if original_char in substitution_table:
  83.                 del substitution_table[original_char]
  84.                 last_action= f"Removed substitution for '{original_char}'."
  85.             else:
  86.                 last_action= f"No substitution found for '{original_char}'."
  87.         else:
  88.             last_action= "Usage: R[EM] [original_char]"
  89.     elif action[0] == 'C' or action[0] == 'COUNT':
  90.         if len(action) == 2:
  91.             original_char = action[1]
  92.             count = cyphered_message.count(original_char)
  93.             last_action = f"Occurrences of {original_char}': {count}"
  94.         else:
  95.             last_action= "Usage: C[OUNT] [original_char]"
  96.     elif action[0] == 'H' or action[0] == 'HIGHLIGHT':
  97.         if len(action) == 2:    
  98.             highlighted_char = action[1]
  99.             last_action = f"Highlighted '{highlighted_char}'."
  100.     else:
  101.         last_action="Invalid action. Use 'A[DD]', 'R[EM]', 'C[OUNT]', 'H[IGHLIGHT]', 'CLR', or 'Q[UIT]'."
  102.  
Advertisement
Add Comment
Please, Sign In to add comment