Skip to Content
oxcrypto

crypto_box

Public-key authenticated encryption compatible with libsodium crypto_box.

from oxcrypto.nacl.crypto_box import CRYPTO_BOX
from oxcrypto.random import bytes as random_bytes

box = CRYPTO_BOX.XSALSA20POLY1305
alice, bob = box.generate(), box.generate()
nonce = random_bytes(24)
ciphertext = box.encrypt(alice, bob.public_key(), nonce, b"hello")
assert box.decrypt(bob, alice.public_key(), nonce, ciphertext) == b"hello"

sealed = box.seal(bob.public_key(), b"anonymous")
assert box.seal_open(bob, sealed) == b"anonymous"
Variantlibsodium nameKey exchangeCipher
XSALSA20POLY1305crypto_box_curve25519xsalsa20poly1305X25519, HSalsa20XSalsa20-Poly1305
XCHACHA20POLY1305crypto_box_curve25519xchacha20poly1305X25519, HChaCha20XChaCha20-Poly1305

encrypt and decrypt take your private key and the peer's public key, a 24-byte nonce and the data. The output is the 16-byte tag followed by the ciphertext, as in crypto_box_easy. seal and seal_open implement crypto_box_seal and crypto_box_seal_open: the output is the 32-byte ephemeral public key followed by the box. They are available for XSALSA20POLY1305 only; the other variant raises ValueError.

Keys are raw 32-byte X25519 keys and work with both variants. Load them with load_private_bytes and load_public_bytes, and export them with to_bytes.

PropertyValue
StandardNaCl and libsodium construction; not an IETF or NIST standard. X25519 is RFC 7748.
Cratecrypto_box 0.9.1, stable, no published audit
MSRVRust 1.60 declared; builds with Rust 1.88
Test vectorscrypto_box 0.9.1 tests/lib.rs, adapted from PHP sodium_compat