def demo(self, ui): ui.message("===== Demo Mode =====") ui.message("Running a small demo/testing!") ui.message("") text = "JULE CAESAR WAS A FAMOUS ROMAN GENERAL" ui.message("--- Cyphering ---") ui.message("(Note: you can cypher a same text with different " "algorithms/keys at once.)") ui.message("Data to cypher: {}\n".format(text)) ui.message("Caesar Basic cyphered data, key 13: {}" "".format(caesar.cypher(text, caesar.ALGO_BASIC, 13))) ui.message("Caesar Progressive cyphered data, geometric progression, " "key 7: {}" "".format(caesar.cypher(text, caesar.ALGO_PROGRESS, 7, caesar.PROGRESS_GEOMETRIC))) ui.message("Caesar Progressive cyphered data, shifted unitary " "progression, key 5: {}" "".format(caesar.cypher(text, caesar.ALGO_PROGRESS, 5, caesar.PROGRESS_SHIFT))) ui.message("Caesar Square cyphered data, squarish square: {}" "".format(caesar.cypher(text, caesar.ALGO_SQUARE, None, caesar.SQUARE_SQUARE))) ui.message("Caesar Square cyphered data, constant width “square”, " "key 3: {}" "".format(caesar.cypher(text, caesar.ALGO_SQUARE, 3, caesar.SQUARE_CONSTWIDTH))) ui.message("Caesar Square cyphered data, constant high “square”, " "key 3: {}" "".format(caesar.cypher(text, caesar.ALGO_SQUARE, 3, caesar.SQUARE_CONSTHIGH))) text = text.replace(' ', '') ui.message("Please note, however, that space-less data will be much " "harder to hack. With progressive algorithm, it even " "completely changes the results (and square algo always " "removes them, anyway!):") ui.message("Data to cypher: {}\n".format(text)) ui.message("Caesar Basic cyphered data, key 13: {}" "".format(caesar.cypher(text, caesar.ALGO_BASIC, 13))) ui.message("Caesar Progressive cyphered data, geometric progression, " "key 7: {}" "".format(caesar.cypher(text, caesar.ALGO_PROGRESS, 7, caesar.PROGRESS_GEOMETRIC))) ui.message("Caesar Progressive cyphered data, shifted unitary " "progression, key 5: {}" "".format(caesar.cypher(text, caesar.ALGO_PROGRESS, 5, caesar.PROGRESS_SHIFT))) ui.message("") htext = "JSEAUAOUANERSLLRCSACIITOPNETOANATNICMSNESCABCASOOEIODUP" ui.message("--- Decyphering ---") ui.message("In addition to usual known algo/key uncyphering, Caesar " "can also hack himself! It will then propose you all " "possible outputs, sorted by relevance.") ui.message("Caesar text used as input: {}".format(htext)) out = caesar.decypher(htext, None, None, None) 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.message("Best solutions found are:\n\n" + "\n\n".join(out[:20])) ui.message("Note: In real situation, you’ll have the choice to see " "more solutions if you like! ;)") ui.message("") # ui.message("--- Won’t work ---") # ui.message("+ The input text to cypher must be acsii lowercase " # "letters only:") # ui.message("Data to cypher: {}\n".format("Hello Wolrd!")) # try: # ui.message("Celldrawer cyphered data: {}" # "".format(celldrawer.cypher("Hello World!"))) # except Exception as e: # ui.message(str(e), level=ui.ERROR) # ui.message("") # ui.message("+ The input text to decypher must be phone digits only:") # htext = "123580 147*369#8 321457*0# 1N7*369#8 *74269#8 32470# " \ # "147*538# *74269#8 *7412690 321457*0# *741k369# 15380!" # ui.message("Celldrawer text used as input: {}".format(htext)) # try: # ui.message("The decyphered data is: {}" # "".format(celldrawer.decypher(htext))) # except Exception as e: # ui.message(str(e), level=ui.ERROR) # ui.message("") ui.get_choice("", [("", "Go back to $menu", "")], oneline=True)
def demo(self, ui): ui.message("===== Demo Mode =====") ui.message("Running a small demo/testing!") ui.message("") text = "JULE CAESAR WAS A FAMOUS ROMAN GENERAL" ui.message("--- Cyphering ---") ui.message("(Note: you can cypher a same text with different " "algorithms/keys at once.)") ui.message("Data to cypher: {}\n".format(text)) ui.message("Caesar Basic cyphered data, key 13: {}" "".format(caesar.cypher(text, caesar.ALGO_BASIC, 13))) ui.message("Caesar Progressive cyphered data, geometric progression, " "key 7: {}" "".format( caesar.cypher(text, caesar.ALGO_PROGRESS, 7, caesar.PROGRESS_GEOMETRIC))) ui.message("Caesar Progressive cyphered data, shifted unitary " "progression, key 5: {}" "".format( caesar.cypher(text, caesar.ALGO_PROGRESS, 5, caesar.PROGRESS_SHIFT))) ui.message("Caesar Square cyphered data, squarish square: {}" "".format( caesar.cypher(text, caesar.ALGO_SQUARE, None, caesar.SQUARE_SQUARE))) ui.message("Caesar Square cyphered data, constant width “square”, " "key 3: {}" "".format( caesar.cypher(text, caesar.ALGO_SQUARE, 3, caesar.SQUARE_CONSTWIDTH))) ui.message("Caesar Square cyphered data, constant high “square”, " "key 3: {}" "".format( caesar.cypher(text, caesar.ALGO_SQUARE, 3, caesar.SQUARE_CONSTHIGH))) text = text.replace(' ', '') ui.message("Please note, however, that space-less data will be much " "harder to hack. With progressive algorithm, it even " "completely changes the results (and square algo always " "removes them, anyway!):") ui.message("Data to cypher: {}\n".format(text)) ui.message("Caesar Basic cyphered data, key 13: {}" "".format(caesar.cypher(text, caesar.ALGO_BASIC, 13))) ui.message("Caesar Progressive cyphered data, geometric progression, " "key 7: {}" "".format( caesar.cypher(text, caesar.ALGO_PROGRESS, 7, caesar.PROGRESS_GEOMETRIC))) ui.message("Caesar Progressive cyphered data, shifted unitary " "progression, key 5: {}" "".format( caesar.cypher(text, caesar.ALGO_PROGRESS, 5, caesar.PROGRESS_SHIFT))) ui.message("") htext = "JSEAUAOUANERSLLRCSACIITOPNETOANATNICMSNESCABCASOOEIODUP" ui.message("--- Decyphering ---") ui.message("In addition to usual known algo/key uncyphering, Caesar " "can also hack himself! It will then propose you all " "possible outputs, sorted by relevance.") ui.message("Caesar text used as input: {}".format(htext)) out = caesar.decypher(htext, None, None, None) 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.message("Best solutions found are:\n\n" + "\n\n".join(out[:20])) ui.message("Note: In real situation, you’ll have the choice to see " "more solutions if you like! ;)") ui.message("") # ui.message("--- Won’t work ---") # ui.message("+ The input text to cypher must be acsii lowercase " # "letters only:") # ui.message("Data to cypher: {}\n".format("Hello Wolrd!")) # try: # ui.message("Celldrawer cyphered data: {}" # "".format(celldrawer.cypher("Hello World!"))) # except Exception as e: # ui.message(str(e), level=ui.ERROR) # ui.message("") # ui.message("+ The input text to decypher must be phone digits only:") # htext = "123580 147*369#8 321457*0# 1N7*369#8 *74269#8 32470# " \ # "147*538# *74269#8 *7412690 321457*0# *741k369# 15380!" # ui.message("Celldrawer text used as input: {}".format(htext)) # try: # ui.message("The decyphered data is: {}" # "".format(celldrawer.decypher(htext))) # except Exception as e: # ui.message(str(e), level=ui.ERROR) # ui.message("") ui.get_choice("", [("", "Go back to $menu", "")], oneline=True)
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