Skip to main content

libffx - Format Preserving Encryption

Python 3.9+ License: MIT

A Python implementation of the FFX Mode of Operation for Format-Preserving Encryption (FPE).

Format-preserving encryption encrypts data while preserving its format. For example, a 16-digit credit card number encrypts to another 16-digit number, and a 9-digit SSN encrypts to another 9-digit number.

Quick Start

import ffx

# 128-bit key (as hex)
key = ffx.FFXInteger('2b7e151628aed2a6abf7158809cf4f3c', radix=16, blocksize=32)

# Create encrypter for decimal digits
ffx_obj = ffx.new(key.to_bytes(16), radix=10)

# Encrypt a credit card number
cc_number = ffx.FFXInteger('4111111111111111', radix=10, blocksize=16)
tweak = ffx.FFXInteger('0000000000', radix=10, blocksize=10)

encrypted = ffx_obj.encrypt(tweak, cc_number)
decrypted = ffx_obj.decrypt(tweak, encrypted)

print(f"Original:  {cc_number}")   # 4111111111111111
print(f"Encrypted: {encrypted}")   # 3847592710482695
print(f"Decrypted: {decrypted}")   # 4111111111111111

API Reference

ffx.new(key, radix)

Create a new FFX encrypter.

  • key: 16-byte AES-128 key
  • radix: Base for message alphabet (2-36)

FFXInteger(value, radix=2, blocksize=None)

Represent a value in a specific radix.

  • value: Integer, string representation, or another FFXInteger
  • radix: Base (2-36)
  • blocksize: Minimum output length (zero-padded)

FFXEncrypter.encrypt(tweak, plaintext) / .decrypt(tweak, ciphertext)

Encrypt/decrypt with an optional tweak (public associated data).

Specification

This implementation follows the NIST FFX-A2 specification:

  • Cipher: AES-128
  • Mode: Maximally-balanced Feistel network
  • Rounds: 10 (constant)
  • Radix: 2–36 (binary through alphanumeric)

Security Considerations

  • FFX is designed for format-preserving encryption of small domains
  • Security depends on domain size; very small domains may be vulnerable to brute force
  • Always use cryptographically random keys
  • Tweaks should be unique per encryption when possible

Links

License

MIT License

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libffx-1.0.3.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

libffx-1.0.3-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file libffx-1.0.3.tar.gz.

File metadata

  • Download URL: libffx-1.0.3.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for libffx-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ab6a019230f0513dda6815077720086aabb3762fc6d6a9cefb8488f16890cb36
MD5 b303e6337aa017797474bd663392e7e4
BLAKE2b-256 b8c9499846e6f29f143d95b1460ac85820026cedd6cc5397fc2287545fa9930f

See more details on using hashes here.

Provenance

The following attestation bundles were made for libffx-1.0.3.tar.gz:

Publisher: publish.yml on kpdyer/libffx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file libffx-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: libffx-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for libffx-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dd0d36bc012052289e0f7b28dddb0899b74473e89b3be87ba96829b558e6588c
MD5 4fddd81b5396264ae26223ab90d97d9d
BLAKE2b-256 f9fbff8d1c5c6d74c47a63dcd5adc21b7ce6f5d1cdaea414e32440ca3cb9a50d

See more details on using hashes here.

Provenance

The following attestation bundles were made for libffx-1.0.3-py3-none-any.whl:

Publisher: publish.yml on kpdyer/libffx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 files

1.0.2

2 files

1.0.1

2 files

1.0.0

2 files

0.0.2

1 file

0.0.1

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page