cryptosploit_modules.encodings.morse

View Source
  0from os.path import isfile
  1
  2from cryptosploit.cprint import Printer
  3from cryptosploit.exceptions import ArgError
  4from cryptosploit_modules import BaseModule
  5
  6MORSE_CODE_DICT = {
  7    "A": ".-",
  8    "B": "-...",
  9    "C": "-.-.",
 10    "D": "-..",
 11    "E": ".",
 12    "F": "..-.",
 13    "G": "--.",
 14    "H": "....",
 15    "I": "..",
 16    "J": ".---",
 17    "K": "-.-",
 18    "L": ".-..",
 19    "M": "--",
 20    "N": "-.",
 21    "O": "---",
 22    "P": ".--.",
 23    "Q": "--.-",
 24    "R": ".-.",
 25    "S": "...",
 26    "T": "-",
 27    "U": "..-",
 28    "V": "...-",
 29    "W": ".--",
 30    "X": "-..-",
 31    "Y": "-.--",
 32    "Z": "--..",
 33    "1": ".----",
 34    "2": "..---",
 35    "3": "...--",
 36    "4": "....-",
 37    "5": ".....",
 38    "6": "-....",
 39    "7": "--...",
 40    "8": "---..",
 41    "9": "----.",
 42    "0": "-----",
 43    ".": ".-.-.-",
 44    ",": "--..--",
 45    ":": "---...",
 46    "?": "..--..",
 47    "'": ".----.",
 48    "-": "-....-",
 49    "/": "-..-.",
 50    "@": ".--.-.",
 51    "=": "-...-",
 52    "(": "-.--.",
 53    ")": "-.--.-",
 54}
 55
 56MORSE_DECODE_DICT = {value: key for key, value in MORSE_CODE_DICT.items()}
 57
 58
 59class Morse(BaseModule):
 60    def __init__(self):
 61        super().__init__()
 62        self.env.check_var = self.check_var
 63
 64    @staticmethod
 65    def check_var(name, value):
 66        """Must return isvalid_variable: bool, error_msg: str"""
 67        match name:
 68            case "mode":
 69                if value in ("encode", "decode"):
 70                    return True, ""
 71                return False, "May be encode/decode"
 72            case _:
 73                return True, ""
 74
 75    def encode_command(self, message, letter_delimiter, word_delimiter):
 76        message = message.upper()
 77        encoded = ""
 78        for letter in message:
 79            if letter not in (" ", "\n"):
 80                if letter in MORSE_CODE_DICT:
 81                    encoded += MORSE_CODE_DICT[letter] + letter_delimiter
 82                else:
 83                    raise ArgError(f"Letter '{letter}' can't be parsed")
 84            else:
 85                encoded += word_delimiter
 86        Printer.positive("Encoded string:\n" + encoded)
 87
 88    def decode_command(self, message, letter_delimiter, word_delimiter):
 89        words = filter(lambda a: a, message.split(word_delimiter))
 90        decoded = ""
 91        for word in words:
 92            morse_codes = filter(lambda a: a, word.split(letter_delimiter))
 93            for morse_code in morse_codes:
 94                if morse_code in MORSE_DECODE_DICT:
 95                    decoded += MORSE_DECODE_DICT[morse_code]
 96                else:
 97                    raise ArgError(f"Morse code '{morse_code}' can't be parsed")
 98            decoded += " "
 99        Printer.positive("Decoded string:\n" + decoded)
