python-rsa
Pure-Python RSA library for key generation, PKCS#1 encryption, decryption, signing, and verification, with no C extensions required.
Repository Health
Technical Analysis
rsa is a pure-Python implementation of RSA public-key cryptography covering PKCS#1 v1.5 key generation, encryption, decryption, signing, and signature verification. It ships as both an importable library (import rsa) and a set of console scripts (pyrsa-keygen, pyrsa-encrypt, pyrsa-sign, and more), and reads and writes standard PEM/DER-encoded keys via pyasn1 so it interoperates with keys generated by OpenSSL and other PKCS#1 tooling.
Because it has no compiled extensions, it drops into environments where installing cryptography’s C/Rust bindings is impractical. It includes decryption blinding to reduce timing side-channel exposure and a multiprocessing-based parallel key generator to offset the cost of large-integer primality testing in pure Python, though the maintainer is explicit that pure-Python execution can’t guarantee full constant-time behavior, and the project has been archived with no further releases planned.
What You Get
- PKCS#1 key generation -
rsa.newkeys()generates matched public/private key pairs of a chosen bit length using OS-backed randomness. - Encrypt, decrypt, sign, verify - top-level
rsa.encrypt,rsa.decrypt,rsa.sign, andrsa.verifyfunctions implement PKCS#1 v1.5 padding for messages and signatures. - PEM/DER key I/O -
PublicKey/PrivateKey.load_pkcs1()and.save_pkcs1()read and write standard PEM-encoded keys via pyasn1, interoperable with OpenSSL-generated keys. - Command-line tools -
pyrsa-keygen,pyrsa-encrypt,pyrsa-decrypt,pyrsa-sign, andpyrsa-verifyconsole scripts wrap the library for shell use without writing Python. - Parallel key generation -
rsa.parallelspreads prime search across multiple CPU cores viamultiprocessingto speed up otherwise-slow pure-Python key generation.
Common Use Cases
- Signing release artifacts - CI pipelines shell out to
pyrsa-sign/pyrsa-verifyto sign build outputs without a system OpenSSL dependency. - Restricted deployment targets - projects that can’t rely on compiled crypto libraries (no C toolchain, locked-down platforms) use pure-Python RSA for basic encrypt/sign needs.
- Teaching and prototyping RSA - the readable Python implementation of key generation, padding, and primality testing is used in cryptography coursework and prototypes.
- Legacy PKCS#1 v1.5 interoperability - services that need to exchange RSA-encrypted or RSA-signed payloads with PEM keys generated by other PKCS#1-speaking tools.
Under The Hood
Architecture
The codebase is layered from primitives up to the CLI: rsa/core.py implements raw modular exponentiation, rsa/prime.py implements Miller-Rabin primality testing and prime generation on top of rsa/randnum.py’s OS-backed randomness, rsa/key.py defines an AbstractKey base class with PublicKey/PrivateKey subclasses that add PEM/DER loading, saving, and per-instance decryption blinding guarded by a threading.Lock, and rsa/pkcs1.py/rsa/pkcs1_v2.py build PKCS#1 v1.5 and OAEP encrypt/decrypt/sign/verify operations on top of core and key. rsa/pem.py and rsa/asn1.py handle DER/PEM encoding via pyasn1, rsa/parallel.py offers a multiprocessing-based alternative key generator, and rsa/cli.py wires all of the above into the pyrsa-* console scripts. Changing AbstractKey’s blinding/mutex handling would affect thread-safety across every decrypt call in the library.
Tech Stack
Pure Python 3.8+ with a single runtime dependency, pyasn1, for ASN.1/DER (de)serialization of keys. Packaging and builds go through Poetry (pyproject.toml, poetry-core backend), with pyrsa-keygen/pyrsa-encrypt/pyrsa-decrypt/pyrsa-sign/pyrsa-verify/pyrsa-priv2pub registered as console-script entry points. The dev toolchain uses tox for multi-interpreter test runs, mypy for type checking, flake8 for linting, pytest/pytest-cov/coveralls for testing and coverage, and Sphinx for documentation; GitHub Actions runs the tox suite across CPython 3.8-3.12 and PyPy 3.9/3.10.
Code Quality
The test suite is extensive relative to the codebase’s size: dedicated files cover key blinding/unblinding, key generation, PEM load/save round-trips, PKCS#1 v1.5 and OAEP padding, parallel key generation, and the CLI, plus a test_mypy.py that runs mypy against the package as part of the test suite itself. Modules ship a py.typed marker and consistent type hints, docstrings include runnable doctest examples (e.g. gcd() in prime.py), and errors are explicit typed exceptions (DecryptionError, VerificationError) rather than silently swallowed. CI enforces this across the supported interpreter matrix.
What Makes It Unique The library implements standard, well-known PKCS#1 v1.5/OAEP cryptography rather than a novel scheme, and the maintainer states plainly in the README that pure-Python execution cannot guarantee constant-time behavior against timing attacks. Its distinguishing engineering choices are practical mitigations for that constraint: automatic decryption blinding to randomize each private-key operation, and a multiprocessing-based parallel prime search to offset the cost of large-integer primality testing that pure Python performs comparatively slowly.
Used by 3 apps in this directory
Agno
Devops · AI Development · Automation
Build, run, and manage agent platforms with a full production stack — SDK, runtime, and control plane included.
ClickHouse
Databases · Analytics · Data Engineering
Open-source column-oriented database that delivers real-time analytical queries on petabyte-scale data with millisecond latency.
Helicone
Monitoring · AI Development · Analytics
An open-source AI gateway and LLM observability platform that routes requests to 100+ models while logging cost, latency, and full traces for every call.