title: DES cipher with pyca/cryptography (PBEWithMD5AndDES)
body: <p>To support some legacy application I need to implement <code>PBEWithMD5AndDES</code> (<a href="https://www.rfc-editor.org/rfc/rfc2898#section-6.1" rel="nofollow noreferrer">RFC2898 Section 6.1</a>) in python. I know this is insecure, deprecated and should not be used anymore. But this is sadly the requirement I have.</p>
  <p>I already have a working version that uses <code>PyCrypto</code>/<code>PyCryptodome</code> but I would need to introduce <a href="https://pycryptodome.readthedocs.io/en/latest/" rel="nofollow noreferrer"><code>PyCryptodome</code></a> as additional dependency to the project which is something I want to avoid. As we are already using <a href="https://cryptography.io/en/latest/" rel="nofollow noreferrer"><code>pyca/cryptography</code></a> in other parts of our code I'd prefer this library over <code>PyCrypto(dome)</code>. However due to the nature of <code>PBEWithMD5AndDES</code> I need DES encryption support but <code>pyca/cryptography</code> only supports Triple DES (3DES) as far as I understood.</p>
  <p>Is there a way to (single) DES encrypt something using <code>pyca/cryptography</code>? Basically I need to replace the following usage of <code>Crypto.Cipher.DES</code> from with something from <code>pyca/cryptography</code>:</p>
  <pre><code>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)
  </code></pre>
  <br>
  <br>
  **UPDATE**:
  <br>
  <br>
  Thanks to @SquareRootOfTwentyThree I ended up with this:
  <pre><code>(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]
  </code></pre>