Exemplo n.º 1
0
    def test_pad_key(self):
        original_s = get_random_str(9).encode()
        padded_s = self.cipher._pad_key(original_s, alignment=16)
        expected_s = original_s + self.cipher.PADDING_CHAR * (16 -
                                                              len(original_s))
        self.assertEqual(padded_s, expected_s)
        self.assertEqual(len(padded_s), 16)

        original_s = get_random_str(9).encode()
        padded_s = self.cipher._pad_key(original_s, alignment=32)
        expected_s = original_s + self.cipher.PADDING_CHAR * (32 -
                                                              len(original_s))
        self.assertEqual(padded_s, expected_s)
        self.assertEqual(len(padded_s), 32)

        original_s = get_random_str(18).encode()
        padded_s = self.cipher._pad_key(original_s, alignment=16)
        expected_s = original_s + self.cipher.PADDING_CHAR * (32 -
                                                              len(original_s))
        self.assertEqual(padded_s, expected_s)
        self.assertEqual(len(padded_s), 32)

        original_s = get_random_str(18).encode()
        padded_s = self.cipher._pad_key(original_s, alignment=24)
        expected_s = original_s + self.cipher.PADDING_CHAR * (24 -
                                                              len(original_s))
        self.assertEqual(padded_s, expected_s)
        self.assertEqual(len(padded_s), 24)

        with self.assertRaises(ValueError):
            original_s = get_random_str(30).encode()
            padded_s = self.cipher._pad_key(original_s, alignment=24)
Exemplo n.º 2
0
    def test_long_key(self):
        with self.assertRaises(ValueError):
            key = get_random_str(33).encode()
            cipher = AES_Cipher(key, key_align=32)

        with self.assertRaises(ValueError):
            key = get_random_str(33).encode()
            cipher = AES_Cipher(key, key_align=24)

        with self.assertRaises(ValueError):
            key = get_random_str(30).encode()
            cipher = AES_Cipher(key, key_align=24)

        with self.assertRaises(ValueError):
            key = get_random_str(33).encode()
            cipher = AES_Cipher(key, key_align=16)
Exemplo n.º 3
0
    def test_init(self):
        key = get_random_str(20).encode()
        cipher = AES_Cipher(key, key_align=32)
        self.assertTrue(len(cipher.key) == 32)

        key = get_random_str(20).encode()
        cipher = AES_Cipher(key, key_align=16)
        self.assertTrue(len(cipher.key) == 32)

        key = get_random_str(20).encode()
        cipher = AES_Cipher(key, key_align=24)
        self.assertTrue(len(cipher.key) == 24)

        key = get_random_str(30).encode()
        cipher = AES_Cipher(key, key_align=16)
        self.assertTrue(len(cipher.key) == 32)

        key = get_random_str(30).encode()
        cipher = AES_Cipher(key, key_align=32)
        self.assertTrue(len(cipher.key) == 32)
Exemplo n.º 4
0
 def setUp(self):
     self.key = get_random_str(20).encode()
     self.cipher = AES_Cipher(self.key)
Exemplo n.º 5
0
 def test_RSA_ascii(self):
     text = get_random_str(128)
     encrypted_text = self.cipher.encrypt(text.encode())
     decrypted_text = self.decipher.decrypt(encrypted_text)
     self.assertEqual(text, decrypted_text.decode())