Example #1
0
    def demo(self, ui):
        ui.message("===== Demo Mode =====")
        ui.message("Running a small demo/testing!")

        ui.message("--- Encoding ---")
        text = "Hello World!"
        ui.message("Data to cypher: {}\n".format(text))
        ui.message("Gray cyphered data (3, 4, 5 and 8 bits words, utf-8):"
                   "\n    {}".format("\n    ".join(gray.cypher(text,
                                                   lengths=(3, 4, 5, 8)))))
        ui.message("+ Note how padding bits are added to get an integer "
                   "number of words of n bits.")
        ui.message("")

        ui.message("--- Decoding ---")
        ui.message("+ You must specify the word length used at encode time "
                   "(which must be a multiple of number of binary bits given "
                   "to decypher).")
        htext = "0110010101101010010100011101101101000010010110101011000" \
                "001100100110010110100000101000001001100101100000010110111000"
        ui.message("“Numbers” utf-8 text used as input (5 bits words): {}"
                   "".format(htext))
        ui.message("The decypherd data is: {}"
                   "".format(gray.decypher(htext, codec="utf-8", length=5)))
        ui.message("")

        ui.message("+ The input text to decypher may have space-separated "
                   "bytes:")
        htext = "10010011 11000000 11110101 01001100 01010001 01001010 " \
                "00110000 01010001 00110000 01100011 01010111 01011010 " \
                "01011010 00110000 01101010 01010001 01010011 01001010 " \
                "10010011 11000000 11010101 00110000 01001011 01010111 " \
                "01001010 01010111 01010001 01010010 01011100 01010111 " \
                "01001011 00111001"
        ui.message("“binary” utf-8, 8 bits words, text used as input: {}"
                   "".format(htext))
        ui.message("The decypherd data is: {}"
                   "".format(gray.decypher(htext, codec="utf-8", length=8)))
        ui.message("")

        ui.message("--- Won’t work ---")
        ui.message("+ The input text to decypher must contain only valid "
                   "binary digits (and optionally spaces):")
        htext = "011001010111211101101100015000110110111101101101011a" \
                "001010010001"
        ui.message("“Numbers” text used as binary input: {}".format(htext))
        try:
            ui.message("The decypherd data is: {}"
                       "".format(gray.decypher(htext, codec="ascii",
                                               length=8)))
        except Exception as e:
            ui.message(str(e), level=ui.ERROR)
        ui.message("")

        ui.message("+ The input text to decypher must have an integer number "
                   "of words of n length (once spaces have been striped):")
        htext = "01100101 0110111 0110110 0110011 0110111 0101101 0110011 " \
                "0000001"
        ui.message("“Numbers” text used as input: {}".format(htext))
        try:
            ui.message("The decypherd data is: {}"
                       "".format(gray.decypher(htext, codec="ascii",
                                               length=8)))
        except Exception as e:
            ui.message(str(e), level=ui.ERROR)
        ui.message("")

        ui.get_choice("", [("", "Go back to $menu", "")], oneline=True)
Example #2
0
    def demo(self, ui):
        ui.message("===== Demo Mode =====")
        ui.message("Running a small demo/testing!")

        ui.message("--- Encoding ---")
        text = "Hello World!"
        ui.message("Data to cypher: {}\n".format(text))
        ui.message("Gray cyphered data (3, 4, 5 and 8 bits words, utf-8):"
                   "\n    {}".format("\n    ".join(gray.cypher(text,
                                                   lengths=(3, 4, 5, 8)))))
        ui.message("+ Note how padding bits are added to get an integer "
                   "number of words of n bits.")
        ui.message("")

        ui.message("--- Decoding ---")
        ui.message("+ You must specify the word length used at encode time "
                   "(which must be a multiple of number of binary bits given "
                   "to decypher).")
        htext = "0110010101101010010100011101101101000010010110101011000" \
                "001100100110010110100000101000001001100101100000010110111000"
        ui.message("“Numbers” utf-8 text used as input (5 bits words): {}"
                   "".format(htext))
        ui.message("The decypherd data is: {}"
                   "".format(gray.decypher(htext, codecs="utf-8", lengths=5)))
        ui.message("")

        ui.message("+ The input text to decypher may have space-separated "
                   "bytes:")
        htext = "10010011 11000000 11110101 01001100 01010001 01001010 " \
                "00110000 01010001 00110000 01100011 01010111 01011010 " \
                "01011010 00110000 01101010 01010001 01010011 01001010 " \
                "10010011 11000000 11010101 00110000 01001011 01010111 " \
                "01001010 01010111 01010001 01010010 01011100 01010111 " \
                "01001011 00111001"
        ui.message("“binary” utf-8, 8 bits words, text used as input: {}"
                   "".format(htext))
        ui.message("The decypherd data is: {}"
                   "".format(gray.decypher(htext, codecs="utf-8", lengths=8)))
        ui.message("")

        ui.message("--- Won’t work ---")
        ui.message("+ The input text to decypher must contain only valid "
                   "binary digits (and optionally spaces):")
        htext = "011001010111211101101100015000110110111101101101011a" \
                "001010010001"
        ui.message("“Numbers” text used as binary input: {}".format(htext))
        try:
            ui.message("The decypherd data is: {}"
                       "".format(gray.decypher(htext, codecs="ascii",
                                               lengths=8)))
        except Exception as e:
            ui.message(str(e), level=ui.ERROR)
        ui.message("")

        ui.message("+ The input text to decypher must have an integer number "
                   "of words of n length (once spaces have been striped):")
        htext = "01100101 0110111 0110110 0110011 0110111 0101101 0110011 " \
                "0000001"
        ui.message("“Numbers” text used as input: {}".format(htext))
        try:
            ui.message("The decypherd data is: {}"
                       "".format(gray.decypher(htext, codecs="ascii",
                                               lengths=8)))
        except Exception as e:
            ui.message(str(e), level=ui.ERROR)
        ui.message("")

        ui.get_choice("", [("", "Go back to $menu", "")], oneline=True)
