Пример #1
0
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)
Пример #2
0
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)
Пример #3
0
'''
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))
                "The length of the initialization vector needs to be 16, please enter 16 bytes long iv\n"
            )
        obj = AES(key)
        cipher = [[
            82, 214, 73, 255, 189, 148, 31, 109, 36, 213, 241, 19, 240, 128,
            113, 142
        ],
                  [
                      248, 241, 148, 140, 143, 63, 222, 195, 202, 210, 244, 74,
                      102, 0, 190, 200
                  ],
                  [
                      29, 45, 179, 186, 183, 88, 115, 91, 115, 240, 60, 133,
                      170, 156, 139, 215
                  ]]
        msg = obj.decrypt_cbc(cipher, iv)
        msgstr = ""
        for i in msg:
            for j in i:
                msgstr += chr(j)
        print("Here is your decrypted message:", unpad(msgstr))

    elif choice == 3:
        text = input("Enter the text to be encrypted:\n")
        key = input("Enter 16 bytes long encryption key:\n")
        while (len(key) != 16):
            key = input(
                "The length of the key needs to be 16, please enter 16 bytes long key\n"
            )
        iv = input("Enter 16 bytes long initialization vector:\n")
        while (len(iv) != 16):