Beispiel #1
0
    def encrypt_gamming(self, data, sync):
        blocks = self.__split_into_sub_lists(data, 16)
        encoded = []
        temp = sync
        for block in blocks:
            enc_temp = self.__encrypt(temp)
            enc_sliced = enc_temp[:len(block)]
            ans = Converter.to_string(
                Operation.bit_xor(Converter.to_bin(block),
                                  Converter.to_bin(enc_sliced)))
            encoded.append(ans)
            temp = ans

        encoded = ''.join(encoded)
        return encoded
Beispiel #2
0
    def decrypt_clutch_blocks(self, data, sync):
        self.check_text_length(data)
        decoded = []
        blocks = self.__split_into_sub_lists(data, 16)
        temp = self.__encrypt(sync)
        for block in blocks:
            bit_temp = Converter.to_bin(temp)
            ans = Converter.to_string(
                Operation.bit_xor(Converter.to_bin(self.__decrypt(block)),
                                  bit_temp))
            decoded.append(ans)
            temp = block

        decoded = ''.join(decoded)
        return decoded
Beispiel #3
0
    def encrypt_clutch_blocks(self, data, sync):
        self.check_text_length(data)
        encoded = []
        blocks = self.__split_into_sub_lists(data, 16)
        encoded_sync = self.__encrypt(sync)
        for block in blocks:
            res = Converter.to_string(
                Operation.bit_xor(Converter.to_bin(encoded_sync),
                                  Converter.to_bin(block)))
            encoded_part = self.__encrypt(res)
            encoded_sync = encoded_part
            encoded.append(encoded_part)

        encoded = ''.join(encoded)
        return encoded
Beispiel #4
0
    def __decrypt(self, text):
        bits_text = Converter.to_bin(text)
        words = self.__split_into_sub_lists(bits_text, 32)
        a = words[0]
        b = words[1]
        c = words[2]
        d = words[3]

        for i in range(8, 0, -1):
            # 1
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 1])
            temp = Operation.plus_mod_32(a, sub_key)
            temp = self.G_operation(temp, 5)
            b = Operation.bit_xor(b, temp)
            # 2
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 1 - 1])
            temp = Operation.plus_mod_32(d, sub_key)
            temp = self.G_operation(temp, 21)
            c = Operation.bit_xor(c, temp)
            # 3
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 2 - 1])
            temp = Operation.plus_mod_32(b, sub_key)
            temp = self.G_operation(temp, 13)
            a = Operation.minus_mod_32(a, temp)
            # 4
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 3 - 1])
            temp = Operation.plus_mod_32(b, c)
            temp = Operation.plus_mod_32(temp, sub_key)
            temp = self.G_operation(temp, 21)
            value_i = i % (2**32)
            value_i = Converter.int_to_bits(value_i, 32)
            e = Operation.bit_xor(temp, value_i)
            # 5
            b = Operation.plus_mod_32(b, e)
            # 6
            c = Operation.minus_mod_32(c, e)
            # 7
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 4 - 1])
            temp = Operation.plus_mod_32(c, sub_key)
            temp = self.G_operation(temp, 13)
            d = Operation.plus_mod_32(d, temp)
            # 8
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 5 - 1])
            temp = Operation.plus_mod_32(a, sub_key)
            temp = self.G_operation(temp, 21)
            b = Operation.bit_xor(b, temp)
            # 9
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 6 - 1])
            temp = Operation.plus_mod_32(d, sub_key)
            temp = self.G_operation(temp, 5)
            c = Operation.bit_xor(c, temp)
            # 10
            a, b = b, a
            # 11
            c, d = d, c
            # 12
            a, d = d, a

        decoded = c + a + d + b
        decoded = Converter.to_string(decoded)
        return decoded
Beispiel #5
0
    def __encrypt(self, text):
        bits_text = Converter.to_bin(text)  # 16 байт (блок) -> 128 бит
        words = self.__split_into_sub_lists(bits_text, 32)
        a = words[0]
        b = words[1]
        c = words[2]
        d = words[3]

        for i in range(1, 9):
            # 1
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 6 - 1])
            value = Operation.plus_mod_32(a, sub_key)
            value = self.G_operation(value, 5)
            b = Operation.bit_xor(b, value)
            # 2
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 5 - 1])
            value = Operation.plus_mod_32(d, sub_key)
            value = self.G_operation(value, 21)
            c = Operation.bit_xor(c, value)
            # 3
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 4 - 1])
            value = Operation.plus_mod_32(b, sub_key)
            value = self.G_operation(value, 13)
            a = Operation.minus_mod_32(a, value)
            # 4
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 3 - 1])
            value = Operation.plus_mod_32(b, c)
            value = Operation.plus_mod_32(value, sub_key)
            value = self.G_operation(value, 21)
            value_i = i % (2**32)
            value_i = Converter.int_to_bits(value_i, 32)
            while len(value_i) < 32:
                value_i.insert(0, 0)
            e = Operation.bit_xor(value, value_i)
            # 5
            b = Operation.plus_mod_32(b, e)
            # 6
            c = Operation.minus_mod_32(c, e)
            # 7
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 2 - 1])
            value = Operation.plus_mod_32(c, sub_key)
            value = self.G_operation(value, 13)
            d = Operation.plus_mod_32(d, value)
            # 8
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 1 - 1])
            value = Operation.plus_mod_32(a, sub_key)
            value = self.G_operation(value, 21)
            b = Operation.bit_xor(b, value)
            # 9
            sub_key = copy.deepcopy(self.sub_keys[7 * i - 1])
            value = Operation.plus_mod_32(d, sub_key)
            value = self.G_operation(value, 5)
            c = Operation.bit_xor(c, value)
            # 10
            a, b = b, a
            # 11
            c, d = d, c
            # 12
            b, c = c, b

        encrypted = b + d + a + c
        encrypted = Converter.to_string(encrypted)
        return encrypted