aes.py revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
1"""Abstract class for AES.""" 2 3class AES: 4 def __init__(self, key, mode, IV, implementation): 5 if len(key) not in (16, 24, 32): 6 raise AssertionError() 7 if mode != 2: 8 raise AssertionError() 9 if len(IV) != 16: 10 raise AssertionError() 11 self.isBlockCipher = True 12 self.block_size = 16 13 self.implementation = implementation 14 if len(key)==16: 15 self.name = "aes128" 16 elif len(key)==24: 17 self.name = "aes192" 18 elif len(key)==32: 19 self.name = "aes256" 20 else: 21 raise AssertionError() 22 23 #CBC-Mode encryption, returns ciphertext 24 #WARNING: *MAY* modify the input as well 25 def encrypt(self, plaintext): 26 assert(len(plaintext) % 16 == 0) 27 28 #CBC-Mode decryption, returns plaintext 29 #WARNING: *MAY* modify the input as well 30 def decrypt(self, ciphertext): 31 assert(len(ciphertext) % 16 == 0)