1 """Cryptlib 3DES implementation."""
3 from cryptomath import *
5 from TripleDES import *
9 def new(key, mode, IV):
10 return Cryptlib_TripleDES(key, mode, IV)
12 class Cryptlib_TripleDES(TripleDES):
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)
23 cryptlib_py.cryptDestroyContext(self.context)
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)
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)