To support some legacy application I need to implement PBEWithMD5AndDES (RFC2898 Section 6.1) in python. I know this is insecure, deprecated and should not be used anymore. But this is sadly the requirement I have.
I already have a working version that uses PyCrypto/PyCryptodome but I would need to introduce PyCryptodome as additional dependency to the project which is something I want to avoid. As we are already using pyca/cryptography in other parts of our code I'd prefer this library over PyCrypto(dome). However due to the nature of PBEWithMD5AndDES I need DES encryption support but pyca/cryptography only supports Triple DES (3DES) as far as I understood.
Is there a way to (single) DES encrypt something using pyca/cryptography? Basically I need to replace the following usage of Crypto.Cipher.DES from with something from pyca/cryptography:
key, init_vector = _pbkdf1_md5(a_password, a_salt, a_iterations)
cipher = DES.new(key, DES.MODE_CBC, init_vector)
encrypted_message = cipher.encrypt(encoded_message)
**UPDATE**:
Thanks to @SquareRootOfTwentyThree I ended up with this:
(key, init_vector) = _pbkdf1_md5(a_password, a_salt, a_iterations)
cipher = Cipher(algorithms.TripleDES(key), modes.CBC(init_vector), default_backend())
encryptor = self.cipher.encryptor()
encrypted = encryptor.update(encoded_message)
encryptor.finalize()
def _pbkdf1_md5(a_password, a_salt, a_iterations):
digest = Hash(MD5(), default_backend())
digest.update(a_password)
digest.update(a_salt)
key = None
for i in range(a_iterations):
key = digest.finalize()
digest = Hash(MD5(), default_backend())
digest.update(key)
digest.finalize()
return key[:8], key[8:16]
