def test_initial_permutation(self):
     text = 'Exampl'
     text = divide_into_chunks(text)[0]
     bits = text_to_bits(text)
     bits_after = initial_permutation(bits)
     self.assertEqual(bits[58 - 1], bits_after[0])
     self.assertEqual(bits[26 - 1], bits_after[4])
 def test_final_permutation(self):
     text = 'Exampl'
     text = divide_into_chunks(text)[0]
     bits = text_to_bits(text)
     bits_after = final_permutation(bits)
     self.assertEqual(bits[40 - 1], bits_after[0])
     self.assertEqual(bits[7 - 1], bits_after[9])
 def test_expansion_function(self):
     text = 'Exampl'
     text = divide_into_chunks(text)[0]
     bits = text_to_bits(text)[:32]
     bits_after = expansion_function(bits)
     self.assertEqual(bits[32 - 1], bits_after[0])
     self.assertEqual(bits[5 - 1], bits_after[5])
     self.assertEqual(bits[5 - 1], bits_after[7])
Ejemplo n.º 4
0
    def encrypt(self, message: str) -> list:
        msgs = divide_into_chunks(message)
        msgs_bits = [text_to_bits(m) for m in msgs]
        total_output = []

        for m in msgs_bits:
            m = initial_permutation(m)
            L, R = divide_message(m)

            for r in range(1, 17):
                k_i = self.subkeys[r - 1]
                old_L = L
                L = R
                R = xor(old_L, f_function(R, k_i))

            output = R + L
            output = final_permutation(output)
            total_output.append(output)

        return total_output
 def test_divide(self):
     text = 'This is an example'
     splitted = divide_into_chunks(text)
     self.assertEqual(len(splitted), 3)