Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to encrypt and decrypt data in Python
Cryptography deals with converting plain text into cipher text (encryption) and cipher text back to plain text (decryption). Python's cryptography package provides the Fernet module for symmetric encryption, which uses a single key for both encryption and decryption.
Installation
The cryptography module is not included with Python by default. Install it using pip ?
pip install cryptography
Once installed, import the Fernet module ?
from cryptography.fernet import Fernet
Generating a Key
Before encrypting data, you must generate a Fernet key ?
from cryptography.fernet import Fernet
# Generate a key
key = Fernet.generate_key()
print("Generated key:", key.decode())
# Create Fernet instance with the key
cipher = Fernet(key)
Generated key: Bq64GE--93K1RVro4go1frN-8twBSvXdbCPSPLIKz9U=
Encrypting Data
Use the encrypt() method to convert plain text into encrypted data. The input must be in bytes format ?
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher = Fernet(key)
# Encrypt data (must be bytes)
message = "This is a secret message!"
encrypted_data = cipher.encrypt(message.encode())
print("Original message:", message)
print("Encrypted data:", encrypted_data)
Original message: This is a secret message! Encrypted data: b'gAAAAABhILy91p_wqMntdT3mDkh0IBXSLjuBMQAfnGZAFkZCX1U6Q7TU2PthgFBwVz0QbKXpuNTHRzAgbdDV4zfuuzkGCXqVD--xJdkTycKH2iurC_oqHySLc9xJEXz93LkhTbKUa5HCxfJtB-Um_YkxqjclftXXZQ=='
Decrypting Data
Use the decrypt() method to convert encrypted data back to plain text ?
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher = Fernet(key)
# Encrypt then decrypt
message = "This is a secret message!"
encrypted_data = cipher.encrypt(message.encode())
decrypted_data = cipher.decrypt(encrypted_data)
print("Encrypted:", encrypted_data)
print("Decrypted (bytes):", decrypted_data)
print("Decrypted (string):", decrypted_data.decode())
Encrypted: b'gAAAAABhIL3_qbfM_oMgQn653gpk6a7hqxXiR0dl0vrmOmqnr5b6MqrsjGkK1IknxMLLtOCq6_YlX4x3nBedbZqtCqy4os55pttrl-pBO6-dJf6kVP50IpIaKSXbpAsuWl4h_2o_E-4YEqZ5kkgxWrwnqojmkMyuSQ==' Decrypted (bytes): b'This is a secret message!' Decrypted (string): This is a secret message!
Complete Example
Here's a complete encryption and decryption workflow ?
from cryptography.fernet import Fernet
# Step 1: Generate key
key = Fernet.generate_key()
print("Key:", key.decode())
# Step 2: Create cipher instance
cipher = Fernet(key)
# Step 3: Encrypt data
message = "Confidential information here!"
encrypted_data = cipher.encrypt(message.encode())
print("\nOriginal message:", message)
print("Encrypted data:", encrypted_data)
# Step 4: Decrypt data
decrypted_data = cipher.decrypt(encrypted_data)
print("\nDecrypted message:", decrypted_data.decode())
Key: u4dM7xw8sNNU3Rm_lwDbixudWSeaM0Z4TTDdQNKsouI= Original message: Confidential information here! Encrypted data: b'gAAAAABhIL3_qbfM_oMgQn653gpk6a7hqxXiR0dl0vrmOmqnr5b6MqrsjGkK1IknxMLLtOCq6_YlX4x3nBedbZqtCqy4os55pttrl-pBO6-dJf6kVP50IpIaKSXbpAsuWl4h_2o_E-4YEqZ5kkgxWrwnqojmkMyuSQ==' Decrypted message: Confidential information here!
Key Points
- Always store your key securely − without it, encrypted data cannot be decrypted
- Input data must be in bytes format (use
encode()method) - Decrypted data returns bytes (use
decode()to get string) - The same key is used for both encryption and decryption (symmetric encryption)
Conclusion
Python's Fernet module provides simple symmetric encryption for securing data. Generate a key, encrypt with encrypt(), and decrypt with decrypt() using the same key. Always handle keys securely and remember to encode/decode between string and bytes formats.
