Format Preserving Encryption (FPE) with FF3

1.0.3 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

Encrypt and decrypt a numeric string using the default FF3 algorithm with a specified key and tweak.

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

view raw JSON →