pyffx

raw JSON →
0.3.0 verified Mon Apr 27 auth: no python maintenance

Pure Python implementation of Format Preserving Encryption (FPE) using the FF1 algorithm from NIST SP 800-38G. Supports integer and string alphabets. Current version 0.3.0, no recent releases.

pip install pyffx
error ModuleNotFoundError: No module named 'pyffx'
cause Library not installed or installed in wrong environment.
fix
pip install pyffx
error ValueError: The key must be either 16, 24, or 32 bytes
cause Key length incorrect.
fix
Generate a key of exactly 16, 24, or 32 bytes: key = b'0123456789abcdef'
error ValueError: alphabet must contain exactly 'length' characters
cause Alphabet length mismatch when using custom length.
fix
Ensure alphabet length is >= the length parameter. For Integer, radix is fixed.
error TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
cause Passed a list instead of integer to Integer.encrypt.
fix
Pass an integer: fpe_int.encrypt(12345)
gotcha Key must be exactly 16, 24, or 32 bytes (AES key length). If your key is a string, encode to bytes first.
fix Ensure key is bytes and length is 16, 24, or 32.
gotcha Integer encrypt/decrypt expect a plain integer, not a string. Passing a string will raise TypeError.
fix Cast to int: fpe_int.encrypt(int('123456'))
gotcha String alphabet must contain only unique characters. Duplicates cause incorrect encryption.
fix Use alphabet with unique characters, e.g., '0123456789'.
deprecated Library is unmaintained since 2014. No active development; may not be suitable for production without review.
fix Consider using alternatives like 'pycryptodome' or 'mbi-omapi' for FPE.

Basic usage of Integer and String FPE

from pyffx import Integer, String

# Key must be 16, 24, or 32 bytes
key = b'mysecretkey12345'  # 16 bytes

# Encrypt an integer up to 10^6-1
fpe_int = Integer(key, radix=10, length=6)
plaintext_int = 123456
ciphertext_int = fpe_int.encrypt(plaintext_int)
print(f'{plaintext_int} -> {ciphertext_int}')
decrypted_int = fpe_int.decrypt(ciphertext_int)
print(f'{ciphertext_int} -> {decrypted_int}')

# Encrypt a string with custom alphabet
fpe_str = String(key, alphabet='abcdefghijklmnopqrstuvwxyz', length=10)
plaintext_str = 'helloworld'
ciphertext_str = fpe_str.encrypt(plaintext_str)
print(f'{plaintext_str} -> {ciphertext_str}')
decrypted_str = fpe_str.decrypt(ciphertext_str)
print(f'{ciphertext_str} -> {decrypted_str}')