/gdata/tlslite/utils/AES.py

http://radioappz.googlecode.com/ · Python · 31 lines · 23 code · 3 blank · 5 comment · 7 complexity · 09bf37bb2a48d0b0de4f8f539a750dbd MD5 · raw file

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