1# Author: Trevor Perrin
2# See the LICENSE file for legal information regarding use of this file.
3
4"""PyCrypto 3DES implementation."""
5
6from .cryptomath import *
7from .tripledes import *
8
9if pycryptoLoaded:
10    import Crypto.Cipher.DES3
11
12    def new(key, mode, IV):
13        return PyCrypto_TripleDES(key, mode, IV)
14
15    class PyCrypto_TripleDES(TripleDES):
16
17        def __init__(self, key, mode, IV):
18            TripleDES.__init__(self, key, mode, IV, "pycrypto")
19            key = bytes(key)
20            IV = bytes(IV)
21            self.context = Crypto.Cipher.DES3.new(key, mode, IV)
22
23        def encrypt(self, plaintext):
24            plaintext = bytes(plaintext)
25            return bytearray(self.context.encrypt(plaintext))
26
27        def decrypt(self, ciphertext):
28            ciphertext = bytes(ciphertext)
29            return bytearray(self.context.decrypt(ciphertext))