M2Crypto
M2Crypto is a comprehensive cryptography and SSL toolkit for Python, acting as a wrapper around the well-established OpenSSL library via SWIG. It provides functionalities for RSA, DSA, DH, HMACs, message digests, symmetric ciphers including AES, and TLS for client and server implementations. The library is currently in maintenance mode, with its developers recommending migration to more modern alternatives like PyCA/cryptography. The latest version, 0.47.0, was released on February 8, 2026. While not on a fixed schedule, releases appear periodically.
Common errors
-
error: command 'swig.exe' failed: None
cause SWIG (Simplified Wrapper and Interface Generator) is not installed or not in the system's PATH, or an incompatible version is being used.fixInstall SWIG on your system (e.g., `sudo apt-get install swig` or via Chocolatey on Windows: `choco install -y swig`). For M2Crypto 0.47.0+, ensure SWIG 4.0+ is installed, especially for Python 3.12+. -
error: subprocess-exited-with-error ... running setup.py install for M2Crypto ... error
cause This generic error during `pip install` typically indicates missing OpenSSL development headers, SWIG, or C/C++ build tools required to compile M2Crypto's C extensions.fixInstall necessary build tools (e.g., `build-essential` on Debian/Ubuntu, C++ Build Tools on Windows), OpenSSL development headers (e.g., `libssl-dev` or `openssl-devel`), and SWIG before running `pip install M2Crypto`. -
ImportError: No module named M2Crypto
cause The M2Crypto library was not successfully installed, or the Python interpreter cannot find it in its `sys.path`. This can happen if the installation failed silently or if C extensions were not built.fixVerify successful installation by trying `pip show M2Crypto`. If not installed, re-attempt installation after ensuring all system dependencies (OpenSSL-dev, SWIG) are met. If installed, check your Python environment's `sys.path`. -
AttributeError: module 'M2Crypto' has no attribute 'X509'
cause This error can occur if you're attempting to access a submodule (like X509) using `M2Crypto.X509` without it being explicitly imported into the `M2Crypto` namespace, or in older versions/specific installation contexts.fixUse explicit imports for the required modules, e.g., `from M2Crypto import X509`. This ensures the module is loaded and its attributes are directly available. -
SyntaxError: invalid syntax (when trying to run an application using M2Crypto)
cause While M2Crypto 0.47.0 supports Python 3.6+, older versions of M2Crypto (or legacy code written for Python 2) might be incompatible with the Python interpreter version being used.fixEnsure that the version of M2Crypto installed is compatible with your Python interpreter version. If using older code, consider porting it to use a modern M2Crypto version compatible with your Python 3 environment, or migrate to `cryptography`.
Warnings
- deprecated M2Crypto is officially in maintenance mode. Developers recommend migrating to actively developed and more modern alternatives like PyCA/cryptography for new projects or existing applications.
- breaking Version 0.47.0 (and newer) no longer includes SWIG generated files in the distribution. This means `swig` must be installed on your system when installing M2Crypto, with SWIG 4.0+ being required for Python 3.12+.
- breaking All support for Python's `asyncore` module, `contrib/dispatcher.py`, `M2Crypto/SSL/ssl_dispatcher.py`, and `ZServerSSL` has been removed in M2Crypto 0.47.0 to align with its removal in Python 3.12+.
- gotcha Installation of M2Crypto often requires system-level development headers for OpenSSL and the SWIG utility, which can be challenging, especially on Windows or minimal Linux environments.
- gotcha M2Crypto, due to its direct interfacing with OpenSSL's C API, can encounter memory leaks or segmentation faults if objects are not freed correctly across the Python/C boundary, or due to incompatibilities with different OpenSSL versions.
Install
-
pip install M2Crypto -
sudo apt-get install python3-dev libssl-dev swig pip install M2Crypto -
sudo yum install python3-devel openssl-devel swig pip install M2Crypto
Imports
- RSA
import M2Crypto.RSA
from M2Crypto import RSA
- SSL
import M2Crypto.SSL
from M2Crypto import SSL
- X509
import M2Crypto.X509
from M2Crypto import X509
Quickstart
from M2Crypto import RSA
# Generate RSA key pairs
rsa_key = RSA.gen_key(2048, 65537)
# Message to encrypt
message = b"Hello, M2Crypto!"
# Encrypt a message using the public key
ciphertext = rsa_key.public_encrypt(message, RSA.pkcs1_oaep_padding)
print(f"Ciphertext: {ciphertext.hex()}")
# Decrypt the message using the private key
decrypted = rsa_key.private_decrypt(ciphertext, RSA.pkcs1_oaep_padding)
print(f"Decrypted: {decrypted.decode()}")