def demo(self, ui): ui.message("===== Demo Mode =====") ui.message("Running a small demo/testing!") ui.message("") ui.message("--- Cyphering ---") text = "prime numbers are widely used in cryptography" ui.message("Data to cypher: {}\n".format(text)) ui.message("Prime cyphered data, base 1: {}" "".format(prime.cypher(text, 1))) ui.message("Prime cyphered data, base 79: {}" "".format(prime.cypher(text, 79))) ui.message("") ui.message("--- Decyphering ---") htext = "457 383 389 449 389 449 421 431 383 347 449 383 " \ "457 347 349 409 367" ui.message("Prime text used as input (base 69): {}".format(htext)) ui.message("The decyphered data is: {}" "".format(prime.decypher(htext, 69))) ui.message("") ui.message("--- Won’t work ---") text = "The next prime year is 2017!" ui.message("+ The input text to cypher must be strict " "ascii lowercase chars and spaces:") ui.message("Data to cypher: {}\n".format(text)) try: ui.message("Prime cyphered data: {}" "".format(prime.cypher(text))) except Exception as e: ui.message(str(e), level=ui.ERROR) ui.message("") ui.message("+ The input text to decypher must be valid Prime " "encoded text:") htext = "13447 24 14 15 15 234 2345 6 24 234 1345 3457 2345 " \ "24 2345 1778 1456" ui.message("Prime text used as input (base 1): {}".format(htext)) try: ui.message("The decyphered data is: {}" "".format(prime.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 cypher(self, ui): """Interactive version of cypher().""" txt = "" ui.message("===== Cypher Mode =====") while 1: done = False while 1: txt = ui.text_input("Text to cypher to Prime", sub_type=ui.LOWER) if txt is None: break # Go back to main Cypher menu. # Get base. base = 1 b = ui.text_input("Base prime to use", sub_type=ui.INT) if b: base = b try: txt = prime.cypher(txt, base) 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 " "Prime, please", options, oneline=True) if answ in {None, "menu"}: return # Go back to main Sema menu. # Else, retry with another data to hide. if done: ui.text_output("Text successfully converted", txt, "Prime version 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: txt = ui.text_input("Text to cypher to Prime", sub_type=ui.LOWER) if txt is None: break # Go back to main Cypher menu. # Get base. base = 1 b = ui.text_input("Base prime to use", sub_type=ui.INT) if b: base = b try: txt = prime.cypher(txt, base) 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 " "Prime, please", options, oneline=True) if answ in {None, "menu"}: return # Go back to main Sema menu. # Else, retry with another data to hide. if done: ui.text_output("Text successfully converted", txt, "Prime version 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