PageRenderTime 51ms CodeModel.GetById 37ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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