Install & Compatibility
Where this runs
tested against v2.0.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
des
✓ from pyDes import des
✗ import pyDes
triple_des
✓ from pyDes import triple_des
✗ import pyDes
CBC
✓ from pyDes import CBC
✗ import pyDes
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.
import pyDes
# For Python 3, all strings must be converted to bytes
# For DES, key must be exactly 8 bytes
# For Triple DES, key must be 16 or 24 bytes
# IV (Initialization Vector) must be 8 bytes for CBC mode
key_des = b"DESCRYPT"
key_3des_2key = b"0123456789ABCDEF"
key_3des_3key = b"0123456789ABCDEFabcdefgh"
data = b"Please encrypt my data. It needs to be padded."
# --- Single DES Example (CBC mode, PKCS5 padding) ---
try:
# DES encryption object
k_des = pyDes.des(key_des, pyDes.CBC, b"\0\0\0\0\0\0\0\0", pad=None, padmode=pyDes.PAD_PKCS5)
encrypted_des = k_des.encrypt(data)
print(f"Encrypted DES: {encrypted_des!r}")
decrypted_des = k_des.decrypt(encrypted_des)
print(f"Decrypted DES: {decrypted_des!r}")
assert decrypted_des == data
except ValueError as e:
print(f"DES Error: {e}")
# --- Triple DES Example (2-Key, CBC mode, PKCS5 padding) ---
try:
# Triple DES encryption object (2-key)
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)
encrypted_3des_2key = k_3des_2key.encrypt(data)
print(f"Encrypted 3DES (2-key): {encrypted_3des_2key!r}")
decrypted_3des_2key = k_3des_2key.decrypt(encrypted_3des_2key)
print(f"Decrypted 3DES (2-key): {decrypted_3des_2key!r}")
assert decrypted_3des_2key == data
except ValueError as e:
print(f"3DES (2-key) Error: {e}")
# --- Triple DES Example (3-Key, ECB mode, PKCS5 padding) ---
try:
# Triple DES encryption object (3-key)
k_3des_3key = pyDes.triple_des(key_3des_3key, pyDes.ECB, pad=None, padmode=pyDes.PAD_PKCS5)
encrypted_3des_3key = k_3des_3key.encrypt(data)
print(f"Encrypted 3DES (3-key): {encrypted_3des_3key!r}")
decrypted_3des_3key = k_3des_3key.decrypt(encrypted_3des_3key)
print(f"Decrypted 3DES (3-key): {decrypted_3des_3key!r}")
assert decrypted_3des_3key == data
except ValueError as e:
print(f"3DES (3-key) Error: {e}")
Debug
Known issues
breakingPython 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`.fixEnsure 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. affects: All versions when migrating from Python 2 to Python 3
gotchaKey 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`.fixAlways ensure the provided key has the exact required length in bytes for the chosen DES or Triple DES algorithm.
affects: All versions
gotchaData 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.fixAlways 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.
affects: All versions
deprecatedSecurity 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.fixAvoid 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.
affects: All versions
Upgrade
Version history
2.0.1latest on PyPI · released May 6, 2016
Audit
Dependencies
No dependency data recorded yet.