-2

I am attempting a cipher script where each encoded character could be one of multiple letters, ex: BC = a w k. the encoding part of the script was simple, however i'm running into a problem while trying to decode sentences. because each encoded character looks like BC i have to split the entire string into 2's, then I have to check the key file to see which letters are possible from each character, so could look like TVVQBTTV and the lists would look like:

['G', 'H', 'P', 'R', 'T', 'Y']
['G', 'H', 'P', 'R', 'T', 'Y', 'E', 'L', 'Z', '.']
['G', 'H', 'P', 'R', 'T', 'Y', 'E', 'L', 'Z', '.', 'A', 'F', 'M', 'O', 'S', 'W', 'Z']
['G', 'H', 'P', 'R', 'T', 'Y', 'E', 'L', 'Z', '.', 'A', 'F', 'M', 'O', 'S', 'W', 'Z', 'G', 'H', 'P', 'R', 'T', 'Y']

my goal is to print out every possible combination (ex: GGGG, GGGH, GGGP, ect) in the console so the person receiving the message has to look through all of the combinations to find the right one. this is an attempt to make the cipher harder to break. however, because the amount of lists grow with the amount of characters in the sentence, so 'Hello, how are you?' could look like: TVVQVQVQBTVPBWVOBTTBTVVQDDVOBW and there are to many lists for it to put in this box without it looking thoroughly messy. so is there any way to do this?

1 Answers1

0

I'm a bit confused why your 4 lists extend the previous lists, rather than being 4 separate lists mapping to the possible values of TV, VQ, BT, and TV. In other words I don't get why the possible values of the key 'VQ' include the possible values of 'TV', and so on. But as the question is written:

def get_list_of_possible_vals(key):
    # check key file
    return ['a','b','c','d'] # just an example; list of characters the given key 
                             # could map to 

encoded_list = ['TV','VQ','VQ','VQ','BT','VP']  # input data
decoded_list = []
list_of_lists = []
for enc in encoded_list:
    decoded_list += get_list_of_possible_vals(enc)
    list_of_lists.append(decoded_list)
    # If you want the lists of decoded values to be separate rather than stacking,
    # do decoded_list.append() instead

# Cartesian product of the lists
for element in itertools.product(*list_of_lists):
    print(''.join(element))
Sam Szotkowski
  • 344
  • 1
  • 6
  • each character has two possible ways, for example: A.BT-TB so the letter, A then . to separate, so i can remove that. then i can separate the two possible encodings with the -. then I use random.randint to randomly choose one of the possible encodings. so T could look like DC or TV. does that make sense? – Storm Wolf Apr 04 '21 at 19:20
  • Sounds like that pertains to the internals of the `get_list_of_possible_vals` function or the creation of `encoded_list` which I'm assuming you've already figured out. – Sam Szotkowski Apr 04 '21 at 19:24