PageRenderTime 27ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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