Example #3
0
    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 numbers")
                if txt is None:
                    break  # Go back to main Cypher menu.

                try:
                    # Get codec to use.
                    options = [(gray.DEFAULT, "$utf-8", ""),
                               (gray.ASCII, "*ascii", ""),
                               (None, "or specify another *codec", "")]
                    codec = ui.get_choice("Do you want to use", options,
                                          oneline=True)
                    if codec is None:
                        codec = ui.get_data("Type the codec you want to use "
                                            "(e.g. 'latin-9'): ")

                    # Get word length(s).
                    options = [(3, "*three", ""),
                               (4, "*four", ""),
                               (5, "f$ive", ""),
                               (8, "*height (byte)", ""),
                               (None, "and/or *other word length(s)", "")]
                    lengths = ui.get_choice("Do you want to use", options,
                                            oneline=True, multichoices=",")
                    if None in lengths:
                        lengths.remove(None)
                        lengths += ui.get_data("Type the lengths you want to "
                                               "use (e.g. '7,12,6'): ",
                                               sub_type=ui.INT_LIST)

                    txt = gray.cypher(txt, codec, lengths)
                    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 "
                                         "binary, 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:
                txt = "\n    " + "\n    ".join("{} bits: {}".format(ln, t)
                                               for ln, t in zip(lengths, txt))
                ui.text_output("Data successfully converted", txt,
                               "Gray-encoded form(s) of data")

            options = [("redo", "*cypher another data", ""),
                       ("quit", "or go back to *menu", "")]
            answ = ui.get_choice("Do you want to", options, oneline=True)
            if answ in {None, "quit"}:
                return
Example #4
0
    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 numbers")
                if txt is None:
                    break  # Go back to main Cypher menu.

                try:
                    # Get codec to use.
                    options = [(gray.DEFAULT, "$utf-8", ""),
                               (gray.ASCII, "*ascii", ""),
                               (None, "or specify another *codec", "")]
                    codec = ui.get_choice("Do you want to use", options,
                                          oneline=True)
                    if codec is None:
                        codec = ui.get_data("Type the codec you want to use "
                                            "(e.g. 'latin-9'): ")

                    # Get word length(s).
                    options = [(3, "*three", ""),
                               (4, "*four", ""),
                               (5, "f$ive", ""),
                               (8, "*height (byte)", ""),
                               (None, "and/or *other word length(s)", "")]
                    lengths = ui.get_choice("Do you want to use", options,
                                            oneline=True, multichoices=",")
                    if None in lengths:
                        lengths.remove(None)
                        lengths += ui.get_data("Type the lengths you want to "
                                               "use (e.g. '7,12,6'): ",
                                               sub_type=ui.INT_LIST)

                    txt = gray.cypher(txt, codec, lengths)
                    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 "
                                         "binary, 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:
                txt = "\n    " + "\n    ".join("{} bits: {}".format(ln, t)
                                               for ln, t in zip(lengths, txt))
                ui.text_output("Data successfully converted", txt,
                               "Gray-encoded form(s) of data")

            options = [("redo", "*cypher another data", ""),
                       ("quit", "or go back to *menu", "")]
            answ = ui.get_choice("Do you want to", options, oneline=True)
            if answ in {None, "quit"}:
                return