def _cypher(txt, algos, methods, keys, ui): _txt = {caesar.ALGO_BASIC: {caesar.BASIC_BASIC: []}, caesar.ALGO_PROGRESS: {caesar.PROGRESS_GEOMETRIC: [], caesar.PROGRESS_SHIFT: []}, caesar.ALGO_SQUARE: {caesar.SQUARE_SQUARE: [], caesar.SQUARE_CONSTWIDTH: [], caesar.SQUARE_CONSTHIGH: []}, } nbr = 0 for algo in algos: for method in methods[algo]: # XXX This is rather hackish, ugly and weak... # But simplest solution I found so far. :/ if method == caesar.SQUARE_SQUARE: _txt[algo][method] = \ [("", caesar.cypher(txt, algo, None, method))] else: _txt[algo][method] = \ [(k, caesar.cypher(txt, algo, k, method)) for k in keys[algo]] nbr += len(_txt[algo][method]) txt = [] for algo in algos: for method in methods[algo]: if not _txt[algo][method]: continue elif nbr > 1: txt.append(" ".join((caesar.TXT_ALGOS_MAP[algo], caesar.TXT_METHODS_MAP[method]))) txt += ("{}{: >4}: {}".format(ui.INDENT, k, t) for k, t in _txt[algo][method]) else: txt += (t for k, t in _txt[algo][method]) return "\n".join(txt)
def _cypher(txt, algos, methods, keys, ui): _txt = { caesar.ALGO_BASIC: { caesar.BASIC_BASIC: [] }, caesar.ALGO_PROGRESS: { caesar.PROGRESS_GEOMETRIC: [], caesar.PROGRESS_SHIFT: [] }, caesar.ALGO_SQUARE: { caesar.SQUARE_SQUARE: [], caesar.SQUARE_CONSTWIDTH: [], caesar.SQUARE_CONSTHIGH: [] }, } nbr = 0 for algo in algos: for method in methods[algo]: # XXX This is rather hackish, ugly and weak... # But simplest solution I found so far. :/ if method == caesar.SQUARE_SQUARE: _txt[algo][method] = \ [("", caesar.cypher(txt, algo, None, method))] else: _txt[algo][method] = \ [(k, caesar.cypher(txt, algo, k, method)) for k in keys[algo]] nbr += len(_txt[algo][method]) txt = [] for algo in algos: for method in methods[algo]: if not _txt[algo][method]: continue elif nbr > 1: txt.append(" ".join((caesar.TXT_ALGOS_MAP[algo], caesar.TXT_METHODS_MAP[method]))) txt += ("{}{: >4}: {}".format(ui.INDENT, k, t) for k, t in _txt[algo][method]) else: txt += (t for k, t in _txt[algo][method]) return "\n".join(txt)
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)