caellion-python-commons: caellion/pycommons/encryption/tripledes.py Source File

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
11 class TripleDESECBMD5Key:
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.password passwordpassword = password.encode("ASCII", errors="replace")
28 md5 = hashlib.md5()
29 md5.update(self.password passwordpassword)
30 hash = md5.digest()
31 self.encryptionKey encryptionKeyencryptionKey = hash
32
33 self.DES3DES3 = pyDes.triple_des(
34 self.encryptionKey encryptionKeyencryptionKey, 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
caellion.pycommons.encryption.tripledes.TripleDESECBMD5Key
This class provides implementation of triple DES algorithm which uses MD5 hash of password as a key (...
Definition: tripledes.py:11
caellion.pycommons.encryption.tripledes.TripleDESECBMD5Key.encryptionKey
encryptionKey
Definition: tripledes.py:31
caellion.pycommons.encryption.tripledes.TripleDESECBMD5Key.DES3
DES3
Definition: tripledes.py:18
caellion.pycommons.encryption.tripledes.TripleDESECBMD5Key.password
string password
Definition: tripledes.py:16
caellion.pycommons.encryption.tripledes.TripleDESECBMD5Key.decrypt
def decrypt(self, cipher)
Decodes and decrypts Base64-encoded 3DES-encrypted data.
Definition: tripledes.py:48
caellion.pycommons.encryption.tripledes.TripleDESECBMD5Key.encrypt
def encrypt(self, data)
Encrypts data with 3DES ECB algorithm.
Definition: tripledes.py:37
caellion.pycommons.encryption.tripledes.TripleDESECBMD5Key.password
password
Definition: tripledes.py:27
caellion.pycommons.encryption.tripledes.TripleDESECBMD5Key.padmode
padmode
Definition: tripledes.py:19
caellion.pycommons.encryption.tripledes.TripleDESECBMD5Key.encryptionKey
string encryptionKey
Definition: tripledes.py:17
caellion.pycommons.encryption.tripledes.TripleDESECBMD5Key.__init__
def __init__(self, password)
Sets up the class for use.
Definition: tripledes.py:21