{"id":8476,"library":"pydes","title":"pyDes","description":"pyDes is a pure Python implementation of the DES and Triple-DES encryption algorithms. It is designed for portability across Python versions rather than performance, making it suitable for simple, small-scale encryption tasks. The current version, 2.0.1, was released on April 28, 2010, and the project is no longer actively maintained.","status":"abandoned","version":"2.0.1","language":"en","source_language":"en","source_url":"https://github.com/twhiteman/pyDes","tags":["encryption","DES","cryptography","security"],"install":[{"cmd":"pip install pyDes","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"The primary DES class is accessed via the `pyDes` module.","symbol":"des","correct":"import pyDes\ndes_cipher = pyDes.des(key, mode, iv, pad, padmode)"},{"note":"The Triple DES class is accessed via the `pyDes` module.","symbol":"triple_des","correct":"import pyDes\ntdes_cipher = pyDes.triple_des(key, mode, iv, pad, padmode)"}],"quickstart":{"code":"import pyDes\n\n# For Python 3, all strings must be converted to bytes\n# For DES, key must be exactly 8 bytes\n# For Triple DES, key must be 16 or 24 bytes\n# IV (Initialization Vector) must be 8 bytes for CBC mode\n\nkey_des = b\"DESCRYPT\"\nkey_3des_2key = b\"0123456789ABCDEF\"\nkey_3des_3key = b\"0123456789ABCDEFabcdefgh\"\n\ndata = b\"Please encrypt my data. It needs to be padded.\"\n\n# --- Single DES Example (CBC mode, PKCS5 padding) ---\ntry:\n    # DES encryption object\n    k_des = pyDes.des(key_des, pyDes.CBC, b\"\\0\\0\\0\\0\\0\\0\\0\\0\", pad=None, padmode=pyDes.PAD_PKCS5)\n    encrypted_des = k_des.encrypt(data)\n    print(f\"Encrypted DES: {encrypted_des!r}\")\n    decrypted_des = k_des.decrypt(encrypted_des)\n    print(f\"Decrypted DES: {decrypted_des!r}\")\n    assert decrypted_des == data\nexcept ValueError as e:\n    print(f\"DES Error: {e}\")\n\n# --- Triple DES Example (2-Key, CBC mode, PKCS5 padding) ---\ntry:\n    # Triple DES encryption object (2-key)\n    k_3des_2key = pyDes.triple_des(key_3des_2key, pyDes.CBC, b\"\\0\\0\\0\\0\\0\\0\\0\\0\", pad=None, padmode=pyDes.PAD_PKCS5)\n    encrypted_3des_2key = k_3des_2key.encrypt(data)\n    print(f\"Encrypted 3DES (2-key): {encrypted_3des_2key!r}\")\n    decrypted_3des_2key = k_3des_2key.decrypt(encrypted_3des_2key)\n    print(f\"Decrypted 3DES (2-key): {decrypted_3des_2key!r}\")\n    assert decrypted_3des_2key == data\nexcept ValueError as e:\n    print(f\"3DES (2-key) Error: {e}\")\n\n# --- Triple DES Example (3-Key, ECB mode, PKCS5 padding) ---\ntry:\n    # Triple DES encryption object (3-key)\n    k_3des_3key = pyDes.triple_des(key_3des_3key, pyDes.ECB, pad=None, padmode=pyDes.PAD_PKCS5)\n    encrypted_3des_3key = k_3des_3key.encrypt(data)\n    print(f\"Encrypted 3DES (3-key): {encrypted_3des_3key!r}\")\n    decrypted_3des_3key = k_3des_3key.decrypt(encrypted_3des_3key)\n    print(f\"Decrypted 3DES (3-key): {decrypted_3des_3key!r}\")\n    assert decrypted_3des_3key == data\nexcept ValueError as e:\n    print(f\"3DES (3-key) Error: {e}\")","lang":"python","description":"This quickstart demonstrates basic DES and Triple DES encryption and decryption using pyDes. It highlights the necessity of using byte strings for keys, IVs, and data in Python 3, and the recommended practice of using PKCS5 padding. Separate examples are provided for DES and different Triple DES key lengths."},"warnings":[{"fix":"Ensure all `key`, `IV`, and `data` arguments are byte strings (e.g., `b\"mydata\"`). Encode regular strings using `.encode('utf-8')` and decode results with `.decode('utf-8')` if text is required.","message":"Python 2 vs. Python 3 string handling: pyDes expects bytes for all key, IV, and data arguments. In Python 2, `str` was implicitly bytes, but in Python 3, explicit byte strings (`b\"...\"`) must be used. Passing regular Python 3 strings will lead to `TypeError` or `ValueError`.","severity":"breaking","affected_versions":"All versions when migrating from Python 2 to Python 3"},{"fix":"Always ensure the provided key has the exact required length in bytes for the chosen DES or Triple DES algorithm.","message":"Key length requirements: DES requires an 8-byte key. Triple DES requires either a 16-byte key (DES-EDE2) or a 24-byte key (DES-EDE3). Providing an incorrect key length will raise a `ValueError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always use `padmode=pyDes.PAD_PKCS5` when initializing `des` or `triple_des` instances. If not using PKCS5, ensure data length is a multiple of 8 or manually apply a consistent padding scheme.","message":"Data padding is crucial: Encryption/decryption operates on 8-byte blocks. If input data is not a multiple of 8 bytes, it must be padded. Failure to pad, or incorrect padding, leads to `ValueError: Data must be a multiple of 8 bytes in length.` The recommended padding mode is `pyDes.PAD_PKCS5` as it is unambiguously reversible.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid using DES or Triple DES for new applications. Consider modern encryption algorithms like AES (e.g., via `cryptography` library) with appropriate modes (e.g., GCM or CBC with a strong IV). If you must use pyDes, always prefer `pyDes.CBC` mode with a unique, random IV for each encryption.","message":"Security vulnerability: DES and Triple DES (3DES) are cryptographically weak and considered obsolete for modern security requirements due to short key lengths and susceptibility to various attacks. The default ECB mode is particularly insecure as it leaks patterns in the plaintext.","severity":"deprecated","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure the key is a byte string of the correct length: 8 bytes for DES, or 16/24 bytes for Triple DES. Example: `key = b\"8bytekey\"`.","cause":"The encryption key provided to `pyDes.des` was not exactly 8 bytes in length. For `pyDes.triple_des`, the key must be 16 or 24 bytes.","error":"ValueError: Invalid DES key size. Key must be exactly 8 bytes long."},{"fix":"Initialize the `des` or `triple_des` object with `padmode=pyDes.PAD_PKCS5` to automatically handle padding. Example: `k = pyDes.des(key, pyDes.CBC, iv, padmode=pyDes.PAD_PKCS5)`.","cause":"The input data for encryption or decryption was not a multiple of 8 bytes, and no padding mode or character was specified.","error":"ValueError: Data must be a multiple of 8 bytes in length. Use padmode=PAD_PKCS5 or set the pad character."},{"fix":"Explicitly convert `bytes` to `str` using `.decode('utf-8')` for display/string operations, and `str` to `bytes` using `.encode('utf-8')` before passing to encryption functions. Ensure all inputs to pyDes are `bytes` type.","cause":"Attempting to concatenate or mix Python 3 `bytes` objects (returned by `encrypt`/`decrypt`) with `str` objects, or passing a `str` where `bytes` is expected.","error":"TypeError: Can't convert 'bytes' object to str implicitly"}]}