100
101    def run(self):
102        inp = self.env.get_var("input").value
103        if isfile(inp):
104            with open(inp) as f:
105                inp = f.read()
106        letter_delimiter = self.env.get_var("letter_delimiter").value
107        word_delimiter = self.env.get_var("word_delimiter").value
108        if inp and letter_delimiter and word_delimiter:
109            func = getattr(self, self.env.get_var("mode").value + "_command")
110            return func(inp, letter_delimiter, word_delimiter)
111        raise ArgError("All variables must be set")
112
113
114module = Morse
View Source
 60class Morse(BaseModule):
 61    def __init__(self):
 62        super().__init__()
 63        self.env.check_var = self.check_var
 64
 65    @staticmethod
 66    def check_var(name, value):
 67        """Must return isvalid_variable: bool, error_msg: str"""
 68        match name:
 69            case "mode":
 70                if value in ("encode", "decode"):
 71                    return True, ""
 72                return False, "May be encode/decode"
 73            case _:
 74                return True, ""
 75
 76    def encode_command(self, message, letter_delimiter, word_delimiter):
 77        message = message.upper()
 78        encoded = ""
 79        for letter in message:
 80            if letter not in (" ", "\n"):
 81                if letter in MORSE_CODE_DICT:
 82                    encoded += MORSE_CODE_DICT[letter] + letter_delimiter
 83                else:
 84                    raise ArgError(f"Letter '{letter}' can't be parsed")
 85            else:
 86                encoded += word_delimiter
 87        Printer.positive("Encoded string:\n" + encoded)
 88
 89    def decode_command(self, message, letter_delimiter, word_delimiter):
 90        words = filter(lambda a: a, message.split(word_delimiter))
 91        decoded = ""
 92        for word in words:
 93            morse_codes = filter(lambda a: a, word.split(letter_delimiter))
 94            for morse_code in morse_codes:
 95                if morse_code in MORSE_DECODE_DICT:
 96                    decoded += MORSE_DECODE_DICT[morse_code]
 97                else:
 98                    raise ArgError(f"Morse code '{morse_code}' can't be parsed")
 99            decoded += " "
100        Printer.positive("Decoded string:\n" + decoded)
101
102    def run(self):
103        inp = self.env.get_var("input").value
104        if isfile(inp):
105            with open(inp) as f:
106                inp = f.read()
107        letter_delimiter = self.env.get_var("letter_delimiter").value
108        word_delimiter = self.env.get_var("word_delimiter").value
109        if inp and letter_delimiter and word_delimiter:
110            func = getattr(self, self.env.get_var("mode").value + "_command")
111            return func(inp, letter_delimiter, word_delimiter)
112        raise ArgError("All variables must be set")
#   Morse()
View Source
61    def __init__(self):
62        super().__init__()
63        self.env.check_var = self.check_var
#  
@staticmethod
def check_var(name, value):
View Source
65    @staticmethod
66    def check_var(name, value):
67        """Must return isvalid_variable: bool, error_msg: str"""
68        match name:
69            case "mode":
70                if value in ("encode", "decode"):
71                    return True, ""
72                return False, "May be encode/decode"
73            case _:
74                return True, ""

Must return isvalid_variable: bool, error_msg: str

#   def encode_command(self, message, letter_delimiter, word_delimiter):
View Source
76    def encode_command(self, message, letter_delimiter, word_delimiter):
77        message = message.upper()
78        encoded = ""
79        for letter in message:
80            if letter not in (" ", "\n"):
81                if letter in MORSE_CODE_DICT:
82                    encoded += MORSE_CODE_DICT[letter] + letter_delimiter
83                else:
84                    raise ArgError(f"Letter '{letter}' can't be parsed")
85            else:
86                encoded += word_delimiter
87        Printer.positive("Encoded string:\n" + encoded)
#   def decode_command(self, message, letter_delimiter, word_delimiter):
View Source
 89    def decode_command(self, message, letter_delimiter, word_delimiter):
 90        words = filter(lambda a: a, message.split(word_delimiter))
 91        decoded = ""
 92        for word in words:
 93            morse_codes = filter(lambda a: a, word.split(letter_delimiter))
 94            for morse_code in morse_codes:
 95                if morse_code in MORSE_DECODE_DICT:
 96                    decoded += MORSE_DECODE_DICT[morse_code]
 97                else:
 98                    raise ArgError(f"Morse code '{morse_code}' can't be parsed")
 99            decoded += " "
100        Printer.positive("Decoded string:\n" + decoded)
#   def run(self):
View Source
102    def run(self):
103        inp = self.env.get_var("input").value
104        if isfile(inp):
105            with open(inp) as f:
106                inp = f.read()
107        letter_delimiter = self.env.get_var("letter_delimiter").value
108        word_delimiter = self.env.get_var("word_delimiter").value
109        if inp and letter_delimiter and word_delimiter:
110            func = getattr(self, self.env.get_var("mode").value + "_command")
111            return func(inp, letter_delimiter, word_delimiter)
112        raise ArgError("All variables must be set")

Required to be overridden in the child class. Function called by the user

