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.
pip install M2CryptoVerified import paths — ran on the pinned version, not inferred.
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.
For new projects, consider PyCA/cryptography (`pip install cryptography`). For existing projects, consult M2Crypto's documentation on migration paths.
Ensure SWIG (version 4.0 or newer for Python 3.12+) is installed globally or in your build environment before running `pip install M2Crypto`.
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+.
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.
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.