Format Preserving Encryption (FPE) with FF3
The `ff3` library provides Format Preserving Encryption (FPE) using the NIST FF3 and FF3-1 algorithms. It is currently at version 1.0.3 and receives minor updates for features and compatibility, with the core encryption logic stable since v1.0.0.
Common errors
-
ValueError: Key length must be 16, 24, or 32 bytes
cause The provided encryption key does not meet the AES key length requirements (128, 192, or 256 bits).fixEnsure the `key` byte string is exactly 16, 24, or 32 bytes long. Use `.encode('utf-8')` and slice/pad if starting from a string. -
ValueError: Tweak length must be 7 bytes (FF3-1) or 8 bytes (FF3)
cause The provided `tweak` byte string is not the correct length for the FF3 algorithm variant being used.fixEnsure `tweak` is 8 bytes for standard FF3 (default `FF3Cipher` constructor) or 7 bytes for FF3-1 (if using `FF3Cipher.withFF3_1`). Use `.encode('utf-8')` and slice/pad if starting from a string. -
ImportError: cannot import name 'FF3Cipher' from 'ff3'
cause The `ff3` package is either not installed or the import path is incorrect.fixFirst, ensure installation with `pip install ff3`. If installed, verify the import statement is exactly `from ff3 import FF3Cipher`. -
TypeError: argument 'text' must be str, not bytes
cause The `encrypt` or `decrypt` method received a bytes object when it expects a string plaintext/ciphertext.fixEnsure `plaintext` and `ciphertext` passed to `encrypt`/`decrypt` are `str` type. Note that `key` and `tweak` *must* be `bytes` type.
Warnings
- breaking FF3 (64-bit tweak) and FF3-1 (56-bit tweak) have different tweak length requirements. Using a tweak of incorrect length for the chosen algorithm variant will lead to `ValueError` or incorrect encryption/decryption.
- gotcha The `key` must be 16, 24, or 32 bytes (128, 192, or 256 bits) and the `tweak` must be 7 or 8 bytes. Providing incorrect lengths will result in a `ValueError`.
- gotcha When using a custom alphabet via `FF3Cipher.withCustomAlphabet(key, tweak, custom_alphabet)`, the exact same alphabet must be used for both encryption and decryption, or decryption will fail.
Install
-
pip install ff3
Imports
- FF3Cipher
from ff3 import FF3Cipher
Quickstart
from ff3 import FF3Cipher
import os
# Key must be 16, 24, or 32 bytes (128, 192, or 256 bits)
# Example uses a 32-byte key
key = os.environ.get('FF3_KEY', '0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF').encode('utf-8')[:32]
# Tweak must be 7 bytes for FF3-1, 8 bytes for FF3 (default)
# Example uses an 8-byte tweak for standard FF3
tweak = os.environ.get('FF3_TWEAK', '1234567890ABCDEF').encode('utf-8')[:8]
# Initialize FF3 cipher with key and tweak
cipher = FF3Cipher(key, tweak)
# The default alphabet includes digits 0-9
plaintext = "1234567890"
ciphertext = cipher.encrypt(plaintext)
decrypted_text = cipher.decrypt(ciphertext)
print(f"Original: {plaintext}")
print(f"Encrypted: {ciphertext}")
print(f"Decrypted: {decrypted_text}")
assert plaintext == decrypted_text