cryptography
Encrypt, sign and make certificates in Python.
1. License
“"license_expression":"Apache-2.0 OR BSD-3-Clause"”
Choose Apache-2.0 or BSD-3-Clause; both allow paid and closed-source products.
pypi.org/pypi/cryptography/50.0.2/json2. Maintained
github.com/pyca/cryptography3. Used by agents' projects
Dependency in 148 public GitHub projects set up for AI coding agents that we scanned (2.4%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
From the package registry and GitHub, checked daily; card checked Nov 10, 2026. Tap a verdict to see its source.How we checkedReport an error
What cryptography does
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.
What people build with 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.
cryptography popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How cryptography is used
- cryptography is a dependency in 148 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (9.1% of the 1,623 Python ones), #4 of 141 in auth and security libraries.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 1,374; those are left out of every share on this page.
- The most used alternative in our scan is PyJWT (118 projects).
- Projects that use it most often also use python-dotenv (66.2%), Uvicorn (65.5%), Pydantic (64.9%).
By coding agent
Often used with
cryptography in practice
Strengths
The README's example generates a key with <code>Fernet.generate_key()</code>, wraps it in <code>Fernet</code>, then calls <code>encrypt</code> and <code>decrypt</code> on a byte string.
The docs say the recipes level requires "little to no configuration choices". They also say it does not require developers to make many decisions.
The low-level primitives sit in their own <code>cryptography.hazmat</code> package. The docs say their documentation "will always contain an admonition at the top."
The recipes section of the table of contents also lists Cobblestone streaming symmetric encryption in 128- and 256-bit encryptor and decryptor variants. It also lists X.509 pages on Certificate Transparency, OCSP and X.509 Verification.
Watch out for
The docs state that neither the code nor the documentation has been externally audited. They invite contact from anyone interested in discussing an audit.
The maintainers write that hazmat primitives "require making decisions" and in-depth knowledge of the concepts involved. They recommend the recipes layer whenever possible.
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.
More about cryptography
Packages around it
- python-dotenv: .env file loader
- requests: HTTP client
- pydantic: data validation
- uvicorn: ASGI server
- pyyaml: YAML parser
- aiohttp: async HTTP client/server
More in Auth and security libraries
Alternatives
cryptography This page
Pick PyNaCl if you prefer a small set of fixed, hand-picked algorithms; the cryptography FAQ recommends it for that and the same team maintains it.
Pick argon2-cffi to store and check users' login passwords; it hashes them one way instead of encrypting them.
Pick PyJWT when you need standard JSON Web Tokens for logins between services rather than private encrypted tokens.
Modern password hashing library with secure bcrypt algorithm implementation.
Get started
Tell your AI assistant
“Use the Python package cryptography (pin cryptography==50.0.2, Python 3.9+) and its Fernet recipe to encrypt the stored tokens; read the key from the FERNET_KEY environment variable, never hard-code it, and catch InvalidToken.”
Install it into your project's Python environment (Python 3.9 or newer).
$ pip install cryptographyMake 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.'
Rules for your agent
## 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.
Mistakes to avoid
The low-level layer needs many correct decisions; the docs recommend the recipes layer whenever possible.
A new key every start makes all stored tokens unreadable.
Fernet expects a random 32-byte key; a password must be stretched first.
memory_cost is in KiB, so 2**21 asks for 2 GiB per call; the docs give the low-memory setting from RFC 9106.
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.