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 base64
6 import hashlib
7 
8 import pyDes
9 
10 
12  """!
13  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)
14  """
15 
16  password = ""
17  encryptionKey = ""
18  DES3 = None
19  padmode = pyDes.PAD_PKCS5
20 
21  def __init__(self, password):
22  """!
23  Sets up the class for use
24 
25  @param password password to use for key generation
26  """
27  self.passwordpasswordpassword = password.encode("ASCII", errors="replace")
28  md5 = hashlib.md5()
29  md5.update(self.passwordpasswordpassword)
30  hash = md5.digest()
31  self.encryptionKeyencryptionKeyencryptionKey = hash
32 
33  self.DES3DES3 = pyDes.triple_des(
34  self.encryptionKeyencryptionKeyencryptionKey, pyDes.ECB, padmode=self.padmodepadmode
35  )
36 
37  def encrypt(self, data):
38  """!
39  Encrypts data with 3DES ECB algorithm
40 
41  @param data data to be encrypted
42 
43  @returns base64-encoded 3DES encrypted string
44  """
45  cipher = self.DES3DES3.encrypt(data, padmode=self.padmodepadmode)
46  return base64.encodebytes(cipher).decode("ASCII").strip()
47 
48  def decrypt(self, cipher):
49  """!
50  Decodes and decrypts Base64-encoded 3DES-encrypted data
51 
52  @param cipher base64-encoded encrypted data to decrypt
53 
54  @returns decrypted string
55  """
56  cipher = base64.decodebytes((cipher).encode("ASCII"))
57  data = self.DES3DES3.decrypt(cipher, padmode=self.padmodepadmode)
58  data = data.decode("ASCII")
59  return data
This class provides implementation of triple DES algorithm which uses MD5 hash of password as a key (...
Definition: tripledes.py:11
def decrypt(self, cipher)
Decodes and decrypts Base64-encoded 3DES-encrypted data.
Definition: tripledes.py:48
def encrypt(self, data)
Encrypts data with 3DES ECB algorithm.
Definition: tripledes.py:37
def __init__(self, password)
Sets up the class for use.
Definition: tripledes.py:21