/gdata/tlslite/utils/Cryptlib_RC4.py

http://radioappz.googlecode.com/ · Python · 28 lines · 19 code · 8 blank · 1 comment · 1 complexity · 503e67be2ae467ee4be837302d07ff1a MD5 · raw file

  1. """Cryptlib RC4 implementation."""
  2. from cryptomath import *
  3. from RC4 import RC4
  4. if cryptlibpyLoaded:
  5. def new(key):
  6. return Cryptlib_RC4(key)
  7. class Cryptlib_RC4(RC4):
  8. def __init__(self, key):
  9. RC4.__init__(self, key, "cryptlib")
  10. self.context = cryptlib_py.cryptCreateContext(cryptlib_py.CRYPT_UNUSED, cryptlib_py.CRYPT_ALGO_RC4)
  11. cryptlib_py.cryptSetAttribute(self.context, cryptlib_py.CRYPT_CTXINFO_KEYSIZE, len(key))
  12. cryptlib_py.cryptSetAttributeString(self.context, cryptlib_py.CRYPT_CTXINFO_KEY, key)
  13. def __del__(self):
  14. cryptlib_py.cryptDestroyContext(self.context)
  15. def encrypt(self, plaintext):
  16. bytes = stringToBytes(plaintext)
  17. cryptlib_py.cryptEncrypt(self.context, bytes)
  18. return bytesToString(bytes)
  19. def decrypt(self, ciphertext):
  20. return self.encrypt(ciphertext)