Exemplo n.º 1
0
    def decode(self, chiper: bytes):
        result = []
        chiper = [x for x in chiper if is_int_alpha(x)]
        chiper_len = len(chiper)
        a_idx = 0

        while a_idx < chiper_len:
            if is_int_alpha(chiper[a_idx]):
                a_idx, b_idx, a, b = self.get_letter_pair(
                    a_idx, chiper, chiper_len)
                a_new, b_new = self.process_pair(a, b, decode=True)

                result.append(a_new + ord("a"))
                result.append(b_new + ord("a"))
            else:
                result.append(chiper[a_idx])
                a_idx += 1

        return bytes(result)
Exemplo n.º 2
0
    def encode(self, plain: bytes):
        result = []
        plain = [x for x in plain if is_int_alpha(x)]
        plain_len = len(plain)
        a_idx = 0

        while a_idx < plain_len:
            if is_int_alpha(plain[a_idx]):
                a_idx, b_idx, a, b = self.get_letter_pair(
                    a_idx, plain, plain_len)
                a_new, b_new = self.process_pair(a, b)

                result.append(a_new + ord("a"))
                result.append(b_new + ord("a"))

            else:
                result.append(plain[a_idx])
                a_idx += 1

        return bytes(result)
Exemplo n.º 3
0
    def prepare_input(self, input: bytes):
        input = input.strip().lower()
        result = [x for x in input if is_int_alpha(x)]

        if len(result) % self.k != 0:
            # pad message
            padlen = self.k - (len(result) % self.k)
            # print(f"{len(result)=} {len(result) % self.k=} {padlen=}")
            for x in range(padlen):
                c = (x % 26) + ord("a")
                result.append(c)

        return bytes(result)
Exemplo n.º 4
0
    def encode(self, plain: bytes) -> bytes:
        result = []
        key_idx = 0
        key_len = len(self.key)

        for c in plain:
            if is_int_alpha(c):
                c_ord = lalpha_ord(c)
                k_ord = lalpha_ord(self.key[key_idx])

                res_ord = (c_ord + k_ord) % 26 + ord("a")
                result.append(res_ord)
                key_idx = (key_idx + 1) % key_len
            else:
                result.append(c)

        return bytes(result)
Exemplo n.º 5
0
    def decode(self, chiper: bytes) -> bytes:
        result = []
        key_idx = 0
        key_len = len(self.key)

        for c in chiper:
            if is_int_alpha(c):
                c_ord = lalpha_ord(c)
                k_ord = lalpha_ord(self.key[key_idx])

                res_ord = (c_ord - k_ord + 26) % 26 + ord("a")
                result.append(res_ord)
                key_idx = (key_idx + 1) % key_len
            else:
                result.append(c)

        return bytes(result)
Exemplo n.º 6
0
    def decode(self, chiper: bytes) -> bytes:
        result = []
        key_idx = 0
        key_len = len(self.key)

        for c in chiper:
            if is_int_alpha(c):
                c_ord = lalpha_ord(c)

                if key_idx >= key_len:
                    k_ord = result[key_idx - key_len]
                else:
                    k_ord = lalpha_ord(self.key[key_idx])

                res_ord = (c_ord - k_ord) % 26 + ord("a")
                result.append(res_ord)
                key_idx = key_idx + 1
            else:
                result.append(c)

        return bytes(result)
Exemplo n.º 7
0
    def decode(self, chiper: bytes) -> bytes:
        result = []
        key_idx = 0
        key_len = len(self.key)

        for c in chiper:
            if is_int_alpha(c):
                c_ord = lalpha_ord(c)
                k_ord = lalpha_ord(self.key[key_idx])

                for row_num in range(len(self.square)):
                    row = self.square[row_num]
                    res_ord = row[k_ord] - ord("a")
                    if res_ord == c_ord:
                        break
                result.append(row_num + ord("a"))
                key_idx = (key_idx + 1) % key_len
            else:
                result.append(c)

        return bytes(result)
Exemplo n.º 8
0
    def get_letter_pair(self, a_idx, text, text_len):
        a = text[a_idx]
        b_idx = a_idx + 1
        while True:
            if b_idx >= text_len:
                b = ord("x")
                break

            b = text[b_idx]
            if is_int_alpha(b):
                if a == b:
                    b_idx = a_idx
                    b = ord("x")
                break
            else:
                b_idx += 1
                continue

        a_idx = b_idx + 1

        return a_idx, b_idx, lalpha_ord(a), lalpha_ord(b)