Pure-Python RSA implementation for encryption, decryption, signing, and verification
npx @tessl/cli install tessl/pypi-rsa@4.9.0A pure-Python implementation of the RSA cryptographic algorithm supporting encryption, decryption, digital signing, and signature verification according to PKCS#1 version 1.5 standards. This library provides both a Python API for programmatic use and command-line tools for direct RSA operations.
pip install rsaimport rsaImport specific functions:
from rsa import newkeys, encrypt, decrypt, sign, verify
from rsa import PublicKey, PrivateKeyimport rsa
# Generate a key pair
(public_key, private_key) = rsa.newkeys(512)
# Encrypt a message
message = b'hello world'
encrypted = rsa.encrypt(message, public_key)
# Decrypt the message
decrypted = rsa.decrypt(encrypted, private_key)
print(decrypted.decode()) # outputs: hello world
# Sign a message
signature = rsa.sign(message, private_key, 'SHA-256')
# Verify the signature
hash_method = rsa.verify(message, signature, public_key)
print(hash_method) # outputs: SHA-256The RSA library is organized around three main components:
The library implements pure-Python RSA operations without external cryptographic dependencies, making it suitable for educational purposes and environments requiring minimal dependencies.
RSA key pair generation and key object management including loading/saving keys in PKCS#1 PEM/DER formats and OpenSSL compatibility.
def newkeys(nbits: int, accurate: bool = True, poolsize: int = 1, exponent: int = 65537) -> Tuple[PublicKey, PrivateKey]: ...
class PublicKey:
def __init__(self, n: int, e: int): ...
def save_pkcs1_pem(self) -> bytes: ...
def save_pkcs1_der(self) -> bytes: ...
class PrivateKey:
def __init__(self, n: int, e: int, d: int, p: int, q: int): ...
def save_pkcs1_pem(self) -> bytes: ...
def save_pkcs1_der(self) -> bytes: ...PKCS#1 v1.5 encryption, decryption, signing, and signature verification with support for multiple hash algorithms.
def encrypt(message: bytes, pub_key: PublicKey) -> bytes: ...
def decrypt(crypto: bytes, priv_key: PrivateKey) -> bytes: ...
def sign(message: bytes, priv_key: PrivateKey, hash_method: str) -> bytes: ...
def verify(message: bytes, signature: bytes, pub_key: PublicKey) -> str: ...
def sign_hash(hash_value: bytes, priv_key: PrivateKey, hash_method: str) -> bytes: ...
def find_signature_hash(signature: bytes, pub_key: PublicKey) -> str: ...
def compute_hash(message: Union[bytes, BinaryIO], method_name: str) -> bytes: ...Command-line utilities for key generation, encryption, decryption, signing, and signature verification.
# CLI entry points available as shell commands:
# pyrsa-keygen - Generate RSA key pairs
# pyrsa-encrypt - Encrypt data with public key
# pyrsa-decrypt - Decrypt data with private key
# pyrsa-sign - Sign data with private key
# pyrsa-verify - Verify signature with public key
# pyrsa-priv2pub - Convert private key to public keyclass CryptoError(Exception):
"""Base class for all cryptographic exceptions"""
class DecryptionError(CryptoError):
"""Raised when decryption fails"""
class VerificationError(CryptoError):
"""Raised when signature verification fails"""from typing import Tuple, Union, BinaryIO, Iterator, Callable