コード例 #1
0
ファイル: vigenere.py プロジェクト: ridho9/crypto-cli
    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)
コード例 #2
0
ファイル: vigenere.py プロジェクト: ridho9/crypto-cli
    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)
コード例 #3
0
 def __init__(self, key: list):
     self.key = [[-1 for _ in range(5)] for _ in range(5)]
     self.pos = {}
     for i in range(5):
         for j in range(5):
             c = lalpha_ord(key[i][j])
             self.pos[c] = (i, j)
             self.key[i][j] = c
コード例 #4
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)
コード例 #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])

                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)
コード例 #6
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)