/gdata/tlslite/utils/Cryptlib_TripleDES.py

http://radioappz.googlecode.com/ · Python · 35 lines · 25 code · 9 blank · 1 comment · 1 complexity · 1555865aa5eaf7f952ccb0846e2a6583 MD5 · raw file

  1. """Cryptlib 3DES implementation."""
  2. from cryptomath import *
  3. from TripleDES import *
  4. if cryptlibpyLoaded:
  5. def new(key, mode, IV):
  6. return Cryptlib_TripleDES(key, mode, IV)
  7. class Cryptlib_TripleDES(TripleDES):
  8. def __init__(self, key, mode, IV):
  9. TripleDES.__init__(self, key, mode, IV, "cryptlib")
  10. self.context = cryptlib_py.cryptCreateContext(cryptlib_py.CRYPT_UNUSED, cryptlib_py.CRYPT_ALGO_3DES)
  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. TripleDES.encrypt(self, plaintext)
  19. bytes = stringToBytes(plaintext)
  20. cryptlib_py.cryptEncrypt(self.context, bytes)
  21. return bytesToString(bytes)
  22. def decrypt(self, ciphertext):
  23. TripleDES.decrypt(self, ciphertext)
  24. bytes = stringToBytes(ciphertext)
  25. cryptlib_py.cryptDecrypt(self.context, bytes)
  26. return bytesToString(bytes)