Fernet (Pure Python Implementation)

1.0.1 · maintenance · verified Thu Apr 16

This `fernet` library provides a simple, pure Python implementation of the Fernet symmetric encryption specification. Unlike the `cryptography.fernet` module, this package does not rely on C extensions, making it suitable for environments where C-based compiled modules are undesirable or for use as a simple reference. However, it is explicitly *not recommended* for production or security-sensitive applications due to its age and lack of active maintenance compared to the `cryptography` library's implementation. It was last updated in 2016.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a key, encrypt a message, and decrypt it using the `fernet` library. Keys and messages must be bytes. Remember to store your generated key securely, as anyone with the key can decrypt your data. This example is for demonstration; for production, use `cryptography.fernet`.

import os
from fernet import Fernet

# Generate a new Fernet key. This must be kept secret and securely stored.
# In a real application, you would load this from a secure configuration.
key = Fernet.generate_key()
print(f"Generated Key: {key.decode()}")

# Instantiate Fernet with the key
f = Fernet(key)

# Original message (must be bytes)
original_message = b"My super secret message"

# Encrypt the message
encrypted_message = f.encrypt(original_message)
print(f"Encrypted Message: {encrypted_message}")

# Decrypt the message
decrypted_message = f.decrypt(encrypted_message)
print(f"Decrypted Message: {decrypted_message.decode()}")

# Verify decryption
assert original_message == decrypted_message
print("Decryption successful!")

view raw JSON →