hybrid-rsa-aes
Authenticated hybrid RSA and AES encryption for JSON payloads.
Requirements
- Python 3.11–3.14
- An RSA key pair of at least 2048 bits
Install
uv add hybrid-rsa-aes
pip install hybrid-rsa-aes
Quick start
from cryptography.hazmat.primitives.asymmetric import rsa
from hybrid_rsa_aes import HybridCipher
private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
public_key = private_key.public_key()
cipher = HybridCipher()
token = cipher.encrypt(public_key, {"account_id": 42, "roles": ["reader"]})
payload = cipher.decrypt(private_key, token)
assert payload == {"account_id": 42, "roles": ["reader"]}
encrypt accepts standard JSON values and returns a text token. decrypt returns the original JSON value. Tokens are JSON objects with version v, wrapped-key ek, nonce n, and ciphertext ct fields.
Security properties
Each message uses a fresh random AES-256 key and a fresh 96-bit nonce. AES-256-GCM encrypts and authenticates the UTF-8 JSON payload, and RSA-OAEP with SHA-256 encrypts the AES key. The v1 marker is AES-GCM associated data, so a token cannot be relabelled as another format version.
AES-GCM detects modification: treat DecryptionError as an invalid, modified, or wrong-key token. Treat InvalidTokenError as malformed or unsupported token syntax. InvalidKeyError means the supplied key is not an RSA key of at least 2048 bits. PayloadSerializationError means an encryption payload was not standard JSON.
This library protects payload confidentiality and integrity. It does not manage private-key storage, key rotation, replay protection, recipient identity, or application authorization.
Migrating from 0.x
1.0.0 is a breaking security rewrite. It replaces AES-CTR with AES-256-GCM, uses a random AES key instead of a UUID-derived key, and emits versioned JSON tokens. 0.x ciphertexts are not supported and must be decrypted by a 0.x deployment before migration if their plaintext must be retained. The HybridCipher().encrypt(public_key, data) and decrypt(private_key, token) calling pattern remains, but payloads may now be any JSON value and callers must handle the documented exceptions.
Example
uv run python examples/basic_usage.py
Development
uv sync
make lint
make test
make build
License
hybrid-rsa-aes is distributed under the Apache License 2.0.
Release files for hybrid-rsa-aes 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hybrid_rsa_aes-1.0.0.tar.gz | 59.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hybrid_rsa_aes-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 69.3 kB
Release files / hybrid_rsa_aes-1.0.0.tar.gz
| Download URL | hybrid_rsa_aes-1.0.0.tar.gz |
|---|---|
| Size | 59.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ae85babaffdc2ffc20b486979926d60b9809c8bda6ad412541a1467aef15ea6f
|
|
BLAKE2b-256 checksum How to use checksums |
d2a4c4c48f018f5deefd5d0bfad1be0371a4d7c3388857efdbedb94142ce17f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 20, 2026.
Transparency logRelease files / hybrid_rsa_aes-1.0.0-py3-none-any.whl
| Download URL | hybrid_rsa_aes-1.0.0-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
83e1c5517a199a0d833569a3e2627702896c7d25491bec66840202e0d7529713
|
|
BLAKE2b-256 checksum How to use checksums |
379346b4d8906372104787ecb96462b7441d9409a7dba79b560ee474d3b8b3bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 20, 2026.
Transparency log