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
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