/gdata/tlslite/utils/Cryptlib_AES.py

http://radioappz.googlecode.com/ · Python · 34 lines · 25 code · 8 blank · 1 comment · 1 complexity · 6112ef594a98ef61b95f9ec48c2f9a6c MD5 · raw file

  1. """Cryptlib AES implementation."""
  2. from cryptomath import *
  3. from AES import *
  4. if cryptlibpyLoaded:
  5. def new(key, mode, IV):
  6. return Cryptlib_AES(key, mode, IV)
  7. class Cryptlib_AES(AES):
  8. def __init__(self, key, mode, IV):
  9. AES.__init__(self, key, mode, IV, "cryptlib")
  10. self.context = cryptlib_py.cryptCreateContext(cryptlib_py.CRYPT_UNUSED, cryptlib_py.CRYPT_ALGO_AES)
  11. cryptlib_py.cryptSetAttribute(self.context, cryptlib_py.CRYPT_CTXINFO_MODE, cryptlib_py.CRYPT_MODE_CBC)
  12. cryptlib_py.cryptSetAttribute(self.context, cryptlib_py.CRYPT_CTXINFO_KEYSIZE, len(key))
  13. cryptlib_py.cryptSetAttributeString(self.context, cryptlib_py.CRYPT_CTXINFO_KEY, key)
  14. cryptlib_py.cryptSetAttributeString(self.context, cryptlib_py.CRYPT_CTXINFO_IV, IV)
  15. def __del__(self):
  16. cryptlib_py.cryptDestroyContext(self.context)
  17. def encrypt(self, plaintext):
  18. AES.encrypt(self, plaintext)
  19. bytes = stringToBytes(plaintext)
  20. cryptlib_py.cryptEncrypt(self.context, bytes)
  21. return bytesToString(bytes)
  22. def decrypt(self, ciphertext):
  23. AES.decrypt(self, ciphertext)
  24. bytes = stringToBytes(ciphertext)
  25. cryptlib_py.cryptDecrypt(self.context, bytes)
  26. return bytesToString(bytes)