Last modified: Mar 28, 2025 By Alexander Williams

Install Cryptography in Python Step by Step

Cryptography is a vital library for secure data handling in Python. It provides encryption, decryption, and other security features. This guide will help you install it easily.

Prerequisites

Before installing Cryptography, ensure you have Python and pip installed. Check Python version using python --version. Verify pip with pip --version.

 
# Check Python version
python --version

# Check pip version
pip --version


Python 3.9.0
pip 22.0.4

If you encounter errors like ModuleNotFoundError, refer to our guide on solving ModuleNotFoundError.

Install Cryptography Using pip

The easiest way to install Cryptography is via pip. Open your terminal or command prompt and run the following command.

 
pip install cryptography


Successfully installed cryptography-36.0.0

This will download and install the latest version of Cryptography. Ensure you have a stable internet connection.

Verify the Installation

After installation, verify it by importing the module in Python. Open a Python shell and run the following code.

 
import cryptography
print(cryptography.__version__)


36.0.0

If no errors appear, Cryptography is installed correctly. If you face issues, check your Python environment.

Basic Usage of Cryptography

Here’s a simple example to encrypt and decrypt data using Cryptography. This uses Fernet symmetric encryption.

 
from cryptography.fernet import Fernet

# Generate a key
key = Fernet.generate_key()

# Create a Fernet object
cipher = Fernet(key)

# Encrypt a message
message = b"Hello, Cryptography!"
encrypted = cipher.encrypt(message)
print("Encrypted:", encrypted)

# Decrypt the message
decrypted = cipher.decrypt(encrypted)
print("Decrypted:", decrypted)


Encrypted: b'gAAAAABj...'
Decrypted: b'Hello, Cryptography!'

This example shows how easy it is to use Cryptography for basic encryption tasks. Always keep your keys secure.

Common Issues and Fixes

Sometimes, installation fails due to missing dependencies. On Linux, install them first.

 
# For Ubuntu/Debian
sudo apt-get install build-essential libssl-dev libffi-dev python3-dev

# For CentOS/RHEL
sudo yum install gcc openssl-devel libffi-devel python3-devel

After installing dependencies, retry pip install cryptography. If issues persist, check your Python path.

Conclusion

Installing Cryptography in Python is straightforward with pip. This guide covered installation, verification, and basic usage. Secure your data easily with this powerful library.

For more Python tips, explore our other tutorials. Happy coding!