|
| 1 | +# libffx - Format Preserving Encryption |
| 2 | + |
| 3 | +[](https://www.python.org/downloads/) |
| 4 | +[](https://opensource.org/licenses/MIT) |
| 5 | + |
| 6 | +A Python implementation of the FFX Mode of Operation for Format-Preserving Encryption (FPE). |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +## Quick Start |
| 11 | + |
| 12 | +```python |
| 13 | +import ffx |
| 14 | + |
| 15 | +# 128-bit key (as hex) |
| 16 | +key = ffx.FFXInteger('2b7e151628aed2a6abf7158809cf4f3c', radix=16, blocksize=32) |
| 17 | + |
| 18 | +# Create encrypter for decimal digits |
| 19 | +ffx_obj = ffx.new(key.to_bytes(16), radix=10) |
| 20 | + |
| 21 | +# Encrypt a credit card number |
| 22 | +cc_number = ffx.FFXInteger('4111111111111111', radix=10, blocksize=16) |
| 23 | +tweak = ffx.FFXInteger('0000000000', radix=10, blocksize=10) |
| 24 | + |
| 25 | +encrypted = ffx_obj.encrypt(tweak, cc_number) |
| 26 | +decrypted = ffx_obj.decrypt(tweak, encrypted) |
| 27 | + |
| 28 | +print(f"Original: {cc_number}") # 4111111111111111 |
| 29 | +print(f"Encrypted: {encrypted}") # 3847592710482695 |
| 30 | +print(f"Decrypted: {decrypted}") # 4111111111111111 |
| 31 | +``` |
| 32 | + |
| 33 | +## API Reference |
| 34 | + |
| 35 | +### `ffx.new(key, radix)` |
| 36 | + |
| 37 | +Create a new FFX encrypter. |
| 38 | + |
| 39 | +- `key`: 16-byte AES-128 key |
| 40 | +- `radix`: Base for message alphabet (2-36) |
| 41 | + |
| 42 | +### `FFXInteger(value, radix=2, blocksize=None)` |
| 43 | + |
| 44 | +Represent a value in a specific radix. |
| 45 | + |
| 46 | +- `value`: Integer, string representation, or another FFXInteger |
| 47 | +- `radix`: Base (2-36) |
| 48 | +- `blocksize`: Minimum output length (zero-padded) |
| 49 | + |
| 50 | +### `FFXEncrypter.encrypt(tweak, plaintext)` / `.decrypt(tweak, ciphertext)` |
| 51 | + |
| 52 | +Encrypt/decrypt with an optional tweak (public associated data). |
| 53 | + |
| 54 | +## Specification |
| 55 | + |
| 56 | +This implementation follows the [NIST FFX-A2 specification](http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/ffx/ffx-spec2.pdf): |
| 57 | + |
| 58 | +- **Cipher**: AES-128 |
| 59 | +- **Mode**: Maximally-balanced Feistel network |
| 60 | +- **Rounds**: 10 (constant) |
| 61 | +- **Radix**: 2–36 (binary through alphanumeric) |
| 62 | + |
| 63 | +## Security Considerations |
| 64 | + |
| 65 | +- FFX is designed for format-preserving encryption of small domains |
| 66 | +- Security depends on domain size; very small domains may be vulnerable to brute force |
| 67 | +- Always use cryptographically random keys |
| 68 | +- Tweaks should be unique per encryption when possible |
| 69 | + |
| 70 | +## Links |
| 71 | + |
| 72 | +- [GitHub Repository](https://github.com/kpdyer/libffx) |
| 73 | +- [Issue Tracker](https://github.com/kpdyer/libffx/issues) |
| 74 | +- [NIST FFX Specification](http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/ffx/ffx-spec2.pdf) |
| 75 | + |
| 76 | +## License |
| 77 | + |
| 78 | +MIT License |
0 commit comments