OLD | NEW |
| (Empty) |
1 """Cryptlib 3DES implementation.""" | |
2 | |
3 from cryptomath import * | |
4 | |
5 from tripledes import * | |
6 | |
7 if 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_UNUS
ED, cryptlib_py.CRYPT_ALGO_3DES) | |
17 cryptlib_py.cryptSetAttribute(self.context, cryptlib_py.CRYPT_CTXINF
O_MODE, cryptlib_py.CRYPT_MODE_CBC) | |
18 cryptlib_py.cryptSetAttribute(self.context, cryptlib_py.CRYPT_CTXINF
O_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) | |
OLD | NEW |