PageRenderTime 27ms CodeModel.GetById 19ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 3from cryptomath import *
 4from RC4 import RC4
 5
 6if cryptlibpyLoaded:
 7
 8    def new(key):
 9        return Cryptlib_RC4(key)
10
11    class Cryptlib_RC4(RC4):
12
13        def __init__(self, key):
14            RC4.__init__(self, key, "cryptlib")
15            self.context = cryptlib_py.cryptCreateContext(cryptlib_py.CRYPT_UNUSED, cryptlib_py.CRYPT_ALGO_RC4)
16            cryptlib_py.cryptSetAttribute(self.context, cryptlib_py.CRYPT_CTXINFO_KEYSIZE, len(key))
17            cryptlib_py.cryptSetAttributeString(self.context, cryptlib_py.CRYPT_CTXINFO_KEY, key)
18
19        def __del__(self):
20             cryptlib_py.cryptDestroyContext(self.context)
21
22        def encrypt(self, plaintext):
23            bytes = stringToBytes(plaintext)
24            cryptlib_py.cryptEncrypt(self.context, bytes)
25            return bytesToString(bytes)
26
27        def decrypt(self, ciphertext):
28            return self.encrypt(ciphertext)