def decypher(self, ui): """Interactive version of decypher().""" txt = "" ui.message("===== Decypher Mode =====") while 1: v, vkw = ui.validate_charset, {"charset": caesar.VALID_CHARSET} txt = ui.text_input("Text to uncypher with caesar", sub_type=ui.UPPER, validate=v, validate_kwargs=vkw) algos = None methods = None # Always try all methods! keys = None # Keys common to all algos. options = ((caesar.ALGO_BASIC, "*basic", ""), (caesar.ALGO_PROGRESS, "*progressive", ""), (caesar.ALGO_SQUARE, "*square", ""), (None, "and/or *all", "")) t = set(ui.get_choice("Which uncyphering algorithm(s) do you " "want to try,", options, oneline=True, multichoices=',')) if None not in t: algos = t if algos and len(algos) == 1: if caesar.ALGO_BASIC in algos: v = ui.validate_number_range vkw = {"minnbr": 1, "maxnbr": 25} tk = ui.text_input("Key(s) to use for basic caesar " "([1 … 25])", indent=1, no_file=True, sub_type=ui.INT_LIST, validate=v, validate_kwargs=vkw) keys = sorted(tk) methods = (caesar.BASIC_BASIC,) elif caesar.ALGO_PROGRESS in algos: v = ui.validate_number_range vkw = {"minnbr": 1, "maxnbr": 25} options = ((caesar.PROGRESS_GEOMETRIC, "*geometric progression", ""), (caesar.PROGRESS_SHIFT, "and/or *shifted unitary progression", "")) tt = set(ui.get_choice("Which progressive method(s) do " "you want to try,", indent=1, options=options, oneline=True, multichoices=',')) methods = [] if caesar.PROGRESS_GEOMETRIC in tt: methods.append(caesar.PROGRESS_GEOMETRIC) if caesar.PROGRESS_SHIFT in tt: methods.append(caesar.PROGRESS_SHIFT) tk = ui.text_input("Key(s) to use for progressive " "caesar ([1 … 25])", no_file=True, indent=1, sub_type=ui.INT_LIST, validate=v, validate_kwargs=vkw) keys = sorted(tk) elif caesar.ALGO_SQUARE in algos: v = ui.validate_number_range vkw = {"minnbr": 2, "maxnbr": caesar.square_max_key(txt)} options = ((caesar.SQUARE_SQUARE, "*squarish", ""), (caesar.SQUARE_CONSTWIDTH, "fixed *width", ""), (caesar.SQUARE_CONSTHIGH, "and/or fixed *high square", "")) tt = set(ui.get_choice("Which square variant(s) do " "you want to try,", indent=1, options=options, oneline=True, multichoices=',')) methods = [] keys = [] if caesar.SQUARE_SQUARE in tt: methods.append(caesar.SQUARE_SQUARE) if ({caesar.SQUARE_CONSTWIDTH, caesar.SQUARE_CONSTHIGH} & tt): tk = ui.text_input("Key(s) to use for non-squarish " "square caesar ([2 … {}])" "".format(vkw["maxnbr"]), sub_type=ui.INT_LIST, no_file=True, indent=1, validate=v, validate_kwargs=vkw) keys = sorted(tk) if caesar.SQUARE_CONSTWIDTH in tt: methods.append(caesar.SQUARE_CONSTWIDTH) if caesar.SQUARE_CONSTHIGH in tt: methods.append(caesar.SQUARE_CONSTHIGH) elif not algos or caesar.ALGO_SQUARE in algos: v = ui.validate_number_range vkw = {"minnbr": 2, "maxnbr": max(25, caesar.square_max_key(txt))} tk = ui.text_input("Key(s) to use for caesar ([2 … {}], or " "nothing)".format(vkw["maxnbr"]), indent=1, sub_type=ui.INT_LIST, allow_void=True, no_file=True, validate=v, validate_kwargs=vkw) if tk: keys = sorted(tk) else: v = ui.validate_number_range vkw = {"minnbr": 2, "maxnbr": 25} tk = ui.text_input("Key(s) to use for caesar ([2 … {}])" "".format(vkw["maxnbr"]), no_file=True, indent=1, sub_type=ui.INT_LIST, validate=v, validate_kwargs=vkw) keys = sorted(tk) try: out = caesar.decypher(txt, algos, methods, keys) except Exception as e: if utils.DEBUG: import traceback traceback.print_tb(sys.exc_info()[2]) ui.message(str(e), level=ui.ERROR) if algos and len(algos) == 1 and keys and len(keys) == 1: ui.text_output("Text successfully decyphered", out, "The decyphered text is") else: t = sorted(out, key=lambda o: o[5], reverse=True) out = [] algos = caesar.TXT_ALGOS_MAP alg_len = caesar.TXT_ALGOS_MAP_MAXLEN methods = caesar.TXT_METHODS_MAP met_len = caesar.TXT_METHODS_MAP_MAXLEN pattern = caesar.TXT_HACKSOLUTIONS_PATTERN for algo, method, key, res, lng, avg in t: out += (pattern.format(avg, lng, algos[algo], methods[method], key, alg_len=alg_len, met_len=met_len), ui.INDENT + res) ui.text_output("Text successfully decyphered", out, "Best solutions found are", maxlen=200, multiline=True, multiblocks=20) options = [("redo", "*decypher another data", ""), ("quit", "or go back to *menu", "")] answ = ui.get_choice("Do you want to", options, oneline=True) if answ == "quit": return
def decypher(self, ui): """Interactive version of decypher().""" txt = "" ui.message("===== Decypher Mode =====") while 1: v, vkw = ui.validate_charset, {"charset": caesar.VALID_CHARSET} txt = ui.text_input("Text to uncypher with caesar", sub_type=ui.UPPER, validate=v, validate_kwargs=vkw) algos = None methods = None # Always try all methods! keys = None # Keys common to all algos. options = ((caesar.ALGO_BASIC, "*basic", ""), (caesar.ALGO_PROGRESS, "*progressive", ""), (caesar.ALGO_SQUARE, "*square", ""), (None, "and/or *all", "")) t = set( ui.get_choice( "Which uncyphering algorithm(s) do you " "want to try,", options, oneline=True, multichoices=',')) if None not in t: algos = t if algos and len(algos) == 1: if caesar.ALGO_BASIC in algos: v = ui.validate_number_range vkw = {"minnbr": 1, "maxnbr": 25} tk = ui.text_input( "Key(s) to use for basic caesar " "([1 … 25])", indent=1, no_file=True, sub_type=ui.INT_LIST, validate=v, validate_kwargs=vkw) keys = sorted(tk) methods = (caesar.BASIC_BASIC, ) elif caesar.ALGO_PROGRESS in algos: v = ui.validate_number_range vkw = {"minnbr": 1, "maxnbr": 25} options = ((caesar.PROGRESS_GEOMETRIC, "*geometric progression", ""), (caesar.PROGRESS_SHIFT, "and/or *shifted unitary progression", "")) tt = set( ui.get_choice( "Which progressive method(s) do " "you want to try,", indent=1, options=options, oneline=True, multichoices=',')) methods = [] if caesar.PROGRESS_GEOMETRIC in tt: methods.append(caesar.PROGRESS_GEOMETRIC) if caesar.PROGRESS_SHIFT in tt: methods.append(caesar.PROGRESS_SHIFT) tk = ui.text_input( "Key(s) to use for progressive " "caesar ([1 … 25])", no_file=True, indent=1, sub_type=ui.INT_LIST, validate=v, validate_kwargs=vkw) keys = sorted(tk) elif caesar.ALGO_SQUARE in algos: v = ui.validate_number_range vkw = {"minnbr": 2, "maxnbr": caesar.square_max_key(txt)} options = ((caesar.SQUARE_SQUARE, "*squarish", ""), (caesar.SQUARE_CONSTWIDTH, "fixed *width", ""), (caesar.SQUARE_CONSTHIGH, "and/or fixed *high square", "")) tt = set( ui.get_choice( "Which square variant(s) do " "you want to try,", indent=1, options=options, oneline=True, multichoices=',')) methods = [] keys = [] if caesar.SQUARE_SQUARE in tt: methods.append(caesar.SQUARE_SQUARE) if ({caesar.SQUARE_CONSTWIDTH, caesar.SQUARE_CONSTHIGH} & tt): tk = ui.text_input("Key(s) to use for non-squarish " "square caesar ([2 … {}])" "".format(vkw["maxnbr"]), sub_type=ui.INT_LIST, no_file=True, indent=1, validate=v, validate_kwargs=vkw) keys = sorted(tk) if caesar.SQUARE_CONSTWIDTH in tt: methods.append(caesar.SQUARE_CONSTWIDTH) if caesar.SQUARE_CONSTHIGH in tt: methods.append(caesar.SQUARE_CONSTHIGH) elif not algos or caesar.ALGO_SQUARE in algos: v = ui.validate_number_range vkw = { "minnbr": 2, "maxnbr": max(25, caesar.square_max_key(txt)) } tk = ui.text_input("Key(s) to use for caesar ([2 … {}], or " "nothing)".format(vkw["maxnbr"]), indent=1, sub_type=ui.INT_LIST, allow_void=True, no_file=True, validate=v, validate_kwargs=vkw) if tk: keys = sorted(tk) else: v = ui.validate_number_range vkw = {"minnbr": 2, "maxnbr": 25} tk = ui.text_input("Key(s) to use for caesar ([2 … {}])" "".format(vkw["maxnbr"]), no_file=True, indent=1, sub_type=ui.INT_LIST, validate=v, validate_kwargs=vkw) keys = sorted(tk) try: out = caesar.decypher(txt, algos, methods, keys) except Exception as e: if utils.DEBUG: import traceback traceback.print_tb(sys.exc_info()[2]) ui.message(str(e), level=ui.ERROR) if algos and len(algos) == 1 and keys and len(keys) == 1: ui.text_output("Text successfully decyphered", out, "The decyphered text is") else: t = sorted(out, key=lambda o: o[5], reverse=True) out = [] algos = caesar.TXT_ALGOS_MAP alg_len = caesar.TXT_ALGOS_MAP_MAXLEN methods = caesar.TXT_METHODS_MAP met_len = caesar.TXT_METHODS_MAP_MAXLEN pattern = caesar.TXT_HACKSOLUTIONS_PATTERN for algo, method, key, res, lng, avg in t: out += (pattern.format(avg, lng, algos[algo], methods[method], key, alg_len=alg_len, met_len=met_len), ui.INDENT + res) ui.text_output("Text successfully decyphered", out, "Best solutions found are", maxlen=200, multiline=True, multiblocks=20) options = [("redo", "*decypher another data", ""), ("quit", "or go back to *menu", "")] answ = ui.get_choice("Do you want to", options, oneline=True) if answ == "quit": return
def cypher(self, ui): """Interactive version of cypher().""" txt = "" ui.message("===== Cypher Mode =====") while 1: done = False while 1: v, vkw = ui.validate_charset, {"charset": caesar.DIC_CHARSET, "charmap": caesar.DIC_CHARMAP} txt = ui.text_input("Text to cypher with caesar", sub_type=ui.UPPER, validate=v, validate_kwargs=vkw) if txt is None: break # Go back to main Cypher menu. algos = [] methods = {} # Only used by progressive and square! keys = {} options = ((caesar.ALGO_BASIC, "*basic", ""), (caesar.ALGO_PROGRESS, "*progressive", ""), (caesar.ALGO_SQUARE, "and/or *square", "")) t = set(ui.get_choice("Which cyphering algorithm(s) do you " "want to use,", options, oneline=True, multichoices=',')) if caesar.ALGO_BASIC in t: a = caesar.ALGO_BASIC algos.append(a) v = ui.validate_number_range vkw = {"minnbr": 1, "maxnbr": 25} tk = ui.text_input("Key(s) to use for basic caesar " "([1 … 25])", indent=1, no_file=True, sub_type=ui.INT_LIST, validate=v, validate_kwargs=vkw) keys[a] = sorted(tk) methods[a] = (caesar.BASIC_BASIC,) if caesar.ALGO_PROGRESS in t: a = caesar.ALGO_PROGRESS algos.append(a) v = ui.validate_number_range vkw = {"minnbr": 1, "maxnbr": 25} options = ((caesar.PROGRESS_GEOMETRIC, "*geometric progression", ""), (caesar.PROGRESS_SHIFT, "and/or *shifted unitary progression", "")) tt = set(ui.get_choice("Which progressive method(s) do " "you want to use,", indent=1, options=options, oneline=True, multichoices=',')) methods[a] = [] if caesar.PROGRESS_GEOMETRIC in tt: methods[a].append(caesar.PROGRESS_GEOMETRIC) if caesar.PROGRESS_SHIFT in tt: methods[a].append(caesar.PROGRESS_SHIFT) tk = ui.text_input("Key(s) to use for progressive " "caesar ([1 … 25])", no_file=True, indent=1, sub_type=ui.INT_LIST, validate=v, validate_kwargs=vkw) keys[a] = sorted(tk) if caesar.ALGO_SQUARE in t: a = caesar.ALGO_SQUARE algos.append(a) v = ui.validate_number_range vkw = {"minnbr": 2, "maxnbr": caesar.square_max_key(txt)} options = ((caesar.SQUARE_SQUARE, "*squarish", ""), (caesar.SQUARE_CONSTWIDTH, "fixed *width", ""), (caesar.SQUARE_CONSTHIGH, "and/or fixed *high square", "")) tt = set(ui.get_choice("Which square variant(s) do " "you want to use,", indent=1, options=options, oneline=True, multichoices=',')) methods[a] = [] keys[a] = [] if caesar.SQUARE_SQUARE in tt: methods[a].append(caesar.SQUARE_SQUARE) if ({caesar.SQUARE_CONSTWIDTH, caesar.SQUARE_CONSTHIGH} & tt): tk = ui.text_input("Key(s) to use for non-squarish " "square caesar ([2 … {}])" "".format(vkw["maxnbr"]), sub_type=ui.INT_LIST, no_file=True, indent=1, validate=v, validate_kwargs=vkw) keys[a] = sorted(tk) if caesar.SQUARE_CONSTWIDTH in tt: methods[a].append(caesar.SQUARE_CONSTWIDTH) if caesar.SQUARE_CONSTHIGH in tt: methods[a].append(caesar.SQUARE_CONSTHIGH) try: txt = self._cypher(txt, algos, methods, keys, ui) done = True # Out of those loops, output result. break except Exception as e: if utils.DEBUG: import traceback traceback.print_tb(sys.exc_info()[2]) ui.message(str(e), level=ui.ERROR) options = [("retry", "*try again", ""), ("menu", "or go back to *menu", "")] answ = ui.get_choice("Could not convert that data into " "caesar, please", options, oneline=True) if answ in {None, "menu"}: return # Go back to main Caesar menu. # Else, retry with another data to hide. if done: ui.text_output("Text successfully converted", txt, "Caesar version(s) of text") options = [("redo", "*cypher another text", ""), ("quit", "or go back to *menu", "")] answ = ui.get_choice("Do you want to", options, oneline=True) if answ in {None, "quit"}: return
def cypher(self, ui): """Interactive version of cypher().""" txt = "" ui.message("===== Cypher Mode =====") while 1: done = False while 1: v, vkw = ui.validate_charset, { "charset": caesar.DIC_CHARSET, "charmap": caesar.DIC_CHARMAP } txt = ui.text_input("Text to cypher with caesar", sub_type=ui.UPPER, validate=v, validate_kwargs=vkw) if txt is None: break # Go back to main Cypher menu. algos = [] methods = {} # Only used by progressive and square! keys = {} options = ((caesar.ALGO_BASIC, "*basic", ""), (caesar.ALGO_PROGRESS, "*progressive", ""), (caesar.ALGO_SQUARE, "and/or *square", "")) t = set( ui.get_choice( "Which cyphering algorithm(s) do you " "want to use,", options, oneline=True, multichoices=',')) if caesar.ALGO_BASIC in t: a = caesar.ALGO_BASIC algos.append(a) v = ui.validate_number_range vkw = {"minnbr": 1, "maxnbr": 25} tk = ui.text_input( "Key(s) to use for basic caesar " "([1 … 25])", indent=1, no_file=True, sub_type=ui.INT_LIST, validate=v, validate_kwargs=vkw) keys[a] = sorted(tk) methods[a] = (caesar.BASIC_BASIC, ) if caesar.ALGO_PROGRESS in t: a = caesar.ALGO_PROGRESS algos.append(a) v = ui.validate_number_range vkw = {"minnbr": 1, "maxnbr": 25} options = ((caesar.PROGRESS_GEOMETRIC, "*geometric progression", ""), (caesar.PROGRESS_SHIFT, "and/or *shifted unitary progression", "")) tt = set( ui.get_choice( "Which progressive method(s) do " "you want to use,", indent=1, options=options, oneline=True, multichoices=',')) methods[a] = [] if caesar.PROGRESS_GEOMETRIC in tt: methods[a].append(caesar.PROGRESS_GEOMETRIC) if caesar.PROGRESS_SHIFT in tt: methods[a].append(caesar.PROGRESS_SHIFT) tk = ui.text_input( "Key(s) to use for progressive " "caesar ([1 … 25])", no_file=True, indent=1, sub_type=ui.INT_LIST, validate=v, validate_kwargs=vkw) keys[a] = sorted(tk) if caesar.ALGO_SQUARE in t: a = caesar.ALGO_SQUARE algos.append(a) v = ui.validate_number_range vkw = {"minnbr": 2, "maxnbr": caesar.square_max_key(txt)} options = ((caesar.SQUARE_SQUARE, "*squarish", ""), (caesar.SQUARE_CONSTWIDTH, "fixed *width", ""), (caesar.SQUARE_CONSTHIGH, "and/or fixed *high square", "")) tt = set( ui.get_choice( "Which square variant(s) do " "you want to use,", indent=1, options=options, oneline=True, multichoices=',')) methods[a] = [] keys[a] = [] if caesar.SQUARE_SQUARE in tt: methods[a].append(caesar.SQUARE_SQUARE) if ({caesar.SQUARE_CONSTWIDTH, caesar.SQUARE_CONSTHIGH} & tt): tk = ui.text_input("Key(s) to use for non-squarish " "square caesar ([2 … {}])" "".format(vkw["maxnbr"]), sub_type=ui.INT_LIST, no_file=True, indent=1, validate=v, validate_kwargs=vkw) keys[a] = sorted(tk) if caesar.SQUARE_CONSTWIDTH in tt: methods[a].append(caesar.SQUARE_CONSTWIDTH) if caesar.SQUARE_CONSTHIGH in tt: methods[a].append(caesar.SQUARE_CONSTHIGH) try: txt = self._cypher(txt, algos, methods, keys, ui) done = True # Out of those loops, output result. break except Exception as e: if utils.DEBUG: import traceback traceback.print_tb(sys.exc_info()[2]) ui.message(str(e), level=ui.ERROR) options = [("retry", "*try again", ""), ("menu", "or go back to *menu", "")] answ = ui.get_choice( "Could not convert that data into " "caesar, please", options, oneline=True) if answ in {None, "menu"}: return # Go back to main Caesar menu. # Else, retry with another data to hide. if done: ui.text_output("Text successfully converted", txt, "Caesar version(s) of text") options = [("redo", "*cypher another text", ""), ("quit", "or go back to *menu", "")] answ = ui.get_choice("Do you want to", options, oneline=True) if answ in {None, "quit"}: return