caellion-python-commons
tripledes.py
Go to the documentation of this file.
1 """!
2 This module provides utilities related to or using 3DES symmetric encryption algorithm
3 """
4 
5 import pyDes
6 import hashlib
7 import base64
8 
9 
11  """!
12  This class provides implementation of triple DES algorithm which uses MD5 hash of password as a key (it uses 16-byte key so it is triple DES with 2 keys)
13  """
14 
15  password = ""
16  encryptionKey = ""
17  DES3 = None
18  padmode = pyDes.PAD_PKCS5
19 
20  def __init__(self, password):
21  """!
22  Sets up the class for use
23 
24  @param password password to use for key generation
25  """
26  self.password = password.encode("ASCII", errors="replace")
27  md5 = hashlib.md5()
28  md5.update(self.password)
29  hash = md5.digest()
30  self.encryptionKey = hash
31 
32  self.DES3 = pyDes.triple_des(self.encryptionKey, pyDes.ECB, padmode=self.padmode)
33 
34  def encrypt(self, data):
35  """!
36  Encrypts data with 3DES ECB algorithm
37 
38  @param data data to be encrypted
39 
40  @returns base64-encoded 3DES encrypted string
41  """
42  cipher = self.DES3.encrypt(data, padmode=self.padmode)
43  return base64.encodebytes(cipher).decode("ASCII").strip()
44 
45  def decrypt(self, cipher):
46  """!
47  Decodes and decrypts Base64-encoded 3DES-encrypted data
48 
49  @param cipher base64-encoded encrypted data to decrypt
50 
51  @returns decrypted string
52  """
53  cipher = base64.decodebytes((cipher).encode("ASCII"))
54  data = self.DES3.decrypt(cipher, padmode=self.padmode)
55  data = data.decode("ASCII")
56  return data
This class provides implementation of triple DES algorithm which uses MD5 hash of password as a key (...
Definition: tripledes.py:10
def decrypt(self, cipher)
Decodes and decrypts Base64-encoded 3DES-encrypted data.
Definition: tripledes.py:45
def encrypt(self, data)
Encrypts data with 3DES ECB algorithm.
Definition: tripledes.py:34
def __init__(self, password)
Sets up the class for use.
Definition: tripledes.py:20