{"id":7387,"library":"m2crypto","title":"M2Crypto","description":"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.","status":"maintenance","version":"0.47.0","language":"en","source_language":"en","source_url":"https://gitlab.com/m2crypto/m2crypto","tags":["cryptography","ssl","security","openssl","tls"],"install":[{"cmd":"pip install M2Crypto","lang":"bash","label":"Basic installation"},{"cmd":"sudo apt-get install python3-dev libssl-dev swig\npip install M2Crypto","lang":"bash","label":"Debian/Ubuntu with build dependencies"},{"cmd":"sudo yum install python3-devel openssl-devel swig\npip install M2Crypto","lang":"bash","label":"RHEL/CentOS with build dependencies"}],"dependencies":[{"reason":"M2Crypto is a wrapper around the OpenSSL library and requires it for core cryptographic functions.","package":"openssl","optional":false},{"reason":"M2Crypto uses SWIG (Simplified Wrapper and Interface Generator) to create its Python interface from C/C++ code. SWIG 4.0+ is required for M2Crypto 0.47.0+.","package":"swig","optional":false},{"reason":"Used for version parsing and compatibility checks.","package":"packaging","optional":false}],"imports":[{"note":"Many M2Crypto modules are designed to be imported directly from the M2Crypto package, rather than as submodules, to ensure attributes are correctly resolved.","wrong":"import M2Crypto.RSA","symbol":"RSA","correct":"from M2Crypto import RSA"},{"note":"Similar to RSA, direct import from M2Crypto is the standard and recommended practice.","wrong":"import M2Crypto.SSL","symbol":"SSL","correct":"from M2Crypto import SSL"},{"note":"Older patterns like `import M2Crypto` followed by `M2Crypto.X509` may lead to 'M2Crypto has no attributes' errors in some contexts, especially when not fully installed or with specific runtime environments.","wrong":"import M2Crypto.X509","symbol":"X509","correct":"from M2Crypto import X509"}],"quickstart":{"code":"from M2Crypto import RSA\n\n# Generate RSA key pairs\nrsa_key = RSA.gen_key(2048, 65537)\n\n# Message to encrypt\nmessage = b\"Hello, M2Crypto!\"\n\n# Encrypt a message using the public key\nciphertext = rsa_key.public_encrypt(message, RSA.pkcs1_oaep_padding)\nprint(f\"Ciphertext: {ciphertext.hex()}\")\n\n# Decrypt the message using the private key\ndecrypted = rsa_key.private_decrypt(ciphertext, RSA.pkcs1_oaep_padding)\nprint(f\"Decrypted: {decrypted.decode()}\")\n","lang":"python","description":"This quickstart demonstrates basic RSA key generation, encryption, and decryption using M2Crypto. It generates a 2048-bit RSA key pair, encrypts a simple message using PKCS1 OAEP padding with the public key, and then decrypts it using the private key. Note that messages should be bytes for cryptographic operations."},"warnings":[{"fix":"For new projects, consider PyCA/cryptography (`pip install cryptography`). For existing projects, consult M2Crypto's documentation on migration paths.","message":"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.","severity":"deprecated","affected_versions":"All versions"},{"fix":"Ensure SWIG (version 4.0 or newer for Python 3.12+) is installed globally or in your build environment before running `pip install M2Crypto`.","message":"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+.","severity":"breaking","affected_versions":"0.47.0+"},{"fix":"Applications relying on `asyncore` or related M2Crypto modules for asynchronous SSL operations must refactor their code to use more modern asynchronous frameworks (e.g., `asyncio`) or different SSL libraries if targeting M2Crypto 0.47.0+ with Python 3.12+.","message":"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+.","severity":"breaking","affected_versions":"0.47.0+"},{"fix":"Install `openssl-devel` (or equivalent) and `swig` via your system's package manager before attempting `pip install M2Crypto`. On Windows, manual compilation or pre-built wheels from other projects might be necessary if `pip install` fails.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Handle M2Crypto objects carefully, ensuring proper lifecycle management. Keep OpenSSL and M2Crypto versions compatible. Regularly test your application for memory leaks, especially when dealing with high-volume cryptographic operations.","message":"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.","severity":"gotcha","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":"Install 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+.","cause":"SWIG (Simplified Wrapper and Interface Generator) is not installed or not in the system's PATH, or an incompatible version is being used.","error":"error: command 'swig.exe' failed: None"},{"fix":"Install 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`.","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.","error":"error: subprocess-exited-with-error\n... running setup.py install for M2Crypto ... error"},{"fix":"Verify 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`.","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.","error":"ImportError: No module named M2Crypto"},{"fix":"Use explicit imports for the required modules, e.g., `from M2Crypto import X509`. This ensures the module is loaded and its attributes are directly available.","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.","error":"AttributeError: module 'M2Crypto' has no attribute 'X509'"},{"fix":"Ensure 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`.","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.","error":"SyntaxError: invalid syntax (when trying to run an application using M2Crypto)"}]}