# cryptography — card for AI agents

Python library providing cryptographic recipes and primitives for secure applications.

Category: Auth and security libraries · PyPI package `cryptography` · latest version 50.0.2 (checked 2026-11-01)
Install: `pip install cryptography`
License: Apache-2.0 OR BSD-3-Clause (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (paste into AGENTS.md or CLAUDE.md)

## cryptography
- Prefer the recipes layer, such as `cryptography.fernet.Fernet`, for symmetric encryption. The docs say to fall back to `cryptography.hazmat` "only when necessary".
- Create Fernet keys with `Fernet.generate_key()` and keep them somewhere safe. The README example comments "Put this somewhere safe!"
- Before reaching into `cryptography.hazmat`, note that the docs say these primitives "require making decisions" and in-depth knowledge.
- The project supports Python 3.9+ and PyPy3 7.3.11+.
- Install with `pip install cryptography` or `uv add cryptography`. If the install fails, check the FAQ entries on the Rust compiler and on OpenSSL older than 3.0.0.
- To report a security issue, consult the project's security reporting documentation.

## What it is
- pyca/cryptography is a Python package that provides "cryptographic recipes and primitives". The maintainers write that their goal is for it to be your "cryptographic standard library". The README says it supports Python 3.9+ and PyPy3 7.3.11+. The site gives two install commands: `pip install cryptography` and `uv add cryptography`.
- The docs divide the package into two levels. The table of contents puts Fernet symmetric encryption, Cobblestone streaming encryption and X.509 material under the recipes layer. The docs describe that level as "safe and easy to use". The other level lives in `cryptography.hazmat` and exposes raw primitives. The docs say these "are often dangerous and can be used incorrectly."
- The docs tell readers to use the recipes layer first and to fall back to hazmat "only when necessary". For readers who want to learn the field itself, the site recommends Crypto 101 by Laurens Van Houtven and The Cryptopals Crypto Challenges.

## Caveats
- No external audit: The docs state that neither the code nor the documentation has been externally audited. They invite contact from anyone interested in discussing an audit.
- Hazmat expects expertise: The maintainers write that hazmat primitives "require making decisions" and in-depth knowledge of the concepts involved. They recommend the recipes layer whenever possible.
- Install trouble has its own FAQ entries: The FAQ list includes entries titled for installs that fail with OpenSSL older than 3.0.0, for a missing Rust compiler, and for errors installing or importing on AWS Lambda.
- Listed security limitations: The security section has a "Known security limitations" heading. It covers secure memory wiping and RSA PKCS1 v1.5 constant time decryption.

## Numbers (AgentGid data)
- Gid Score: 77.1 (#158 of all tools, 4 of 141 in its category)
- Used in 148 public projects set up for AI coding agents (2.4%)
- Downloads last week: 315,991,868 (as of 2026-11-24), +12.1% over 4 weeks
- GitHub stars: 7,859 (as of 2026-11-24)
- Last push: 2026-11-24

## Often used with
python-dotenv 66.2%, Uvicorn 65.5%, Pydantic 64.9%, PyYAML 58.8%, Requests 58.1%, FastAPI 54.1%

## Checked alternatives (same job)
- PyJWT: https://agentgid.com/tools/pyjwt/agent.md
- bcrypt (Python): https://agentgid.com/tools/bcrypt-pypi/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/cryptography/
- Homepage: https://github.com/pyca/cryptography
- Repository: https://github.com/pyca/cryptography
- Source: https://github.com/pyca/cryptography#readme
- Source: https://github.com/pyca/cryptography
- Source: https://pypi.org/project/cryptography/
- Source: https://cryptography.io


## cryptography: encrypt, sign and make certificates in Python

cryptography is the main free Python library for keeping data secret and proving it was not changed. Its simple part, Fernet, locks a piece of text with a key so nobody without the key can read or alter it. It can also sign messages, turn a password into a key and create certificates for secure connections.

### Where to use it
- Store secrets your app must reuse: Your app saves customers' tokens for other services and needs them back later; it keeps them encrypted in the database and decrypts them only when calling out.
- Links that stop working: A password reset or invite link carries an encrypted token that your server refuses once it is older than, say, 15 minutes.
- Replace a key after staff changes: Someone who knew your encryption key leaves; you add a new key, re-encrypt stored tokens with it and then retire the old one.
- Encrypt with a person's passphrase: A notes or backup feature lets people protect their data with a passphrase they choose; the passphrase is stretched into a proper key first.
- Prove a message came from you: Your server signs an order receipt or a file, and anyone holding your public key can check that nobody changed it.
- Certificate for local secure testing: Your development server needs a certificate for localhost so you can test secure connections before going live.

### How to start
- Install it into your project's Python environment (Python 3.9 or newer).

```
$ pip install cryptography
```

- Make a key once, keep it secret, and use Fernet to encrypt and decrypt.

```
>>> from cryptography.fernet import Fernet
>>> # Put this somewhere safe!
>>> key = Fernet.generate_key()
>>> f = Fernet(key)
>>> token = f.encrypt(b"A really secret message. Not for prying eyes.")
>>> token
b'...'
>>> f.decrypt(token)
b'A really secret message. Not for prying eyes.'
```


### License
Free to use in paid and closed-source products; you may pick either the Apache 2.0 or the BSD 3-clause license. Keep the copyright and license notices when you ship it.

### Good to know
- If you lose a Fernet key, nothing encrypted with it can be read again; anyone who gets the key can read and forge messages.
- Each Fernet token shows, unencrypted, when it was made, so an attacker can see that time.
- Fernet works on data that fits in memory; for very large files the library added a separate streaming recipe, Cobblestone, in version 50.
- The low-level part, called hazmat, is easy to misuse; the docs advise the simple recipes whenever possible.
- Since version 49 there are ready-made installs only for Apple silicon Macs and 64-bit Windows; older Intel Macs must build it themselves.

Checked 2026-11-10 against: https://github.com/pyca/cryptography/blob/main/README.rst, https://github.com/pyca/cryptography/blob/main/LICENSE, https://github.com/pyca/cryptography/blob/main/LICENSE.APACHE, https://github.com/pyca/cryptography/blob/main/LICENSE.BSD, https://pypi.org/pypi/cryptography/50.0.2/json, https://cryptography.io/en/stable/, https://cryptography.io/en/stable/fernet/, https://cryptography.io/en/stable/installation/, https://cryptography.io/en/stable/changelog/, https://cryptography.io/en/stable/x509/tutorial/, https://cryptography.io/en/stable/limitations/, https://cryptography.io/en/stable/hazmat/primitives/asymmetric/ed25519/, https://cryptography.io/en/stable/hazmat/primitives/key-derivation-functions/, https://cryptography.io/en/stable/faq/, https://cryptography.io/en/stable/api-stability/

### Mistakes to avoid
- Don't: Building encryption from hazmat pieces (AES plus your own mode and padding) for ordinary app data → Do: Use Fernet from cryptography.fernet unless a fixed outside format forces the low-level parts (The low-level layer needs many correct decisions; the docs recommend the recipes layer whenever possible.)
- Don't: Calling Fernet.generate_key() at app start-up, or keeping the key only in memory → Do: Generate the key once, store it as a secret (FERNET_KEY in the environment) and back it up (A new key every start makes all stored tokens unreadable.)
- Don't: Fernet(password.encode()) or a padded password used as the key → Do: Derive the key with a key derivation function such as Argon2id, store the random salt, then base64-encode it for Fernet (Fernet expects a random 32-byte key; a password must be stretched first.)
- Don't: Copying the docs' Argon2id example (memory_cost=2**21) onto a small server → Do: On machines with little memory use iterations=3, memory_cost=2**16, as our demo did (memory_cost is in KiB, so 2**21 asks for 2 GiB per call; the docs give the low-memory setting from RFC 9106.)
- Don't: Encrypting a multi-gigabyte file with one Fernet call → Do: Use the Cobblestone streaming recipe (version 50+) or encrypt in separate parts (Fernet needs the whole message in memory.)

### Questions
- Can I use it in a paid, closed-source app? Yes. You choose between Apache 2.0 and BSD 3-clause; both allow commercial and closed-source use with the notices kept.
- Should I store user login passwords with Fernet? No. Fernet can be undone with the key. Login passwords should go through a slow one-way function such as Argon2, which the docs describe as designed for password storage.
- How is it different from PyNaCl? cryptography is a general-purpose library that works with existing systems; PyNaCl offers a smaller, hand-picked set of algorithms and is made by the same team.
- Will updates break my code? Documented functions are not removed or renamed without a compatibility alias, except when a security fix requires it.
- Is it maintained? Yes. Version 50.0.2 came out on 30 September 2026, and new major versions appear every few months.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
