EncryptHelper

class privex.helpers.crypto.EncryptHelper.EncryptHelper(encrypt_key: str, **kwargs)[source]

Symmetric AES-128 encryption/decryption made painless - wrapper class for cryptography.fernet.Fernet

A wrapper class for the cryptography.fernet.Fernet encryption system, designed to make usage of Fernet as painless as possible.

The class EncryptHelper contains various methods for simplifying the use of the Python library Cryptography ‘s cryptography.fernet.Fernet system.

encrypt_str() / decrypt_str() facilitate painless encryption and decryption of data using AES-128 CBC. They can either be passed a 32-byte Fernet key (base64 encoded) as an argument, or leave the key as None and they’ll try to use the key defined on the EncryptHelper instance at EncryptHelper.encrypt_key

Basic usage:

>>> from privex.helpers import EncryptHelper
>>> key = EncryptHelper.generate_key()     # Generates a 32-byte symmetric key, returned as a base64 encoded string
>>> crypt = EncryptHelper(key)             # Create an instance of EncryptHelper, en/decrypting using ``key`` by default
# Encrypts the string 'hello world' with AES-128 CBC using the instance's key, returned as a base64 string
>>> enc = crypt.encrypt_str('hello world')
>>> print(enc)
gAAAAABc7ERTpu2D_uven3l-KtU_ewUC8YWKqXEbLEKrPKrKWT138MNq-I9RRtCD8UZLdQrcdM_IhUU6r8T16lQkoJZ-I7N39g==
>>> crypt.is_encrypted(enc)       # Check if a string/bytes is encrypted (only works with data matching the key)
True
>>> data = crypt.decrypt_str(enc) # Decrypt the encrypted data using the same key, outputs as a string
>>> print(data)
hello world
__init__(encrypt_key: str, **kwargs)[source]

Create an instance of EncryptHelper using the cryptography.fernet.Fernet key encrypt_key as the default key for encrypting/decrypting data.

Parameters

encrypt_key (str) – Base64 encoded Fernet key, used by default for encrypting/decrypting data

Methods

Methods

__init__(encrypt_key, **kwargs)

Create an instance of EncryptHelper using the cryptography.fernet.Fernet key encrypt_key as the default key for encrypting/decrypting data.

decrypt_str(data[, key])

Decrypts data previously encrypted using encrypt_str() with the same Fernet compatible key, and returns the decrypted version as a string.

encrypt_str(data[, key])

Encrypts a piece of data data passed as a string or bytes using Fernet with the passed 32-bit symmetric encryption key key.

from_file(obj, **settings)

Create an instance of EncryptHelper (or inheriting class) using a Fernet key loaded from a file, or stream object.

from_password(password, salt, **settings)

Create an instance of EncryptHelper (or inheriting class) from a password derived Fernet key, instead of a pre-generated Fernet key.

generate_key([output, mode])

Generate a compatible encryption key for use with cryptography.fernet.Fernet

get_fernet([key])

Used internally for getting Fernet instance with auto-fallback to encrypt_key and exception handling.

is_encrypted(data[, key])

Returns True if the passed data appears to be encrypted.

password_key(password[, salt, kdf])

Generate a cryptography.fernet.Fernet key based on a password and salt.