M2Crypto

0.47.0 · maintenance · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

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.

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()}")

view raw JSON →