View Source
 60class Morse(BaseModule):
 61    def __init__(self):
 62        super().__init__()
 63        self.env.check_var = self.check_var
 64
 65    @staticmethod
 66    def check_var(name, value):
 67        """Must return isvalid_variable: bool, error_msg: str"""
 68        match name:
 69            case "mode":
 70                if value in ("encode", "decode"):
 71                    return True, ""
 72                return False, "May be encode/decode"
 73            case _:
 74                return True, ""
 75
 76    def encode_command(self, message, letter_delimiter, word_delimiter):
 77        message = message.upper()
 78        encoded = ""
 79        for letter in message:
 80            if letter not in (" ", "\n"):
 81                if letter in MORSE_CODE_DICT:
 82                    encoded += MORSE_CODE_DICT[letter] + letter_delimiter
 83                else:
 84                    raise ArgError(f"Letter '{letter}' can't be parsed")
 85            else:
 86                encoded += word_delimiter
 87        Printer.positive("Encoded string:\n" + encoded)
 88
 89    def decode_command(self, message, letter_delimiter, word_delimiter):
 90        words = filter(lambda a: a, message.split(word_delimiter))
 91        decoded = ""
 92        for word in words:
 93            morse_codes = filter(lambda a: a, word.split(letter_delimiter))
 94            for morse_code in morse_codes:
 95                if morse_code in MORSE_DECODE_DICT:
 96                    decoded += MORSE_DECODE_DICT[morse_code]
 97                else:
 98                    raise ArgError(f"Morse code '{morse_code}' can't be parsed")
 99            decoded += " "
100        Printer.positive("Decoded string:\n" + decoded)
101
102    def run(self):
103        inp = self.env.get_var("input").value
104        if isfile(inp):
105            with open(inp) as f:
106                inp = f.read()
107        letter_delimiter = self.env.get_var("letter_delimiter").value
108        word_delimiter = self.env.get_var("word_delimiter").value
109        if inp and letter_delimiter and word_delimiter:
110            func = getattr(self, self.env.get_var("mode").value + "_command")
111            return func(inp, letter_delimiter, word_delimiter)
112        raise ArgError("All variables must be set")
#   module()
View Source
61    def __init__(self):
62        super().__init__()
63        self.env.check_var = self.check_var
#  
@staticmethod
def check_var(name, value):
View Source
65    @staticmethod
66    def check_var(name, value):
67        """Must return isvalid_variable: bool, error_msg: str"""
68        match name:
69            case "mode":
70                if value in ("encode", "decode"):
71                    return True, ""
72                return False, "May be encode/decode"
73            case _:
74                return True, ""

Must return isvalid_variable: bool, error_msg: str

#   def encode_command(self, message, letter_delimiter, word_delimiter):
View Source
76    def encode_command(self, message, letter_delimiter, word_delimiter):
77        message = message.upper()
78        encoded = ""
79        for letter in message:
80            if letter not in (" ", "\n"):
81                if letter in MORSE_CODE_DICT:
82                    encoded += MORSE_CODE_DICT[letter] + letter_delimiter
83                else:
84                    raise ArgError(f"Letter '{letter}' can't be parsed")
85            else:
86                encoded += word_delimiter
87        Printer.positive("Encoded string:\n" + encoded)
#   def decode_command(self, message, letter_delimiter, word_delimiter):
View Source
 89    def decode_command(self, message, letter_delimiter, word_delimiter):
 90        words = filter(lambda a: a, message.split(word_delimiter))
 91        decoded = ""
 92        for word in words:
 93            morse_codes = filter(lambda a: a, word.split(letter_delimiter))
 94            for morse_code in morse_codes:
 95                if morse_code in MORSE_DECODE_DICT:
 96                    decoded += MORSE_DECODE_DICT[morse_code]
 97                else:
 98                    raise ArgError(f"Morse code '{morse_code}' can't be parsed")
 99            decoded += " "
100        Printer.positive("Decoded string:\n" + decoded)
#   def run(self):
View Source
102    def run(self):
103        inp = self.env.get_var("input").value
104        if isfile(inp):
105            with open(inp) as f:
106                inp = f.read()
107        letter_delimiter = self.env.get_var("letter_delimiter").value
108        word_delimiter = self.env.get_var("word_delimiter").value
109        if inp and letter_delimiter and word_delimiter:
110            func = getattr(self, self.env.get_var("mode").value + "_command")
111            return func(inp, letter_delimiter, word_delimiter)
112        raise ArgError("All variables must be set")

Required to be overridden in the child class. Function called by the user