class TestCbc(unittest.TestCase): """ Tests AES-128 in CBC mode. """ def setUp(self): self.aes = AES(b'\x00' * 16) self.iv = b'\x01' * 16 self.message = b'my message' def test_single_block(self): """ Should be able to encrypt and decrypt single block messages. """ ciphertext = self.aes.encrypt_cbc(self.message, self.iv) self.assertEqual(self.aes.decrypt_cbc( ciphertext, self.iv), self.message) # Since len(message) < block size, padding won't create a new block. self.assertEqual(len(ciphertext), 16) def test_wrong_iv(self): """ CBC mode should verify the IVs are of correct length.""" with self.assertRaises(AssertionError): self.aes.encrypt_cbc(self.message, b'short iv') with self.assertRaises(AssertionError): self.aes.encrypt_cbc(self.message, b'long iv' * 16) with self.assertRaises(AssertionError): self.aes.decrypt_cbc(self.message, b'short iv') with self.assertRaises(AssertionError): self.aes.decrypt_cbc(self.message, b'long iv' * 16) def test_different_iv(self): """ Different IVs should generate different ciphertexts. """ iv2 = b'\x02' * 16 ciphertext1 = self.aes.encrypt_cbc(self.message, self.iv) ciphertext2 = self.aes.encrypt_cbc(self.message, iv2) self.assertNotEqual(ciphertext1, ciphertext2) plaintext1 = self.aes.decrypt_cbc(ciphertext1, self.iv) plaintext2 = self.aes.decrypt_cbc(ciphertext2, iv2) self.assertEqual(plaintext1, plaintext2) self.assertEqual(plaintext1, self.message) def test_whole_block_padding(self): """ When len(message) == block size, padding will add a block. """ block_message = b'M' * 16 ciphertext = self.aes.encrypt_cbc(block_message, self.iv) self.assertEqual(len(ciphertext), 32) self.assertEqual(self.aes.decrypt_cbc( ciphertext, self.iv), block_message) def test_long_message(self): """ CBC should allow for messages longer than a single block. """ long_message = b'M' * 100 ciphertext = self.aes.encrypt_cbc(long_message, self.iv) self.assertEqual(self.aes.decrypt_cbc( ciphertext, self.iv), long_message)
class TestCbc(unittest.TestCase): """ Tests AES-128 in CBC mode. """ def setUp(self): self.aes = AES(b'\00' * 16) self.iv = b'\01' * 16 self.message = b'my message' def test_single_block(self): """ Should be able to encrypt and decrypt single block messages. """ ciphertext = self.aes.encrypt_cbc(self.message, self.iv) self.assertEqual(self.aes.decrypt_cbc(ciphertext, self.iv), self.message) # Since len(message) < block size, padding won't create a new block. self.assertEqual(len(ciphertext), 16) def test_wrong_iv(self): """ CBC mode should verify the IVs are of correct length.""" with self.assertRaises(AssertionError): self.aes.encrypt_cbc(self.message, b'short iv') with self.assertRaises(AssertionError): self.aes.encrypt_cbc(self.message, b'long iv' * 16) with self.assertRaises(AssertionError): self.aes.decrypt_cbc(self.message, b'short iv') with self.assertRaises(AssertionError): self.aes.decrypt_cbc(self.message, b'long iv' * 16) def test_different_iv(self): """ Different IVs should generate different ciphertexts. """ iv2 = b'\02' * 16 ciphertext1 = self.aes.encrypt_cbc(self.message, self.iv) ciphertext2 = self.aes.encrypt_cbc(self.message, iv2) self.assertNotEqual(ciphertext1, ciphertext2) plaintext1 = self.aes.decrypt_cbc(ciphertext1, self.iv) plaintext2 = self.aes.decrypt_cbc(ciphertext2, iv2) self.assertEqual(plaintext1, plaintext2) self.assertEqual(plaintext1, self.message) def test_whole_block_padding(self): """ When len(message) == block size, padding will add a block. """ block_message = b'M' * 16 ciphertext = self.aes.encrypt_cbc(block_message, self.iv) self.assertEqual(len(ciphertext), 32) self.assertEqual(self.aes.decrypt_cbc(ciphertext, self.iv), block_message) def test_long_message(self): """ CBC should allow for messages longer than a single block. """ long_message = b'M' * 100 ciphertext = self.aes.encrypt_cbc(long_message, self.iv) self.assertEqual(self.aes.decrypt_cbc(ciphertext, self.iv), long_message)
''' Created on Apr 21, 2020 @author: KSHITIZ CHATURVEDI ''' from aes import AES from aes import unpad if __name__ == '__main__': text = "Two One Nine TwoTwo One Nine abcd" #input("Enter plain text") key = "Thats My Kung Fu" #input("Enter key") iv = "given initializa" obj = AES(key) cipher = obj.encrypt_cbc(text, iv) print(cipher) msg = obj.decrypt_cbc(cipher, iv) print(msg) msgstr = "" for i in msg: for j in i: msgstr += chr(j) print(unpad(msgstr))